Skip to content

Commit

Permalink
fix: allow trimValue to work with values shaped like objects
Browse files Browse the repository at this point in the history
* fix: allow trimValue to work with values shaped like objects
  • Loading branch information
mihkeleidast authored May 2, 2022
1 parent 0c94edd commit 31493c5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/fair-mangos-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@divriots/style-dictionary-to-figma': patch
---

Fixes trimValue when used on values that are objects.
19 changes: 12 additions & 7 deletions src/trim-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
Expand Down
32 changes: 32 additions & 0 deletions test/trim-value.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 31493c5

Please sign in to comment.