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(json/original): Add json/original format #737

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [3.1.0](https://github.com/amzn/style-dictionary/compare/v3.0.3...v3.1.0) (2021-11-21)


### Features

* dictionary filtering and json/original format ([c14c437](https://github.com/amzn/style-dictionary/commit/c14c437de648c00a010cf71627f813d4177a24dd))
* **formats:** Add `outputReferences` support to `scss/map-deep` ([#720](https://github.com/amzn/style-dictionary/issues/720)) ([65453e0](https://github.com/amzn/style-dictionary/commit/65453e0d980273aeeb1a201a1996c4ad3a5719a5)), closes [#712](https://github.com/amzn/style-dictionary/issues/712)
* **formats:** add support for Microsoft's JSONC format ([#732](https://github.com/amzn/style-dictionary/issues/732)) ([cfa83cb](https://github.com/amzn/style-dictionary/commit/cfa83cb65b7ec7b3e5a9def7f0ceb6ac3b2898df)), closes [#698](https://github.com/amzn/style-dictionary/issues/698)
* **formats:** object handling for typescript/es6-declarations ([#718](https://github.com/amzn/style-dictionary/issues/718)) ([4e3905a](https://github.com/amzn/style-dictionary/commit/4e3905a7a3525f872615adaf0cae0b5553309bb6))


### Bug Fixes

* **types:** adding registerFileHeader type ([#722](https://github.com/amzn/style-dictionary/issues/722)) ([54332b3](https://github.com/amzn/style-dictionary/commit/54332b3e4ee77a0b0e8f84836413949f8e64ccd5)), closes [#665](https://github.com/amzn/style-dictionary/issues/665)
* **types:** fixing transform group types ([#729](https://github.com/amzn/style-dictionary/issues/729)) ([ad7f6ea](https://github.com/amzn/style-dictionary/commit/ad7f6ea555ec77defd264c9ade9628aefd108959))

theKosh marked this conversation as resolved.
Show resolved Hide resolved
### [3.0.3](https://github.com/amzn/style-dictionary/compare/v3.0.2...v3.0.3) (2021-10-15)


Expand Down
237 changes: 237 additions & 0 deletions __tests__/common/formatHelpers/filterDictionary.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
/*
theKosh marked this conversation as resolved.
Show resolved Hide resolved
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

const filterDictionary = require('../../../lib/common/formatHelpers/filterDictionary');

const simpleData = {
empty: {},
noTarget: {
color: {
tertiary: "#222",
},
},
rootLevel: {
primary: {
color: 'darkblue',
},
color: {
tertiary: "#222",
},
},
firstLevel: {
primary: {
color: 'darkblue',
},
color: {
primary: "#333",
secondary: "#222",
tertiary: "#111",
},
},
fourthLevel: {
gradient: {
border: {
radius: {
primary: "30px",
secondary: "40px",
},
},
},
motion: {
border: {
radius: {
tertiary: '50px',
accent: '60px',
},
}
},
},
};

const simpleDataResultForFilter = {
target: 'primary',
targetArray: ['primary', 'secondary'],
rootLevelResult: {
primary: {
color: 'darkblue',
},
},
firstLevelResult: {
primary: {
color: 'darkblue',
},
color: {
primary: "#333",
secondary: "#222",
},
},
firstLevelResultWithRoot: {
color: {
primary: "#333",
},
},
fourthLevelResult: {
gradient: {
border: {
radius: {
primary: "30px",
secondary: "40px",
},
},
},
}
};

const complexData = {
validJSONTokens: {
"color": {
"common": {
"primary": {
"description": null,
"type": "color",
"value": "#2196F3",
"filePath": "tokens/design-tokens.json",
"isSource": true,
"original": {
"description": null,
"type": "color",
"value": "#2196F3ff"
},
"name": "primary",
"attributes": {},
"path": [
"color",
"common",
"primary"
]
},
"secondary": {
"description": null,
"type": "color",
"value": "#3F51B5",
"filePath": "tokens/design-tokens.json",
"isSource": true,
"original": {
"description": null,
"type": "color",
"value": "#3F51B5ff"
},
"name": "secondary",
"attributes": {},
"path": [
"color",
"common",
"secondary"
]
},
"tertiary": {
"description": null,
"type": "color",
"value": "#673AB7",
"filePath": "tokens/design-tokens.json",
"isSource": true,
"original": {
"description": null,
"type": "color",
"value": "#673AB7ff"
},
"name": "tertiary",
"attributes": {},
"path": [
"color",
"common",
"tertiary"
]
}
}
}
},
result: {
"color": {
"common": {
"primary": {
"description": null,
"type": "color",
"value": "#2196F3",
},
"secondary": {
"description": null,
"type": "color",
"value": "#3F51B5",
},
"tertiary": {
"description": null,
"type": "color",
"value": "#673AB7",
}
}
}
},
targets: [
"description",
"type",
"value",
],
};

describe('common', function () {
describe('formatHelpers', function () {
it('should return empty object when it gets null object', function () {
const result = filterDictionary(null, null, simpleData.target);
const expectVal = {};
expect(result).toEqual(expectVal);
});

it('should return source object when there is no target', function () {
const result = filterDictionary(simpleData.root);
const expectVal = simpleData.root;
expect(result).toEqual(expectVal);
});

it('should return empty object when it gets empty object', function () {
const result = filterDictionary(simpleData.empty, null, simpleDataResultForFilter.target);
const expectVal = {};
expect(result).toEqual(expectVal);
});

it('should return empty object when nothing is found', function () {
const result = filterDictionary(simpleData.noTarget, 'original', simpleDataResultForFilter.target);
const expectVal = {};
expect(result).toEqual(expectVal);
});

it('should return valid json when it gets target on root level', function () {
const result = filterDictionary(simpleData.rootLevel, 'original', simpleDataResultForFilter.target);
const expectVal = simpleDataResultForFilter.rootLevelResult;
expect(result).toEqual(expectVal);
});

it('should return valid json when it gets target on first level', function () {
const result = filterDictionary(simpleData.firstLevel, 'original', ...simpleDataResultForFilter.targetArray);
const expectVal = simpleDataResultForFilter.firstLevelResult;
expect(result).toEqual(expectVal);
});

it('should return valid json when it gets target on fourth level', function () {
const result = filterDictionary(simpleData.fourthLevel, 'original', simpleDataResultForFilter.targetArray);
const expectVal = simpleDataResultForFilter.fourthLevelResult;
expect(result).toEqual(expectVal);
});

it('should return valid json when it gets complex json', function () {
const result = filterDictionary(complexData.validJSONTokens, 'original', complexData.targets);
const expectVal = complexData.result;
expect(result).toEqual(expectVal);
});
});
});
10 changes: 10 additions & 0 deletions __tests__/formats/__snapshots__/all.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,16 @@ exports[`formats all should match json/nested snapshot 1`] = `
}"
`;

exports[`formats all should match json/original snapshot 1`] = `
"{
\\"color\\": {
\\"red\\": {
\\"value\\": \\"#FF0000\\"
}
}
}"
`;

theKosh marked this conversation as resolved.
Show resolved Hide resolved
exports[`formats all should match less/icons snapshot 1`] = `
"
// Do not edit directly
Expand Down
106 changes: 106 additions & 0 deletions __tests__/formats/jsonOriginal.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
theKosh marked this conversation as resolved.
Show resolved Hide resolved
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/

const formats = require('../../lib/common/formats');
const fs = require('fs-extra');
const helpers = require('../__helpers');
const createDictionary = require('../../lib/utils/createDictionary');
const createFormatArgs = require('../../lib/utils/createFormatArgs');

const file = {
destination: 'output/',
format: 'json/original',
};

const properties = {
color: {
base: {
red: {
primary: {
value: '#611D1D',
type: 'color',
desciption: null,
isSource: true,
name: "primary"
},
secondary: {
value: '#611D1C',
type: 'color',
desciption: null,
isSource: true,
original: {
value: '#611D1C',
type: 'color',
desciption: null
},
name: "secondary"
},
tertiary: {
inverse: {
value: '#000000'
},
},
},
},
},
};

const expected = {
color: {
base: {
red: {
primary: {
value: '#611D1D',
type: 'color',
desciption: null,
},
secondary: {
value: '#611D1C',
type: 'color',
desciption: null,
},
tertiary: {
inverse: {
value: '#000000'
},
},
},
},
},
}

const formatter = formats['json/original'].bind(file);
const dictionary = createDictionary({ properties });

describe('formats', function () {
describe('json/original', function () {
beforeEach(() => {
helpers.clearOutput();
});

afterEach(() => {
helpers.clearOutput();
});

it('should be a valid JSON file', function () {
fs.writeFileSync('./__tests__/__output/json-original.json', formatter(createFormatArgs({
dictionary,
file,
platform: {}
}), {}, file));
const test = require('../__output/json-original.json');
expect(test)
.toEqual(expected);
});
});
});
Loading