-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
102 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
function getClassNameGenerator() { | ||
const cache = new Map<string, string>(); | ||
|
||
function escapeString(string: string) { | ||
if (cache.has(string)) { | ||
return cache.get(string); | ||
} | ||
|
||
const escaped = string.replace(/[^\w\s]/gi, ''); | ||
cache.set(string, escaped); | ||
|
||
return escaped; | ||
} | ||
|
||
function makeRuleName(property: string, value: string) { | ||
return `${escapeString(property)}-${escapeString(value)}`; | ||
} | ||
|
||
function generator(style: Record<string, any> = {}) { | ||
const className = Object.keys(style).reduce((acc, curr) => { | ||
let value = style[curr]; | ||
if (typeof value !== 'string') { | ||
value = generator(value); | ||
} | ||
const prefix = acc ? '_' : ''; | ||
const ruleName = makeRuleName(curr, value); | ||
return `${acc}${prefix}${ruleName}`; | ||
}, ''); | ||
|
||
return className; | ||
} | ||
|
||
return generator; | ||
} | ||
|
||
export const generateClassName = getClassNameGenerator(); |
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,17 @@ | ||
import { generateClassName } from '../src/generateClassName'; | ||
|
||
describe('generateClassName', () => { | ||
it('should generate the class from the style object', () => { | ||
const className = generateClassName({ | ||
bg: 'primary', | ||
}); | ||
|
||
expect(className).toBe('bg-primary'); | ||
}); | ||
|
||
it('should return an empty string in case the style object is not provided', () => { | ||
const className = generateClassName(); | ||
|
||
expect(className).toBe(''); | ||
}); | ||
}); |
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