import { type Infer } from 'superstruct'; import { memoize } from '@knapsack/utils'; import { type TokenGroupData } from '@knapsack/types'; import { type KsDesignTokenSrcGroup, type KsDesignTokenData, type KsTokenValue, type Getables, type KsDesignTokenSrc } from './types.js'; export type TokenGroup = { name: string; path: string[]; children?: TokenGroup[]; tokens?: KsDesignTokenData[]; }; /** * Tests that token strings (brand-color-blue) do not include . / \ */ export declare function validateTokenString(tokenName: string): void; /** * Tests that token strings, ie keys at {brand: {color: {blue: {value: 'blue'}}}} * do not include . \ - */ export declare function validateTokenName(tokenName: string): void; /** * Tests that token names do not include . \ */ export declare function validateGroupTokenName(groupName: string): void; /** * Generate the category and tags off a KsDesignToken or KsDesignTokenData, * according to the logic StyleDictionary uses to determines it. */ export declare const categoryAndTags: ({ path, customTags, }: { path: string[]; customTags?: string[]; }) => { category: string; tags: string[]; }; /** * Quick check to determine if a token's value references another token */ export declare const hasReference: ({ original }: KsDesignTokenData) => boolean; export declare const isValueReference: (value: KsTokenValue) => value is string; /** * Strip the { } off a value that references another value * @example * /// '{a.b.value}' -> 'a.b.value' * cleanReferenceBrackets('{a.b.value}') */ export declare const cleanReferenceBrackets: (val: KsTokenValue) => string; /** * Add brackets around a string * @example * /// 'a.b.value' -> '{a.b.value}' * addReferenceBrackets('a.b.value') */ export declare const addReferenceBrackets: (val: string) => string; /** * join the path with slashes * @example * /// ['a','b'] -> 'a/b' * joinPathWithSlashes(['a','b']) */ export declare const joinPathWithSlashes: (groupPath: string[]) => string; /** * Get the first tokens nav item * @example * getFirstDesignTokensGroupPath([{name:'a', children: {}}]) */ export declare const getFirstDesignTokensGroupPath: (groups: TokenGroupData[]) => string; /** * Turn a value into an array of KSTokenValues * @example * /// 'hi {a.b.value} hi' -> ['hi', '{a.b.value}', 'hi'] * explodeValue('hi {a.b.value} hi') */ export declare const explodeValue: (value: KsTokenValue) => KsTokenValue[]; /** * A quick check to determine if the value of token has multiple parts, * @example * /// '{a.b.value} hi {c.d.value} flerp' -> true * hasMultiValue('{a.b.value} hi {c.d.value} flerp') */ export declare const hasMultiValue: (val: KsTokenValue) => boolean; /** * A simple split on '.' to use in functional composition */ export declare const splitPath: (val: string) => string[]; /** * A simple join on '.' to use in functional composition */ export declare const joinPath: (path: string[]) => string; /** * A simple split on '-' to use in functional composition * @deprecated */ export declare const splitName: (name: string) => string[]; /** * A simple join on '-' to use in functional composition * @deprecated */ export declare const joinName: (name: string[]) => string; /** * This does the same thing as joinName, but it's purpose is more generic so * we can deprecate joinName as the defacto way names are joined. */ export declare const joinDash: (name: string[]) => string; /** * A lot of our strings have .value on the end, when we want to ignore that. */ export declare const stripDotValue: (val: string) => string; /** * .value is needed on the end of a string in various transformations */ export declare const concatDotValue: (val: string) => string; /** * A simple partial application to prefix a path array, used optionally in * naming tokens from paths */ export declare const prefixBeforePath: (prefix?: string) => (path: string[]) => string[]; /** * Tokens names may have a prefix, but require a path array. Kebab it all, just * to be sure. * @example * /// (prefix: 'nerp') ['a', 'b', 'c'] -> 'nerp-a-b-c' * makeTokenName('nerp')(['a', 'b', 'c']) * @example * /// (prefix: undefined) ['a', 'b', 'c'] -> 'a-b-c' * makeTokenName()(['a', 'b', 'c']) * * @deprecated */ export declare const makeTokenName: (prefix?: string) => (pathArray: string[]) => string; /** * Ensure everything kebabs the same way */ export declare const kebabString: (str: string) => string; /** * Kebab each segment in a path array */ export declare const kebabAll: (path: string[]) => string[]; /** * Generate name from path array, keep caps, join on '-', convert spaces to '-' */ export declare const makeTokenKebabName: (prefix?: string) => ((path: string[]) => string); /** * Take a token name like 'a-b-c' and turn it into path array. Takes into * account OPTIONAL prefix string to REMOVE prefix strings if they are * accidentally included in the name. * @example * /// (no prefix) 'a-b-c' -> ['a', 'b', 'c'] * pathFromTokenName()('a-b-c') * @example * /// (prefix: 'nerp') 'a-b-c' -> ['a', 'b', 'c'] * pathFromTokenName('nerp')('a-b-c') * @example * /// (prefix: 'nerp') 'nerp-a-b-c' -> ['a', 'b', 'c'] * pathFromTokenName('nerp')('nerp-a-b-c-') * * We no longer go from string back to path. * @deprecated */ export declare const pathFromTokenName: (prefix?: string) => ((name: string) => string[]); /** * Take a reference string and convert it to the loGet string format * @example * /// '{a.b.c.value}' -> 'a.b.c' * referenceFormatToGetable('{a.b.c.value}') */ export declare const referenceFormatToGetable: (ref: string) => string; /** * Generate the path array from a reference string * @example * /// '{a.b.c.value}' -> ['a', 'b', 'c'] * referenceFormatToPath('{a.b.c.value}) */ export declare const referenceFormatToPath: (ref: string) => string[]; /** * Generate path array from a getables key string * @example * /// 'a.b.c.value' -> ['a', 'b', 'c'] * getableToPath('a.b.c.value') */ export declare const getableToPath: (ref: string) => string[]; /** * Generate getable string from path * @example * /// ['a', 'b'] -> 'a.b.value' * pathToGetable(['a', 'b']) */ export declare const pathToGetable: (ref: string[]) => string; /** * Generate the ".value" form of a path. * @example * /// 'a-b-c' -> '{a.b.c.value}' * nameToReferenceFormat('a-b-c') */ export declare const nameToReferenceFormat: (ref: string) => string; /** * Generate the ".value" form of a path. * @example * /// ['a', 'b', 'c'] -> '{a.b.c.value}' * pathToReferenceFormat(['a', 'b', 'c']) */ export declare const pathToReferenceFormat: (ref: string[]) => string; /** * Empty objects - {} - indicate empty groups. But undefined indicates unfound * token. * @example * isEmptyGroup({}) // true * isEmptyGroup(undefined) // false * isEmptyGroup({a: '#ccc'}) // false * isEmptyGroup('') // false */ export declare const isEmptyGroup: (val: undefined | KsTokenValue | KsDesignTokenSrc | KsDesignTokenSrcGroup) => boolean; /** * Extract token references (i.e. `{size.s.value} {size.m.value}`) from a token * value. Since a token's value can contain multiple references due to string * interpolation "{size.padding.base.value} {color.border.primary.value}" * references is an array of 0 or more references */ export declare function getReferences(tokenValue: KsTokenValue): { path: string[]; }[]; /** * Return the actual token object referenced by another token. This is helpful * to retrieve things like the actual name of the referenced token. Note: this * is tested within style-dict.test.ts due to requiring fs/transformed tokens. */ export declare const getReferencedToken: ({ token, tokens, }: { token: KsDesignTokenData; tokens: KsDesignTokenData[]; }) => KsDesignTokenData | undefined; /** * Transform src object to a getables object so lookups for values are easier * and faster. A "getable" is a string that looks like `a.b.c.value`. * * KsDesignTokensSrcGroup transforms from: * @example * const exampleSrc: KsDesignTokensSrcGroup = { * a: { * b: { * c: { * value: '#ccc', * }, * }, * }, * d: { * e: { * f: { * value: '{a.b.c.value} hi #ccc', * }, * }, * }, * }; * * To a "getables" object like so: * @example * const exampleGetables: Getables = { * 'a.b.c.value': '#ccc', * 'd.e.f.value': '{a.b.c.value}', * }; */ export declare const srcToGetables: ({ tokensGroupSrc, }: { tokensGroupSrc: KsDesignTokenSrcGroup; }) => Getables; /** * Generate path array from getable string. * @example * /// 'a.b.c.value' -> ['a', 'b', 'c'] * makePathFromGetable('a.b.c.value') */ export declare const makePathFromGetable: (getable: string) => string[]; /** * Renaming a group/token means changing its position within the src object. Yet, * there are references to the fromPath (original path) littered throughout src. * Taking the reference path (derived from ref values like {a.b.value}), we can * determine what the new reference path should be. * * A ref value of {a.b.c.value} would be affectd by any group change that changes * 1. a * 2. b * 3. a.b * If 'a' was renamed 'x', then the ref would need to change to 'x.b.c.value' * If 'a.b' was renamed to 'x.y', then the ref would need to change to 'x.y.c.value' * */ export declare const renameGroupRefPathUpdate: (fromPath: string[], toPath: string[], refPath: string[]) => string[]; /** * Return a tuple of the path array and name from just the getable string. * @example * /// 'a.b.value' -> [['a', 'b'], 'a-b'] * makePathAndNameFromGetable()('a.b.value') * @example * /// (prefix: 'nerp') 'a.b.value' -> [['a', 'b'], 'nerp-a-b'] * makePathAndNameFromGetable('nerp')('a.b.value') */ export declare const makePathAndNameFromGetable: (prefix?: string) => (getable: string) => [string[], string]; /** * Lookup against the original source and return the object. Takes the "getable" * string format that looks like 'a.b.c.value'. Note that we strip off '.value' * to do a lookup on the original source object. */ export declare const getSrcObjectFromGetable: (src: KsDesignTokenSrcGroup) => (getable: string) => KsDesignTokenSrc; /** * Same lookup as below but returns undefined if not found instead of error */ export declare const findSrcObjectFromPathArray: (src: KsDesignTokenSrcGroup) => (path: string[]) => KsDesignTokenSrcGroup | KsDesignTokenSrc | undefined; /** * Lookup against the original source using a path array and return the object. * Takes the "path array" format that looks like `['a', 'b']`. */ export declare const getSrcObjectFromPathArray: (src: KsDesignTokenSrcGroup) => (path: string[]) => KsDesignTokenSrc | void; /** * From a getables object, resolve all references (deep and wide). It is * important for all() and single() to share a scope since they reference each * other and we don't want to redeclare memoized functions. */ export declare const resolveGetables: (getables: Getables) => { lookupSingle: ((getableLookup: KsTokenValue) => KsTokenValue) & memoize.Memoized<(getableLookup: KsTokenValue) => KsTokenValue>; lookupAll: ((getableLookup: KsTokenValue) => KsTokenValue) & memoize.Memoized<(getableLookup: KsTokenValue) => KsTokenValue>; }; /** * Paths within src of all tokens that reference this token. */ export declare const lookupAllReferencingTokens: (getables: Getables) => (path: string[]) => string[][]; /** * Return the headers of the getables for UI purposes * @example * /// headers: ['a.b', 'c', 'foo.bar'] */ export declare const generateHeaders: (entries: [string, KsTokenValue][]) => string[]; /** * A helper that curries the above functions encapsulated with the `getables` * object for easy lookup. */ export declare function getableUtil(tokensGroupSrc: KsDesignTokenSrcGroup, prefix?: string | undefined): { _src: KsDesignTokenSrcGroup; src: KsDesignTokenSrcGroup; readonly getables: Getables; readonly getEntries: [string, KsTokenValue][]; readonly resolveGetables: { lookupSingle: ((getableLookup: KsTokenValue) => KsTokenValue) & memoize.Memoized<(getableLookup: KsTokenValue) => KsTokenValue>; lookupAll: ((getableLookup: KsTokenValue) => KsTokenValue) & memoize.Memoized<(getableLookup: KsTokenValue) => KsTokenValue>; }; readonly lookupAllReferencingTokens: (path: string[]) => string[][]; readonly getSrcObjectFromGetable: (getable: string) => KsDesignTokenSrc; readonly findSrcObjectFromPathArray: (path: string[]) => KsDesignTokenSrcGroup | KsDesignTokenSrc | undefined; readonly getSrcObjectFromPathArray: (path: string[]) => KsDesignTokenSrc | void; readonly generateHeaders: string[]; pathFromTokenName: (name: string) => string[]; makeTokenName: (pathArray: string[]) => string; /** * Deleting a token should find all references and set the value of all * references to the fully resolved value of the deleted token. */ deleteSingleToken(tokenPath: string[], updateRefs?: boolean): { src: KsDesignTokenSrcGroup; value: KsTokenValue; deletedToken: KsDesignTokenSrc; }; /** * Adding a token takes into account parent and children so as not to * destroy existing tokens/groups. * */ addSingleToken(tokenPath: string[], tokenSrc: KsDesignTokenSrc): { src: KsDesignTokenSrcGroup; }; /** * Renaming a token is simply deleting the old token and adding a new one. * Utilize the existing add and delete methods to do this. */ renameSingleToken(oldTokenPath: string[], newTokenPath: string[]): { src: KsDesignTokenSrcGroup; oldName: string; newName: string; }; /** * Edit a token's value or comment */ updateSingleToken(tokenPath: string[], tokenSrc: Partial): { newTokenSrc: KsDesignTokenSrc; }; /** * Add empty group to path within src */ addGroup(groupPath: string[]): string[]; /** * Rename group, updates all tokens to with refs to this group to new group */ renameGroup(fromPath: string[], toPath: string[]): KsDesignTokenSrcGroup; /** * Delete group, updates all tokens within to hard references */ deleteGroup(path: string[]): KsDesignTokenSrcGroup; }; export declare const assertDotlessId: (id: string) => void; declare const TokenPathArgs: import("superstruct").Struct; /** * Take in string|string[] and guarantee return a string[] *with no dots* per part */ export declare const ensureTokenPath: (path: Infer) => string[]; export {}; //# sourceMappingURL=utils.d.ts.map