Skip to content

Commit

Permalink
Merge pull request #1378 from florin01hyma/strip-meta
Browse files Browse the repository at this point in the history
feat: add stripMeta util, use in json format
  • Loading branch information
jorenbroekema authored Nov 11, 2024
2 parents 0fcf229 + 4bf68a3 commit 99f687e
Show file tree
Hide file tree
Showing 27 changed files with 1,203 additions and 266 deletions.
5 changes: 5 additions & 0 deletions .changeset/shaggy-pigs-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'style-dictionary': minor
---

Mark javascript/esm as nested, use Prettier on all JavaScript/TypeScript formats, use 3.x.x peerDependency so the user's installation is used when possible.
5 changes: 5 additions & 0 deletions .changeset/violet-goats-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'style-dictionary': minor
---

Apply stripMeta from "json" format to the new "javascript/esm" as well.
7 changes: 7 additions & 0 deletions .changeset/warm-teachers-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'style-dictionary': minor
---

Add new utility in `style-dictionary/utils` -> `stripMeta` for stripping metadata from tokens.
This utility is used now as an opt-in for the built-in `'json'` format by using `options.stripMeta`, which if set to `true` will strip Style Dictionary meta props.
You can specify `keep`/`strip` (allow/blocklist) for granular control about which properties to keep or strip.
81 changes: 36 additions & 45 deletions __integration__/__snapshots__/customFileHeader.test.snap.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,46 +43,40 @@ snapshots["integration valid custom file headers platform options no file option
*/
module.exports = {
"color": {
"red": {
"value": "#ff0000",
"original": {
"value": "#ff0000"
color: {
red: {
value: "#ff0000",
original: {
value: "#ff0000",
},
"name": "ColorRed",
"attributes": {
"category": "color",
"type": "red"
name: "ColorRed",
attributes: {
category: "color",
type: "red",
},
"path": [
"color",
"red"
]
}
}
path: ["color", "red"],
},
},
};
`;
/* end snapshot integration valid custom file headers platform options no file options should match snapshot */

snapshots["integration valid custom file headers platform options showFileHeader should match snapshot"] =
`module.exports = {
"color": {
"red": {
"value": "#ff0000",
"original": {
"value": "#ff0000"
color: {
red: {
value: "#ff0000",
original: {
value: "#ff0000",
},
"name": "ColorRed",
"attributes": {
"category": "color",
"type": "red"
name: "ColorRed",
attributes: {
category: "color",
type: "red",
},
"path": [
"color",
"red"
]
}
}
path: ["color", "red"],
},
},
};
`;
/* end snapshot integration valid custom file headers platform options showFileHeader should match snapshot */
Expand All @@ -93,23 +87,20 @@ snapshots["integration valid custom file headers platform options file header ov
*/
module.exports = {
"color": {
"red": {
"value": "#ff0000",
"original": {
"value": "#ff0000"
color: {
red: {
value: "#ff0000",
original: {
value: "#ff0000",
},
"name": "ColorRed",
"attributes": {
"category": "color",
"type": "red"
name: "ColorRed",
attributes: {
category: "color",
type: "red",
},
"path": [
"color",
"red"
]
}
}
path: ["color", "red"],
},
},
};
`;
/* end snapshot integration valid custom file headers platform options file header override should match snapshot */
Expand Down
21 changes: 5 additions & 16 deletions __tests__/StyleDictionary.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { fileToJSON, clearOutput, fileExists, clearSDMeta } from './__helpers.js
import { resolve } from '../lib/resolve.js';
import GroupMessages from '../lib/utils/groupMessages.js';
import flattenTokens from '../lib/utils/flattenTokens.js';
import { stripMeta } from '../lib/utils/stripMeta.js';
import formats from '../lib/common/formats.js';
import { restore, stubMethod } from 'hanbi';

Expand All @@ -30,18 +31,6 @@ function traverseObj(obj, fn) {
}
}

function stripMeta(obj) {
Object.keys(obj).forEach((key) => {
if (['attributes', 'name', 'original', 'path'].includes(key)) {
delete obj[key];
}
if (typeof obj[key] === 'object') {
stripMeta(obj[key]);
}
});
return obj;
}

const test_props = {
size: {
padding: {
Expand Down Expand Up @@ -570,7 +559,7 @@ Refer to: https://styledictionary.com/reference/logging/
await sd.hasInitialized;
const cssTokens = await sd.exportPlatform('css');
const jsTokens = await sd.exportPlatform('js');
expect(stripMeta(cssTokens)).to.eql({
expect(stripMeta(cssTokens, { keep: ['type', 'value'] })).to.eql({
border: {
color: {
type: 'color',
Expand All @@ -586,7 +575,7 @@ Refer to: https://styledictionary.com/reference/logging/
},
},
});
expect(stripMeta(jsTokens)).to.eql(input);
expect(stripMeta(jsTokens, { keep: ['type', 'value'] })).to.eql(input);
});

it('should allow combining global expand with per platform expand', async () => {
Expand Down Expand Up @@ -628,7 +617,7 @@ Refer to: https://styledictionary.com/reference/logging/
const cssTokens = await sd.exportPlatform('css');
const jsTokens = await sd.exportPlatform('js');

expect(stripMeta(cssTokens)).to.eql({
expect(stripMeta(cssTokens, { keep: ['type', 'value'] })).to.eql({
border: {
color: {
type: 'color',
Expand All @@ -645,7 +634,7 @@ Refer to: https://styledictionary.com/reference/logging/
},
borderTwo: input.borderTwo,
});
expect(stripMeta(jsTokens)).to.eql({
expect(stripMeta(jsTokens, { keep: ['type', 'value'] })).to.eql({
border: {
color: {
type: 'color',
Expand Down
Loading

0 comments on commit 99f687e

Please sign in to comment.