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

feat: add stripMeta util, use in json format #1378

Merged
merged 3 commits into from
Nov 11, 2024
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/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