Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix trimValue when value is object #10

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});