Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Typography): prevent camelCasing typography values #384

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const formatTokens = (
const result = {}
Object.keys(allTokenObj).forEach((key) => {
const keys = key.split('.').filter((k) => k !== type)
makeSdObject(result, keys, allTokenObj[key])
makeSdObject(result, keys, allTokenObj[key], keys[0] !== 'typography')
})

return JSON.stringify(result, null, 2)
Expand Down
36 changes: 36 additions & 0 deletions src/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,40 @@ describe('makeSdObject function', () => {
}
})
})

it('should camelCase values when setCasing is not given', () => {
const obj: { [key: string]: string } = {
'foo.foo-bar': 'bar',
}

const result = {}
Object.keys(obj).forEach((key) => {
const keys = key.split('.').filter((k) => k !== 'colors')
makeSdObject(result, keys, obj[key])
})

expect(result).toEqual({
foo: {
fooBar: 'bar'
}
})
})

it('should not camelCase when setCasing is set to false', () => {
const obj: { [key: string]: string } = {
'typography.foo-bar': 'bar',
}

const result = {}
Object.keys(obj).forEach((key) => {
const keys = key.split('.').filter((k) => k !== 'colors')
makeSdObject(result, keys, obj[key], false)
})

expect(result).toEqual({
typography: {
'foo-bar': 'bar'
}
})
})
})
14 changes: 12 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ export const addHyphen = (str: string) => {
export const makeSdObject = <T extends readonly string[]>(
obj: SdObjType<{ [key: string]: any }>,
keys: T,
value: string
value: string,
setCasing = true
): void => {
const lastIndex = keys.length - 1
for (let i = 0; i < lastIndex; ++i) {
const key = camelCase(keys[i])
let key = keys[i];

if (setCasing) {
key = camelCase(keys[i]);
}

if (!(key in obj)) {
obj[key] = {}
}
Expand All @@ -21,6 +27,10 @@ export const makeSdObject = <T extends readonly string[]>(

// https://v2.tailwindcss.com/docs/upgrading-to-v2#update-default-theme-keys-to-default
if (keys[lastIndex] === 'DEFAULT') {
setCasing = false;
}

if (!setCasing) {
obj[keys[lastIndex]] = value
} else {
obj[camelCase(keys[lastIndex])] = value
Expand Down
Loading