@cobalt-ui/core
Advanced tools
Comparing version 1.10.0 to 1.10.1
# @cobalt-ui/core | ||
## 1.10.1 | ||
### Patch Changes | ||
- [#233](https://github.com/drwpow/cobalt-ui/pull/233) [`54bd5a31f01dd97dceb3808db7f3ff93d4342166`](https://github.com/drwpow/cobalt-ui/commit/54bd5a31f01dd97dceb3808db7f3ff93d4342166) Thanks [@drwpow](https://github.com/drwpow)! - Tiny parser performance bump | ||
## 1.10.0 | ||
@@ -4,0 +10,0 @@ |
@@ -33,3 +33,4 @@ import { formatHex, formatHex8, oklch } from 'culori'; | ||
// 1. build shallow token manifest of IDs -> Tokens (aliases will depend on IDs) | ||
for (const [id, variable] of Object.entries(manifest.variables)) { | ||
for (const id in manifest.variables) { | ||
const variable = manifest.variables[id]; | ||
// find best override, if any | ||
@@ -71,3 +72,4 @@ let override = undefined; | ||
const dtcgFlat = {}; | ||
for (const [id, token] of Object.entries(tokens)) { | ||
for (const id in tokens) { | ||
const token = tokens[id]; | ||
dtcgFlat[tokenIDToNameMap[id]] = token; | ||
@@ -79,3 +81,4 @@ // resolve Figma aliases to DTCG aliases | ||
if (token.$extensions?.mode) { | ||
for (const [k, v] of Object.entries(token.$extensions?.mode)) { | ||
for (const k in token.$extensions?.mode) { | ||
const v = token.$extensions.mode[k]; | ||
if (typeof v === 'object' && 'type' in v && v.type === 'VARIABLE_ALIAS') { | ||
@@ -88,3 +91,3 @@ token.$extensions.mode[k] = `{${tokenIDToNameMap[v.id]}#${k}}`; | ||
// 3. explode flat structure into nested structure | ||
for (const [id, token] of Object.entries(dtcgFlat)) { | ||
for (const id in dtcgFlat) { | ||
const parts = id.split('.'); | ||
@@ -99,3 +102,3 @@ let node = dtcgTokens; | ||
} | ||
node[localName] = token; | ||
node[localName] = dtcgFlat[id]; | ||
} | ||
@@ -122,3 +125,4 @@ return { | ||
token.$type = $type; | ||
for (const [modeId, rawValue] of Object.entries(variable.valuesByMode)) { | ||
for (const modeId in variable.valuesByMode) { | ||
const rawValue = variable.valuesByMode[modeId]; | ||
const isDefaultMode = modeId === collection.defaultModeId; | ||
@@ -125,0 +129,0 @@ const isMultiModal = Object.values(variable.valuesByMode).length > 1; |
@@ -73,3 +73,4 @@ import { cloneDeep, FG_YELLOW, getAliasID, invalidTokenIDError, isAlias, RESET } from '@cobalt-ui/utils'; | ||
} | ||
for (const [k, v] of Object.entries(node)) { | ||
for (const k in node) { | ||
const v = node[k]; | ||
if (!v || !isObj(v)) { | ||
@@ -92,2 +93,3 @@ errors.push(`${k}: unexpected token format "${v}"`); | ||
// token | ||
const { $type = group.$type, ...tokenMetadata } = v; | ||
const token = { | ||
@@ -100,4 +102,4 @@ _original: cloneDeep(v), | ||
id: chain.concat(k).join('.'), | ||
$type: v.$type || group.$type, | ||
...v, | ||
$type, | ||
...tokenMetadata, | ||
}; | ||
@@ -130,3 +132,3 @@ const isToken = '$value' in token; // token MUST have $value, per the sepc | ||
const groupTokens = {}; | ||
for (const propertyKey of Object.keys(v)) { | ||
for (const propertyKey in v) { | ||
// move all "$" properties to group | ||
@@ -166,3 +168,3 @@ if (propertyKey.startsWith('$')) { | ||
const topNodes = {}; | ||
for (const k of Object.keys(schema)) { | ||
for (const k in schema) { | ||
if (k.startsWith('$')) { | ||
@@ -195,7 +197,8 @@ if (k === '$extensions') { | ||
// 2a. pass 1: gather all IDs & values | ||
for (const token of Object.values(tokens)) { | ||
for (const id in tokens) { | ||
const token = tokens[id]; | ||
values[token.id] = token.$value; | ||
if (token.$extensions && token.$extensions.mode) { | ||
for (const [k, v] of Object.entries(token.$extensions.mode || {})) { | ||
values[`${token.id}#${k}`] = v; | ||
for (const k in token.$extensions.mode || {}) { | ||
values[`${token.id}#${k}`] = token.$extensions.mode[k]; | ||
} | ||
@@ -229,3 +232,3 @@ } | ||
object(objVal) { | ||
for (const prop of Object.keys(objVal)) { | ||
for (const prop in objVal) { | ||
objVal[prop] = resolveAliases(id, objVal[prop]); | ||
@@ -239,4 +242,4 @@ } | ||
try { | ||
for (const [id, value] of Object.entries(values)) { | ||
values[id] = resolveAliases(id, value); | ||
for (const id in values) { | ||
values[id] = resolveAliases(id, values[id]); | ||
} | ||
@@ -259,7 +262,8 @@ } | ||
} | ||
for (const k of Object.keys(token.$extensions.mode || {})) { | ||
for (const k in token.$extensions.mode || {}) { | ||
tokens[id].$extensions.mode[k] = validate(values[`${id}#${k}`]); | ||
} | ||
} | ||
for (const [id, token] of Object.entries(tokens)) { | ||
for (const id in tokens) { | ||
const token = tokens[id]; | ||
try { | ||
@@ -266,0 +270,0 @@ switch (token.$type) { |
@@ -30,3 +30,4 @@ import { camelize } from '@cobalt-ui/utils'; | ||
const normalized = {}; | ||
for (const [k, v] of Object.entries(value)) { | ||
for (const k in value) { | ||
const v = value[k]; | ||
const property = camelize(k); | ||
@@ -33,0 +34,0 @@ switch (property) { |
{ | ||
"name": "@cobalt-ui/core", | ||
"description": "Parser/validator for the Design Tokens Community Group (DTCG) standard.", | ||
"version": "1.10.0", | ||
"version": "1.10.1", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Drew Powers", |
@@ -151,3 +151,4 @@ import { formatHex, formatHex8, oklch } from 'culori'; | ||
// 1. build shallow token manifest of IDs -> Tokens (aliases will depend on IDs) | ||
for (const [id, variable] of Object.entries(manifest.variables)) { | ||
for (const id in manifest.variables) { | ||
const variable = manifest.variables[id]!; | ||
// find best override, if any | ||
@@ -193,3 +194,4 @@ let override: FigmaOverride | undefined = undefined; | ||
const dtcgFlat: Record<string, Token> = {}; | ||
for (const [id, token] of Object.entries(tokens)) { | ||
for (const id in tokens) { | ||
const token = tokens[id]!; | ||
dtcgFlat[tokenIDToNameMap[id]!] = token; | ||
@@ -202,3 +204,4 @@ | ||
if (token.$extensions?.mode) { | ||
for (const [k, v] of Object.entries(token.$extensions?.mode)) { | ||
for (const k in token.$extensions?.mode) { | ||
const v = token.$extensions.mode[k]; | ||
if (typeof v === 'object' && 'type' in v && v.type === 'VARIABLE_ALIAS') { | ||
@@ -212,3 +215,3 @@ token.$extensions.mode[k] = `{${tokenIDToNameMap[(v as VariableAlias).id]}#${k}}`; | ||
// 3. explode flat structure into nested structure | ||
for (const [id, token] of Object.entries(dtcgFlat)) { | ||
for (const id in dtcgFlat) { | ||
const parts = id.split('.'); | ||
@@ -223,3 +226,3 @@ let node = dtcgTokens; | ||
} | ||
node[localName] = token; | ||
node[localName] = dtcgFlat[id]!; | ||
} | ||
@@ -252,3 +255,4 @@ | ||
token.$type = $type; | ||
for (const [modeId, rawValue] of Object.entries(variable.valuesByMode)) { | ||
for (const modeId in variable.valuesByMode) { | ||
const rawValue = variable.valuesByMode[modeId]!; | ||
const isDefaultMode = modeId === collection.defaultModeId; | ||
@@ -271,6 +275,6 @@ const isMultiModal = Object.values(variable.valuesByMode).length > 1; | ||
if (isDefaultMode) { | ||
token.$value = rawValue; | ||
token.$value = rawValue as typeof token.$value; | ||
} | ||
if (isMultiModal) { | ||
token.$extensions!.mode![collectionMode.name] = rawValue; | ||
token.$extensions!.mode![collectionMode.name] = rawValue as typeof token.$value; | ||
} | ||
@@ -302,3 +306,3 @@ continue; | ||
default: { | ||
transformedValue = rawValue; | ||
transformedValue = rawValue as string; | ||
break; | ||
@@ -305,0 +309,0 @@ } |
import { cloneDeep, FG_YELLOW, getAliasID, invalidTokenIDError, isAlias, RESET } from '@cobalt-ui/utils'; | ||
import parseJSON from 'parse-json'; | ||
import yaml from 'yaml'; | ||
import type { Group, ParsedToken, TokenType, TokenOrGroup } from '../token.js'; | ||
import type { Group, ParsedToken, TokenType, TokenOrGroup, Token } from '../token.js'; | ||
import { isEmpty, isJSON, isObj, splitType } from '../util.js'; | ||
@@ -114,3 +114,4 @@ import { normalizeBorderValue } from './tokens/border.js'; | ||
} | ||
for (const [k, v] of Object.entries(node)) { | ||
for (const k in node) { | ||
const v = node[k as keyof typeof node]; | ||
if (!v || !isObj(v)) { | ||
@@ -134,2 +135,3 @@ errors.push(`${k}: unexpected token format "${v}"`); | ||
// token | ||
const { $type = group.$type, ...tokenMetadata } = v as Token; | ||
const token = { | ||
@@ -142,4 +144,4 @@ _original: cloneDeep(v), | ||
id: chain.concat(k).join('.'), | ||
$type: v.$type || group.$type, | ||
...v, | ||
$type, | ||
...tokenMetadata, | ||
} as ParsedToken; | ||
@@ -173,3 +175,3 @@ const isToken = '$value' in token; // token MUST have $value, per the sepc | ||
const groupTokens: Record<string, TokenOrGroup> = {}; | ||
for (const propertyKey of Object.keys(v)) { | ||
for (const propertyKey in v as Record<string, unknown>) { | ||
// move all "$" properties to group | ||
@@ -179,5 +181,5 @@ if (propertyKey.startsWith('$')) { | ||
if (propertyKey === '$extensions') { | ||
nextGroup.$extensions = { ...nextGroup.$extensions, ...v.$extensions }; | ||
nextGroup.$extensions = { ...nextGroup.$extensions, ...(v as Group).$extensions }; | ||
} else { | ||
(nextGroup as any)[propertyKey] = v[propertyKey]; | ||
(nextGroup as any)[propertyKey] = v[propertyKey as keyof typeof v]; | ||
} | ||
@@ -193,3 +195,3 @@ if (!RESERVED_KEYS.has(propertyKey)) { | ||
else { | ||
groupTokens[propertyKey] = v[propertyKey]; | ||
groupTokens[propertyKey] = v[propertyKey as keyof typeof v]; | ||
} | ||
@@ -212,3 +214,3 @@ } | ||
const topNodes: Record<string, TokenOrGroup> = {}; | ||
for (const k of Object.keys(schema)) { | ||
for (const k in schema) { | ||
if (k.startsWith('$')) { | ||
@@ -241,7 +243,8 @@ if (k === '$extensions') { | ||
// 2a. pass 1: gather all IDs & values | ||
for (const token of Object.values(tokens)) { | ||
for (const id in tokens) { | ||
const token = tokens[id]!; | ||
values[token.id] = token.$value; | ||
if (token.$extensions && token.$extensions.mode) { | ||
for (const [k, v] of Object.entries(token.$extensions.mode || {})) { | ||
values[`${token.id}#${k}`] = v; | ||
for (const k in token.$extensions.mode || {}) { | ||
values[`${token.id}#${k}`] = token.$extensions.mode[k]; | ||
} | ||
@@ -278,3 +281,3 @@ } | ||
object(objVal) { | ||
for (const prop of Object.keys(objVal as Record<string, unknown>)) { | ||
for (const prop in objVal as Record<string, unknown>) { | ||
objVal[prop] = resolveAliases(id, objVal[prop]); | ||
@@ -288,4 +291,4 @@ } | ||
try { | ||
for (const [id, value] of Object.entries(values)) { | ||
values[id] = resolveAliases(id, value); | ||
for (const id in values) { | ||
values[id] = resolveAliases(id, values[id]); | ||
} | ||
@@ -308,3 +311,3 @@ } catch (err: any) { | ||
} | ||
for (const k of Object.keys(token.$extensions.mode || {})) { | ||
for (const k in token.$extensions.mode || {}) { | ||
(tokens[id] as any).$extensions.mode[k] = validate(values[`${id}#${k}`]); | ||
@@ -314,3 +317,4 @@ } | ||
for (const [id, token] of Object.entries(tokens)) { | ||
for (const id in tokens) { | ||
const token = tokens[id]!; | ||
try { | ||
@@ -317,0 +321,0 @@ switch (token.$type) { |
@@ -33,3 +33,4 @@ import type { ParsedTypographyValue } from '../../token.js'; | ||
const normalized = {} as ParsedTypographyValue; | ||
for (const [k, v] of Object.entries(value)) { | ||
for (const k in value) { | ||
const v = value[k as keyof typeof value]!; | ||
const property = camelize(k); | ||
@@ -36,0 +37,0 @@ switch (property) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
256050
5053