Skip to content

Commit

Permalink
Merge pull request #506 from department-of-veterans-affairs/cu/482-48…
Browse files Browse the repository at this point in the history
…6-sort-and-add-comments-to-tokens

[CU] Alphabetize and add value comments to token declarations
  • Loading branch information
narin authored Sep 30, 2024
2 parents 6f7a2a8 + c68d278 commit 1c72186
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
24 changes: 19 additions & 5 deletions packages/tokens/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const StyleDictionary = require('style-dictionary')
* Utils
*/

/** Reterns a new tokens object sorted alphabetically by key */
/** Returns a new tokens object sorted alphabetically by key */
const sortTokensByKey = (obj) => {
const sortedKeys = Object.keys(obj).sort()
const sortedObj = {}
Expand All @@ -16,6 +16,12 @@ const sortTokensByKey = (obj) => {
return sortedObj
}

/** Returns a new tokens array sorted alphabetically by token name */
const sortTokensByName = (tokens) =>
tokens.sort((a, b) => {
return a.name.localeCompare(b.name)
})

/** Reusable filter function that returns only non-dark-mode tokens */
const filterLight = (token) =>
token.attributes.category.includes('color') &&
Expand Down Expand Up @@ -120,8 +126,10 @@ StyleDictionary.registerFormat({
StyleDictionary.registerFormat({
name: 'typescript/es6-declarations/colors',
formatter: function (dictionary) {
const sortedTokens = sortTokensByName(dictionary.allTokens)
let declaration = 'export declare const colors: {\n'
dictionary.allProperties.forEach((token) => {
sortedTokens.forEach((token) => {
declaration += ` /** ${token.value} */\n`
declaration += ` ${token.name}: string;\n`
})
declaration += '}'
Expand All @@ -134,20 +142,26 @@ StyleDictionary.registerFormat({
name: 'typescript/es6-declarations/spacing',
formatter: function (dictionary) {
let declaration = 'export declare const spacing: {\n'
dictionary.allProperties.forEach((token) => {
let allValuesComment = '/**\n * '
dictionary.allProperties.forEach((token, index) => {
declaration += ` /** Value: ${token.value} */\n`
declaration += ` ${token.name}: number;\n`
allValuesComment += `${index !== 0 ? ' | ' : ''}${token.name.replace('vadsSpace', '')}: ${token.value}`
})
declaration += '}'
return declaration
allValuesComment += '\n */\n'
return allValuesComment + declaration
},
})

/** Creates named type declaration for Themes. Allows for TypeScript autocomplete */
StyleDictionary.registerFormat({
name: 'typescript/es6-declarations/theme',
formatter: function (dictionary) {
const sortedTokens = sortTokensByName(dictionary.allTokens)
let declaration = 'export declare type Theme = {\n'
dictionary.allProperties.forEach((token) => {
sortedTokens.forEach((token) => {
declaration += ` /** ${token.value} */\n`
declaration += ` ${stripMode(token.name)}: string;\n`
})
declaration += '}\n\n'
Expand Down
2 changes: 1 addition & 1 deletion packages/tokens/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@department-of-veterans-affairs/mobile-tokens",
"version": "0.17.0",
"version": "0.17.1-alpha.4",
"description": "VA Design System Mobile Token Library",
"main": "dist/js/index.js",
"types": "dist/index.d.ts",
Expand Down

0 comments on commit 1c72186

Please sign in to comment.