diff --git a/packages/ui/theme/__tests__/resolver.test.js b/packages/ui/theme/__tests__/resolver.test.js deleted file mode 100644 index d0a1eebe5..000000000 --- a/packages/ui/theme/__tests__/resolver.test.js +++ /dev/null @@ -1,71 +0,0 @@ -import resolver from '../resolver'; - -describe('resolver', () => { - describe('creator', () => { - test('should flatten theme', () => { - const r = resolver({ - foo: { - bar: 'red', - }, - }); - expect(r.references()).toEqual({ - 'foo.bar': 'red', - }); - }); - - test('should resolve theme variables', () => { - const r = resolver({ - shadow: { - colorful: '2px $palette.bright.primary', - }, - palette: { - bright: { - primary: '$palette.light', - }, - light: 'ocean', - }, - }); - expect(r.references()).toEqual({ - 'shadow.colorful': '2px ocean', - 'palette.bright.primary': 'ocean', - 'palette.light': 'ocean', - }); - }); - - test('should throw when reference is cyclical', () => { - const fn = () => - resolver({ - foo: { - bar: '4px $foo.bar', - }, - }); - expect(fn).toThrow('Cyclical reference for "$foo.bar"'); - }); - }); - - describe('resolver', () => { - test('should resolve variable references', () => { - const r = resolver({ - typography: { - fontFamily: 'Arial', - }, - }); - expect( - r.resolve({ - font: '16px $typography.fontFamily', - }) - ).toEqual({ - font: '16px Arial', - }); - }); - - test('should resolve value references', () => { - const r = resolver({ - typography: { - fontFamily: 'Arial', - }, - }); - expect(r.get('16px $typography.fontFamily')).toBe('16px Arial'); - }); - }); -}); diff --git a/packages/ui/theme/resolver.js b/packages/ui/theme/resolver.js deleted file mode 100644 index ba65fad27..000000000 --- a/packages/ui/theme/resolver.js +++ /dev/null @@ -1,69 +0,0 @@ -/* eslint no-param-reassign: 0 */ - -const VARIABLE_RX = /\$[A-z0-9.]+/g; - -function throwCyclical(s) { - throw new Error(`Cyclical reference for "${s}"`); -} - -function resolveStyle(style, references, replacer) { - const s = {}; - - Object.keys(style).forEach((key) => { - let value = style[key]; - if (VARIABLE_RX.test(value)) { - value = value.replace(VARIABLE_RX, replacer); - } - s[key] = value; - }); - return s; -} - -function createRefs(obj, refs, s = '') { - Object.keys(obj).forEach((key) => { - if (typeof obj[key] === 'string') { - refs[`${s}${key}`] = obj[key]; - } else if (typeof obj[key] === 'object') { - createRefs(obj[key], refs, `${s}${key}.`); - } - }); -} - -function resolver(theme = {}) { - const refs = {}; - createRefs(theme, refs, ''); - const replacer = (match) => refs[match.substring(1)]; - - function resolveValueReferences(value, path = []) { - const variables = typeof value === 'string' ? value.match(VARIABLE_RX) : null; - if (variables && variables.length) { - variables.forEach((v) => { - const ref = v.substring(1); - if (path.indexOf(ref) !== -1) { - throwCyclical(v); - } - const refValue = resolveValueReferences(refs[ref], [...path, ref]); - value = value.replace(v, refValue); - }); - } - return value; - } - - Object.keys(refs).forEach((key) => { - refs[key] = resolveValueReferences(refs[key], [key]); - }); - - return { - get(value) { - return resolveValueReferences(value); - }, - resolve(style) { - return resolveStyle(style, refs, replacer); - }, - references() { - return refs; - }, - }; -} - -export default resolver;