Created
June 18, 2024 09:46
-
-
Save tubackkhoa/e8fc09a0b0e44f46d36ef55aa90b4ebe to your computer and use it in GitHub Desktop.
convert snake case to camel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { readdirSync, readFileSync, writeFileSync } = require('fs'); | |
const { join } = require('path'); | |
function walk(dir, ext = '.rs') { | |
return readdirSync(dir, { withFileTypes: true }) | |
.flatMap((file) => (file.isDirectory() ? walk(join(dir, file.name), ext) : file.name.endsWith(ext) ? join(dir, file.name) : null)) | |
.filter(Boolean); | |
} | |
const reg = /(?<=#\[wasm_bindgen)\]([\n\t\s])+(?=pub\s+fn\s+([\w_]+)\()/g; | |
const rustFiles = walk('wasm'); | |
for (const file of rustFiles) { | |
const fileContent = readFileSync(file) | |
.toString() | |
.replace(reg, (_, g1, g2) => { | |
const fnName = g2 | |
.split('_') | |
.map((part, i) => (i > 0 ? part[0].toUpperCase() + part.substr(1) : part)) | |
.join(''); | |
return `(js_name = ${fnName})]${g1}#[allow(non_snake_case)]${g1}`; | |
}); | |
writeFileSync(file, fileContent); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment