Skip to content

Commit c1f0f2f

Browse files
authored
Merge pull request #384 from BJJLangedijk/main
feat(Typography): prevent camelCasing typography values
2 parents 3002d10 + 89d3980 commit c1f0f2f

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const formatTokens = (
4040
const result = {}
4141
Object.keys(allTokenObj).forEach((key) => {
4242
const keys = key.split('.').filter((k) => k !== type)
43-
makeSdObject(result, keys, allTokenObj[key])
43+
makeSdObject(result, keys, allTokenObj[key], keys[0] !== 'typography')
4444
})
4545

4646
return JSON.stringify(result, null, 2)

src/tests/utils.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,40 @@ describe('makeSdObject function', () => {
7575
}
7676
})
7777
})
78+
79+
it('should camelCase values when setCasing is not given', () => {
80+
const obj: { [key: string]: string } = {
81+
'foo.foo-bar': 'bar',
82+
}
83+
84+
const result = {}
85+
Object.keys(obj).forEach((key) => {
86+
const keys = key.split('.').filter((k) => k !== 'colors')
87+
makeSdObject(result, keys, obj[key])
88+
})
89+
90+
expect(result).toEqual({
91+
foo: {
92+
fooBar: 'bar'
93+
}
94+
})
95+
})
96+
97+
it('should not camelCase when setCasing is set to false', () => {
98+
const obj: { [key: string]: string } = {
99+
'typography.foo-bar': 'bar',
100+
}
101+
102+
const result = {}
103+
Object.keys(obj).forEach((key) => {
104+
const keys = key.split('.').filter((k) => k !== 'colors')
105+
makeSdObject(result, keys, obj[key], false)
106+
})
107+
108+
expect(result).toEqual({
109+
typography: {
110+
'foo-bar': 'bar'
111+
}
112+
})
113+
})
78114
})

src/utils.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@ export const addHyphen = (str: string) => {
88
export const makeSdObject = <T extends readonly string[]>(
99
obj: SdObjType<{ [key: string]: any }>,
1010
keys: T,
11-
value: string
11+
value: string,
12+
setCasing = true
1213
): void => {
1314
const lastIndex = keys.length - 1
1415
for (let i = 0; i < lastIndex; ++i) {
15-
const key = camelCase(keys[i])
16+
let key = keys[i];
17+
18+
if (setCasing) {
19+
key = camelCase(keys[i]);
20+
}
21+
1622
if (!(key in obj)) {
1723
obj[key] = {}
1824
}
@@ -21,6 +27,10 @@ export const makeSdObject = <T extends readonly string[]>(
2127

2228
// https://v2.tailwindcss.com/docs/upgrading-to-v2#update-default-theme-keys-to-default
2329
if (keys[lastIndex] === 'DEFAULT') {
30+
setCasing = false;
31+
}
32+
33+
if (!setCasing) {
2434
obj[keys[lastIndex]] = value
2535
} else {
2636
obj[camelCase(keys[lastIndex])] = value

0 commit comments

Comments
 (0)