Skip to content

Commit

Permalink
[FIX] Nested token-parsing (#1347)
Browse files Browse the repository at this point in the history
  • Loading branch information
KenAJoh authored Nov 17, 2021
1 parent e3cf943 commit 9338995
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 5 deletions.
2 changes: 1 addition & 1 deletion @navikt/core/tokens/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
},
{
"destination": "tokens-cjs.js",
"format": "javascript/module"
"format": "javascript/module-flat"
}
]
}
Expand Down
9 changes: 6 additions & 3 deletions @navikt/core/tokens/figma/deepen.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import unifyNestedLevel from "./unify-nested-level";

const deepen = (obj: { [key: string]: { value: string } }) => {
const result = {};

const workObj = unifyNestedLevel(obj);

// For each object path (property key) in the object
for (const objectPath in obj) {
for (const objectPath in workObj) {
// Split path into component parts
const parts = objectPath.split("-");

Expand All @@ -12,9 +16,8 @@ const deepen = (obj: { [key: string]: { value: string } }) => {
const part = parts.shift();
target = target[part] = target[part] || {};
}

// Set value at end of path
target[parts[0]] = obj[objectPath];
target[parts[0]] = workObj[objectPath];
}

return result;
Expand Down
2 changes: 1 addition & 1 deletion @navikt/core/tokens/figma/tests/format-sd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ describe("Check conversion to Styled-dictionary format", () => {
semantic: {
color: {
danger: {
value: "{navds.global.color.red.400.value}",
hover: {
value: "{navds.global.color.red.300.value}",
},
"@": { value: "{navds.global.color.red.400.value}" },
},
},
},
Expand Down
43 changes: 43 additions & 0 deletions @navikt/core/tokens/figma/tests/unify-nested-level.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import unifyNestedLevel from "../unify-nested-level";

describe("Check unifying of nested levels", () => {
test("Check color nesting", () => {
const obj = {
"navds-global-color-green-50": { value: "rbga(0,1,0,0)" },
"navds-global-color-green-500": { value: "rbga(0,2,0,0)" },
"navds-semantic-color-primary-hover": {
value: "rbga(0,3,0,0)",
},
"navds-semantic-color-primary-hover-subtle": {
value: "rbga(0,4,0,0)",
},
"navds-semantic-color-danger": { value: "rbga(0,5,0,0)" },
"navds-semantic-color-danger-hover": {
value: "rbga(0,6,0,0)",
},
"navds-semantic-color-danger-hover-subtle": {
value: "rbga(0,7,0,0)",
},
};

const newObj = unifyNestedLevel(obj);

expect(newObj).toEqual({
"navds-global-color-green-50": { value: "rbga(0,1,0,0)" },
"navds-global-color-green-500": { value: "rbga(0,2,0,0)" },
"navds-semantic-color-primary-hover-@": {
value: "rbga(0,3,0,0)",
},
"navds-semantic-color-primary-hover-subtle": {
value: "rbga(0,4,0,0)",
},
"navds-semantic-color-danger-@-@": { value: "rbga(0,5,0,0)" },
"navds-semantic-color-danger-hover-@": {
value: "rbga(0,6,0,0)",
},
"navds-semantic-color-danger-hover-subtle": {
value: "rbga(0,7,0,0)",
},
});
});
});
22 changes: 22 additions & 0 deletions @navikt/core/tokens/figma/unify-nested-level.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const unifyNestedLevel = (obj: { [key: string]: { value: string } }) => {
// deep copy
const workObj = JSON.parse(JSON.stringify(obj));

Object.keys(workObj).forEach((k) => {
const deeperKeys = Object.keys(workObj).filter(
(k2) => k2.startsWith(`${k}-`) && k2.length > k.length
);
if (deeperKeys.length > 0) {
const neededNestedLevel =
Math.max(...deeperKeys.map((k) => k.split("-").length)) -
k.split("-").length;

delete Object.assign(workObj, {
[k + "-@".repeat(neededNestedLevel)]: workObj[k],
})[k];
}
});
return workObj;
};

export default unifyNestedLevel;

0 comments on commit 9338995

Please sign in to comment.