From 31493c58e189b9e1daa46743b5688367fbe0c23d Mon Sep 17 00:00:00 2001 From: Mihkel Eidast Date: Mon, 2 May 2022 17:38:48 +0300 Subject: [PATCH] fix: allow trimValue to work with values shaped like objects * fix: allow trimValue to work with values shaped like objects --- .changeset/fair-mangos-collect.md | 5 +++++ src/trim-value.js | 19 +++++++++++------- test/trim-value.test.js | 32 +++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 .changeset/fair-mangos-collect.md diff --git a/.changeset/fair-mangos-collect.md b/.changeset/fair-mangos-collect.md new file mode 100644 index 0000000..49f3466 --- /dev/null +++ b/.changeset/fair-mangos-collect.md @@ -0,0 +1,5 @@ +--- +'@divriots/style-dictionary-to-figma': patch +--- + +Fixes trimValue when used on values that are objects. diff --git a/src/trim-value.js b/src/trim-value.js index 26f06c7..dd4930d 100644 --- a/src/trim-value.js +++ b/src/trim-value.js @@ -4,17 +4,22 @@ /** * @param {Obj} obj + * @param {boolean} isValueObj * @returns {Obj} */ -export function trimValue(obj) { +export function trimValue(obj, isValueObj = false) { const newObj = { ...obj }; Object.keys(newObj).forEach(key => { - if (key === 'value') { - const val = /** @type {string} */ (newObj[key]); - const reg = /^\{(.*)\}$/g; - const matches = reg.exec(val); - if (matches && matches[1]) { - newObj[key] = val.replace('.value', ''); + if (key === 'value' || isValueObj) { + if (typeof newObj[key] === 'string') { + const val = /** @type {string} */ (newObj[key]); + const reg = /^\{(.*)\}$/g; + const matches = reg.exec(val); + if (matches && matches[1]) { + newObj[key] = val.replace('.value', ''); + } + } else if (typeof newObj[key] === 'object') { + newObj[key] = trimValue(/** @type {Obj} */ (newObj[key]), true); } } else if (typeof newObj[key] === 'object') { newObj[key] = trimValue(/** @type {Obj} */ (newObj[key])); diff --git a/test/trim-value.test.js b/test/trim-value.test.js index a2b505a..ceef826 100644 --- a/test/trim-value.test.js +++ b/test/trim-value.test.js @@ -55,4 +55,36 @@ describe('trim-value', () => { expect(trimmedObj).to.eql(expectedObj); }); + + it('trims away any .value from reference values in nested objects when value is object', () => { + const obj = { + shadow: { + value: { + x: '0', + y: '1', + blur: '2', + spread: '0', + color: '{color.accent.base.value}', + type: 'dropShadow', + }, + }, + }; + + const expectedObj = { + shadow: { + value: { + x: '0', + y: '1', + blur: '2', + spread: '0', + color: '{color.accent.base}', + type: 'dropShadow', + }, + }, + }; + + const trimmedObj = trimValue(obj); + + expect(trimmedObj).to.eql(expectedObj); + }); });