Skip to content

Commit 8e5d09e

Browse files
committed
lib features updated
1 parent f20c53f commit 8e5d09e

File tree

7 files changed

+66
-16
lines changed

7 files changed

+66
-16
lines changed

lib/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export declare function createModuleStylesConverter(styles: any): (...classNames: string[]) => string;
1+
export declare function createModuleStylesConverter(styles?: Record<string, string>): (...classNames: (string | Record<string, boolean>)[]) => string;

lib/index.js

Lines changed: 15 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const gs = createModuleStylesConverter(styles);
2828
gs('class class-two'); // provide all styles in a string OR
2929
gs('class', 'class-two'); // provide styles in multiple strings OR
3030
gs('class', 'class-two class-three') // provide using mix of two types above
31+
gs('class', 'class-two', {'class-three': true}) // provide using object with boolean
3132

3233
<span className={gs('text text_big text_marked')}>Big marked text here...</span>
3334
```

src/index.test.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,36 @@ it('works with mixed types of providing classNames', () => {
4646
});
4747

4848
it('throws exception when no styles provided', () => {
49-
expect(() => createModuleStylesConverter(null)).toThrow();
49+
expect(() => createModuleStylesConverter()).toThrow();
5050
});
51+
52+
it('works with object like { className: true }', () => {
53+
// emulating module styles object
54+
const styles = {
55+
text: 'text_HASH',
56+
red: 'red_HASH',
57+
big: 'big_HASH',
58+
};
59+
60+
// getStyle (gs)
61+
const gs = createModuleStylesConverter(styles);
62+
const classNames = gs('text', { red: false }, { big: true });
63+
64+
expect(classNames).toBe('text_HASH big_HASH');
65+
});
66+
67+
it('returns the original class name if it is not specified in the module', () => {
68+
// emulating module styles object
69+
const styles = {
70+
text: 'text_HASH',
71+
red: 'red_HASH',
72+
big: 'big_HASH',
73+
};
74+
75+
// getStyle (gs)
76+
const gs = createModuleStylesConverter(styles);
77+
const classNames = gs('text', 'red', 'big', 'small', 'green');
78+
79+
expect(classNames).toBe('text_HASH red_HASH big_HASH small green');
80+
})
81+

src/index.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
export function createModuleStylesConverter(styles) {
2-
if (!styles) throw "There is no styles provided (null or undefined)";
1+
export function createModuleStylesConverter(styles?: Record<string, string>) {
2+
if (!styles) throw 'There is no styles provided (null or undefined)';
33
const closureStyles = styles;
4-
5-
return (...classNames: string[]) => {
6-
const splitted = classNames.flatMap(cname => cname.split(' '));
7-
const styled = splitted.map((val) => closureStyles[val]);
4+
5+
return (...classNames: (string | Record<string, boolean>)[]) => {
6+
const splitted = classNames.flatMap(cname =>
7+
typeof cname === 'string'
8+
? cname.split(' ')
9+
: Object.entries(cname)
10+
.filter(([_key, value]) => value)
11+
.map(([key]) => key),
12+
);
13+
const styled = splitted.map(val => closureStyles[val] ?? val);
814
return styled.join(' ');
9-
}
10-
}
15+
};
16+
}

0 commit comments

Comments
 (0)