Skip to content

Commit

Permalink
feat: expose flattenTokens utility
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema committed Jan 11, 2024
1 parent 0f5b1da commit 3aaae33
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fifty-hats-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'style-dictionary': patch
---

Expose flattenTokens utility.
41 changes: 41 additions & 0 deletions docs/using_reference_utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,47 @@ $ npm install -D style-dictionary

## Usage

### Flatten Tokens

Flatten dictionary tokens object to an array of flattened tokens.

```javascript
import StyleDictionary from 'style-dictionary';
import { flattenTokens } from 'style-dictionary/utils';

const sd = new StyleDictionary({
tokens: {
colors: {
black: {
value: '#000',
type: 'color',
name: 'colors-black',
},
},
spacing: {
2: {
value: '2px',
type: 'dimension',
name: 'spacing-2',
},
},
border: {
value: 'solid {spacing.2} {colors.black}',
name: 'border',
},
},
});

const flat = flattenTokens(sd, sd.tokens.border.value);
/**
* [
* { value: '#000', type: 'color', name: 'colors-black' },
* { value: '2px', type: 'dimension', name: 'spacing-2' },
* { value: 'solid {spacing.2} {colors.black}', name: 'border' }
* ]
*/
```

### usesReference

Whether or not a token value contains references
Expand Down
3 changes: 2 additions & 1 deletion lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import usesReferences from './references/usesReferences.js';
import { getReferences } from './references/getReferences.js';
import { resolveReferences } from './references/resolveReferences.js';
import flattenTokens from './flattenTokens.js';

// Public style-dictionary/utils API
export { usesReferences, getReferences, resolveReferences };
export { usesReferences, getReferences, resolveReferences, flattenTokens };

0 comments on commit 3aaae33

Please sign in to comment.