diff --git a/src/index.ts b/src/index.ts index c83b36a..a5f8679 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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) diff --git a/src/tests/utils.test.ts b/src/tests/utils.test.ts index ae5a1fb..62acc74 100644 --- a/src/tests/utils.test.ts +++ b/src/tests/utils.test.ts @@ -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' + } + }) + }) }) diff --git a/src/utils.ts b/src/utils.ts index 4ed2973..b2100e8 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -8,11 +8,17 @@ export const addHyphen = (str: string) => { export const makeSdObject = ( 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] = {} } @@ -21,6 +27,10 @@ export const makeSdObject = ( // 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