@terrazzo/token-tools
Version:
Various utilities for token types
52 lines (49 loc) • 2.04 kB
JavaScript
import { COLORSPACE, CSS_TO_CULORI, CULORI_TO_CSS, kebabCase, padStr, parseColor, pluralize, tokenToCulori } from "./string-BwN3ybNv.js";
import wcmatch from "wildcard-match";
//#region src/id.ts
const ALIAS_RE = /^\{([^}]+)\}$/;
/** Is this token an alias of another? */
function isAlias(value) {
return ALIAS_RE.test(value);
}
/** Same as isTokenMatch but returns the matching pattern */
function getTokenMatch(tokenId, globPatterns) {
for (const pattern of globPatterns) if (wcmatch(pattern)(tokenId)) return pattern;
}
/** Make an alias */
function makeAlias(input) {
return input.replace(/^\{?([^}]+)\}?$/, "{$1}");
}
/** Parse an alias */
function parseAlias(input) {
if (input.includes("#")) throw new Error("Mode aliases (# character) are no longer supported as of v0.6.0. Alias the root token instead, and apply modes in plugins.");
const match = input.match(ALIAS_RE);
if (!match) return input;
return match[1] ?? match[0];
}
/** split a token ID into a local ID and group ID */
function splitID(id) {
const lastSeparatorI = id.lastIndexOf(".");
if (lastSeparatorI === -1) return { local: id };
return {
local: id.substring(lastSeparatorI + 1),
group: id.substring(0, lastSeparatorI)
};
}
//#endregion
//#region src/transform.ts
/** Give a user pertinent feedback if they override a transform incorrectly */
function validateCustomTransform(value, { $type }) {
if (value) {
if (typeof value !== "string" && typeof value !== "object" || Array.isArray(value)) throw new Error(`transform(): expected string or Object of strings, received ${Array.isArray(value) ? "Array" : typeof value}`);
switch ($type) {
case "typography": {
if (typeof value !== "object") throw new Error("transform(): typography tokens must be an object of keys");
break;
}
}
}
}
//#endregion
export { COLORSPACE, CSS_TO_CULORI, CULORI_TO_CSS, getTokenMatch, isAlias, kebabCase, makeAlias, padStr, parseAlias, parseColor, pluralize, splitID, tokenToCulori, validateCustomTransform };
//# sourceMappingURL=index.js.map