@primer/primitives
Version:
Typography, spacing, and color primitives for Primer design system
32 lines (31 loc) • 1.38 kB
JavaScript
import { upperCaseFirstCharacter } from '../utilities/index.js';
/**
* camelCase
* @scope custom camelCase implementation should only be used within `name path to dot notation` transformer
* @description replaces space ` `, dash `-`, underscore `_` and plus `+` by camelCasing e.g. `camel-case` -> `camelCase`
* @param string
* @returns string
*/
const camelCase = (string) => {
return string
.split(/[\s-_+]+/g)
.map((stringPart, index) => (index === 0 ? stringPart : upperCaseFirstCharacter(stringPart)))
.join('');
};
/**
* @description converts the [TransformedToken's](https://github.com/amzn/style-dictionary/blob/main/types/TransformedToken.d.ts) `.path` array to a dot.notation string
* @type name transformer — [StyleDictionary.NameTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
* @matcher omitted to match all tokens
* @transformer returns `string` in dot.notation
*/
export const namePathToDotNotation = {
name: 'name/pathToDotNotation',
type: 'name',
transform: (token, options) => {
return ([options === null || options === void 0 ? void 0 : options.prefix, ...token.path]
// remove undefined if exists
.filter((part) => typeof part === 'string' && part !== '@')
.map((part) => camelCase(part))
.join('.'));
},
};