Skip to content

Commit

Permalink
feat: add deduplicate props
Browse files Browse the repository at this point in the history
  • Loading branch information
yarastqt committed Jun 4, 2020
1 parent 6891948 commit 61a0b25
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/core-v2/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createWhitepaperConfig } from './whitepaper-config'
import { variablesWithPrefix } from './variable-with-prefix'
import { loadMappers } from './mappers'
import { loadThemes } from './themes'
import { dedupeProps } from './dedupe-props'

const store = new Map()

Expand Down Expand Up @@ -69,6 +70,7 @@ export async function build(config: any): Promise<any> {
})
}
const StyleDictionary = StyleDictionaryApi.extend(styleDictionaryConfig)
StyleDictionary.properties = dedupeProps(StyleDictionary.properties)
StyleDictionary.buildPlatform('css')
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/core-v2/dedupe-props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Property } from 'style-dictionary'
import merge from 'deepmerge'

function toDeepToken(path: string, prop: Property): any {
const chunks = path.split('-').reverse()
let result: any = prop
for (let i = 0; i < chunks.length; i++) {
result = { [chunks[i]]: result }
}
return result
}

export function dedupeProps<T extends Record<string, Property>>(props: T): T {
let source = {}
let overrides = {}
for (const propKey in props) {
if (propKey.match(/-/)) {
overrides = merge(overrides, toDeepToken(propKey, props[propKey]))
} else {
source = merge(source, { [propKey]: props[propKey] })
}
}
return merge<T>(source, overrides)
}

0 comments on commit 61a0b25

Please sign in to comment.