-
Notifications
You must be signed in to change notification settings - Fork 566
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(dictionary) Added new filter removePrivate
#770
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e47a632
Merge pull request #1 from amzn/main
silversonicaxel 232e878
feat(dictionary): added filter removePrivate
silversonicaxel 4256681
fix(lib): remove useless import
silversonicaxel 643a5eb
docs(example): add basic documentation for filters
silversonicaxel 3e1244d
docs(example): fix require of current styledictionary lib
silversonicaxel 1e35f84
fix(example): adjust indentation
silversonicaxel e0f8f35
docs(example): add information to filters README.md
silversonicaxel de64b91
docs(example): improve basic documentation for custom filters
silversonicaxel 876a377
fix(docs): add missing comment in index.js
silversonicaxel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
var filters = require('../../lib/common/filters'); | ||
|
||
describe('common', () => { | ||
describe('filters', () => { | ||
describe('removePrivate', () => { | ||
it('should keep a regular token in for distribution', () => { | ||
var regularToken = { | ||
name: 'color-border', | ||
value: '#1a1aed' | ||
} | ||
|
||
expect(filters["removePrivate"](regularToken)).toEqual(true); | ||
}); | ||
|
||
it('should keep an unfiltered token in for distribution', () => { | ||
var unfilteredToken = { | ||
name: 'color-border', | ||
value: '#1a1aed', | ||
private: false | ||
} | ||
|
||
expect(filters["removePrivate"](unfilteredToken)).toEqual(true); | ||
}); | ||
|
||
|
||
it('should remove a filtered token from the distribution output', () => { | ||
var filteredToken = { | ||
name: 'color-border', | ||
value: '#1a1aed', | ||
private: true | ||
} | ||
|
||
expect(filters["removePrivate"](filteredToken)).toEqual(false); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
build/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
## Filters | ||
|
||
This example shows how to use built-in and custom filters to the design tokens. | ||
|
||
Filters are functions that might remove according to some conditions a design token from the output distribution. | ||
|
||
#### Running the example | ||
|
||
First of all, set up the required dependencies running the command `npm install` in your local CLI environment (if you prefer to use *yarn* update the commands accordingly). | ||
|
||
At this point, if you want to build the tokens you can run `npm run build`. This command will generate the files in the `build` folder. | ||
|
||
#### How does it work a built-in filter? | ||
|
||
Currently StyleDictionary supports just the following built-in filters: | ||
|
||
- removePrivate | ||
|
||
You have to apply it in the `config.json` file: | ||
|
||
``` | ||
"scss": { | ||
"buildPath": "build/web/", | ||
"files": [{ | ||
"destination": "colors.scss", | ||
"filter": "removePrivate", | ||
"format": "scss/variables" | ||
}] | ||
} | ||
``` | ||
|
||
The StyleDictionary will take care of filtering out proper design tokens from the source of truth: | ||
|
||
``` | ||
{ | ||
"color": { | ||
"gray": { | ||
"light" : { | ||
"value": "#CCCCCC", | ||
"group": "color", | ||
"private": true | ||
}, | ||
"medium": { | ||
"value": "#999999", | ||
"group": "color" | ||
}, | ||
"dark" : { | ||
"value": "#111111", | ||
"group": "color" | ||
} | ||
}, | ||
} | ||
} | ||
``` | ||
|
||
#### How does it work a custom filter? | ||
|
||
To declare a custom **filter**, you have to call the `registerFilter` method: | ||
|
||
``` | ||
StyleDictionary.registerFilter({ | ||
name: 'isTextTransform', | ||
matcher: function(token) { | ||
return token.attributes.category === 'font' && token.value.includes['lowercase', 'uppercase] | ||
} | ||
}); | ||
``` | ||
|
||
You have to apply it in the `config.json` file: | ||
|
||
``` | ||
"scss": { | ||
"buildPath": "build/web/", | ||
"files": [{ | ||
"destination": "fonts.scss", | ||
"filter": "isTextTransform", | ||
"format": "scss/variables" | ||
}] | ||
} | ||
``` | ||
|
||
The StyleDictionary will take care of filtering out proper design tokens from the source of truth: | ||
|
||
``` | ||
{ | ||
"fonts": { | ||
"title-transform": { | ||
"value": "uppercase", // included | ||
"group": "font" | ||
}, | ||
"title-size": { | ||
"value": "36px", // excluded | ||
"group": "font" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
More information can be found on the [documentation](https://amzn.github.io/style-dictionary/#/api?id=registerfilter). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const StyleDictionary = require('../../../index'); | ||
|
||
console.log('Build started...'); | ||
console.log('\n=============================================='); | ||
|
||
|
||
// REGISTER THE CUSTOM FILTERS | ||
|
||
StyleDictionary.registerFilter({ | ||
name: 'removeBigSpacing', | ||
matcher: function(token) { | ||
return token.group === 'spacing' && token.value < 0.5 | ||
} | ||
}); | ||
|
||
// APPLY THE CONFIGURATION | ||
// IMPORTANT: the registration of custom transforms | ||
// needs to be done _before_ applying the configuration | ||
const StyleDictionaryExtended = StyleDictionary.extend(__dirname + '/config.json'); | ||
|
||
|
||
// FINALLY, BUILD ALL THE PLATFORMS | ||
StyleDictionaryExtended.buildAllPlatforms(); | ||
|
||
|
||
console.log('\n=============================================='); | ||
console.log('\nBuild completed!'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"source": ["tokens/**/*.json"], | ||
"platforms": { | ||
"web": { | ||
"buildPath": "build/web/", | ||
"files": [{ | ||
"destination": "tokens.js", | ||
"filter": "removeBigSpacing", | ||
"format": "javascript/es6" | ||
}] | ||
}, | ||
"scss": { | ||
"buildPath": "build/web/", | ||
"files": [{ | ||
"destination": "tokens.scss", | ||
"filter": "removeBigSpacing", | ||
"format": "scss/variables" | ||
}] | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for adding unit tests!