From 3e397d9e4661a42d14ecd6b6316eb4da766e3ccd Mon Sep 17 00:00:00 2001 From: yarastqt Date: Mon, 13 Apr 2020 17:05:49 +0300 Subject: [PATCH] feat: impl flat tokens --- src/core/flat-tokens.ts | 24 ++++++++++++++++++++++++ src/core/with-prefix.ts | 12 ++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/core/flat-tokens.ts create mode 100644 src/core/with-prefix.ts diff --git a/src/core/flat-tokens.ts b/src/core/flat-tokens.ts new file mode 100644 index 0000000..90f077b --- /dev/null +++ b/src/core/flat-tokens.ts @@ -0,0 +1,24 @@ +import { withPrefix } from './with-prefix' +import { Shape, FlattenToken, Token, TokensMap } from './token.h' + +/** + * Returns flatten shape with tokens. + * + * @param tokens Map with tokens. + */ +export function flatTokens(tokens: TokensMap, __prefix__?: string): Shape { + const result: Shape = {} + for (const key in tokens) { + const transformedKey = withPrefix(key, __prefix__) + const maybeToken = tokens[key] + if (maybeToken.value === undefined) { + Object.assign(result, flatTokens(maybeToken as TokensMap, transformedKey)) + } else { + result[transformedKey] = { + ...maybeToken as Token, + name: transformedKey, + } + } + } + return result +} diff --git a/src/core/with-prefix.ts b/src/core/with-prefix.ts new file mode 100644 index 0000000..f16a319 --- /dev/null +++ b/src/core/with-prefix.ts @@ -0,0 +1,12 @@ +/** + * Returns string with prefix. + * + * @param value Raw string. + * @param prefix Prefix for string. + */ +export function withPrefix(value: string, prefix?: string): string { + if (prefix === undefined) { + return value + } + return `${prefix}_${value}` +}