-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use sanitized component name for variable naming in default case
- Loading branch information
1 parent
e91c8d0
commit 55e8a43
Showing
8 changed files
with
46 additions
and
7 deletions.
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
11 changes: 11 additions & 0 deletions
11
code/lib/core-server/src/utils/get-component-variable-name.test.ts
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,11 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { getComponentVariableName } from './get-component-variable-name'; | ||
|
||
describe('get-variable-name', () => { | ||
it('should return a valid variable name for a given string', () => { | ||
expect(getComponentVariableName('foo-bar')).toBe('FooBar'); | ||
expect(getComponentVariableName('foo bar')).toBe('FooBar'); | ||
expect(getComponentVariableName('0-foo-bar')).toBe('FooBar'); | ||
expect(getComponentVariableName('*Foo-bar-$')).toBe('FooBar$'); | ||
}); | ||
}); |
13 changes: 13 additions & 0 deletions
13
code/lib/core-server/src/utils/get-component-variable-name.ts
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,13 @@ | ||
import camelcase from 'camelcase'; | ||
|
||
/** | ||
* Get a valid variable name for a component. | ||
* | ||
* @param name The name of the component. | ||
* @returns A valid variable name. | ||
*/ | ||
export const getComponentVariableName = (name: string) => { | ||
const camelCased = camelcase(name.replace(/^[^a-zA-Z_$]*/, ''), { pascalCase: true }); | ||
const sanitized = camelCased.replace(/[^a-zA-Z_$]+/, ''); | ||
return sanitized; | ||
}; |
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
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
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