-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: convert field name to valid variable name before defining it
- Loading branch information
1 parent
5fcde09
commit 4965ded
Showing
5 changed files
with
692 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* @vinejs/compiler | ||
* | ||
* (c) VineJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
const NUMBER_CHAR_RE = /\d/ | ||
const VALID_CHARS = /[A-Za-z0-9]+/ | ||
|
||
/** | ||
* Returns if the first charcater is uppercase or not | ||
*/ | ||
function isUppercase(char = ''): boolean | undefined { | ||
if (NUMBER_CHAR_RE.test(char)) { | ||
return undefined | ||
} | ||
return char !== char.toLowerCase() | ||
} | ||
|
||
/** | ||
* Converts the first character to uppercase | ||
*/ | ||
function upperFirst(value: string): string { | ||
return value ? value[0].toUpperCase() + value.slice(1) : '' | ||
} | ||
|
||
/** | ||
* Converts the first character to lowercase | ||
*/ | ||
function lowerFirst(value: string) { | ||
return value ? value[0].toLowerCase() + value.slice(1) : '' | ||
} | ||
|
||
/** | ||
* Splits the string value by special characters | ||
*/ | ||
function splitByCase(value: string) { | ||
const parts: string[] = [] | ||
if (!value || typeof value !== 'string') { | ||
return parts | ||
} | ||
|
||
let buff = '' | ||
let previousUpper: boolean | undefined | ||
let previousSplitter: boolean | undefined | ||
|
||
for (const char of value) { | ||
const isSplitter = !VALID_CHARS.test(char) | ||
if (isSplitter === true) { | ||
parts.push(buff) | ||
buff = '' | ||
previousUpper = undefined | ||
continue | ||
} | ||
|
||
const isUpper = isUppercase(char) | ||
if (previousSplitter === false) { | ||
if (previousUpper === false && isUpper === true) { | ||
parts.push(buff) | ||
buff = char | ||
previousUpper = isUpper | ||
continue | ||
} | ||
|
||
if (previousUpper === true && isUpper === false && buff.length > 1) { | ||
const lastChar = buff.at(-1) | ||
parts.push(buff.slice(0, Math.max(0, buff.length - 1))) | ||
buff = lastChar + char | ||
previousUpper = isUpper | ||
continue | ||
} | ||
} | ||
|
||
buff += char | ||
previousUpper = isUpper | ||
previousSplitter = isSplitter | ||
} | ||
|
||
parts.push(buff) | ||
return parts | ||
} | ||
|
||
/** | ||
* Converts the value to a valid JavaScript variable name | ||
*/ | ||
export function toVariableName(value: string) { | ||
const pascalCase = splitByCase(value) | ||
.map((p) => upperFirst(p.toLowerCase())) | ||
.join('') | ||
|
||
return /^[0-9]+/.test(pascalCase) ? `var_${pascalCase}` : lowerFirst(pascalCase) | ||
} |
This file contains 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
Oops, something went wrong.