Skip to content

Commit

Permalink
Move functions
Browse files Browse the repository at this point in the history
  • Loading branch information
origami-z committed Jan 11, 2024
1 parent 05782e2 commit 0bb42e5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 58 deletions.
58 changes: 0 additions & 58 deletions packages/export-variables-rest-api/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,64 +109,6 @@ function processData(data) {
buildUsingStyleDictionary();
}

/**
* StyleDictionary doesn't support tokens nested within another which has value ($value in our case),
* e.g.
* ```
* color: {
* white: {
* $value: '#fff',
* alpha: {
* $value: 'rgba(1,1,1,0.8)',
* },
* },
* }
* ```
* So we will add `default` to the token structure
* ```
* color: {
* white: {
* default: {
* $value: "#fff",
* },
* alpha: {
* $value: 'rgba(1,1,1,0.8)',
* },
* },
* }
* ```
* See more at https://github.com/amzn/style-dictionary/issues/643#issuecomment-857105609
*/
function addDefaultToNestedTokens(tokens) {
const newTokens = {};

const allKeys = Object.keys(tokens);
const nonDollarKeys = allKeys.filter((k) => !k.startsWith("$"));
if (tokens.$value !== undefined) {
// Any property with $value and other nested names, create a default object hosting all keys with $ prefix
if (nonDollarKeys.length > 0) {
const defaultToken = {};
for (const key of allKeys.filter((k) => k.startsWith("$"))) {
defaultToken[key] = tokens[key];
}
newTokens.default = defaultToken;
} else {
for (const key of allKeys.filter((k) => k.startsWith("$"))) {
newTokens[key] = tokens[key];
}
}
}

// copy over all nested tokens (i.e. not $)
for (const key of nonDollarKeys) {
const value = tokens[key];
newTokens[key] =
typeof value === "object" ? addDefaultToNestedTokens(value) : value;
}

return newTokens;
}

function writeTokensToFile(data, tokens) {
const allCollections = data.variableCollections;

Expand Down
58 changes: 58 additions & 0 deletions packages/export-variables-rest-api/src/modifyData.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,61 @@ export function updateApiResponse(data) {
// return new object
return newData;
}

/**
* StyleDictionary doesn't support tokens nested within another which has value ($value in our case),
* e.g.
* ```
* color: {
* white: {
* $value: '#fff',
* alpha: {
* $value: 'rgba(1,1,1,0.8)',
* },
* },
* }
* ```
* So we will add `default` to the token structure
* ```
* color: {
* white: {
* default: {
* $value: "#fff",
* },
* alpha: {
* $value: 'rgba(1,1,1,0.8)',
* },
* },
* }
* ```
* See more at https://github.com/amzn/style-dictionary/issues/643#issuecomment-857105609
*/
export function addDefaultToNestedTokens(tokens) {
const newTokens = {};

const allKeys = Object.keys(tokens);
const nonDollarKeys = allKeys.filter((k) => !k.startsWith("$"));
if (tokens.$value !== undefined) {
// Any property with $value and other nested names, create a default object hosting all keys with $ prefix
if (nonDollarKeys.length > 0) {
const defaultToken = {};
for (const key of allKeys.filter((k) => k.startsWith("$"))) {
defaultToken[key] = tokens[key];
}
newTokens.default = defaultToken;
} else {
for (const key of allKeys.filter((k) => k.startsWith("$"))) {
newTokens[key] = tokens[key];
}
}
}

// copy over all nested tokens (i.e. not $)
for (const key of nonDollarKeys) {
const value = tokens[key];
newTokens[key] =
typeof value === "object" ? addDefaultToNestedTokens(value) : value;
}

return newTokens;
}

0 comments on commit 0bb42e5

Please sign in to comment.