Skip to content

Commit 3808d1c

Browse files
committed
chore(useTokens): clean-up
1 parent ed1a92f commit 3808d1c

File tree

2 files changed

+38
-38
lines changed

2 files changed

+38
-38
lines changed

packages/0/src/composables/useTokens/index.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
// Composables
2+
import { useTokens } from './index'
3+
4+
// Utilities
15
import { describe, it, expect, vi, beforeEach } from 'vitest'
26
import { mount } from '@vue/test-utils'
37
import { defineComponent, ref, nextTick, computed } from 'vue'
4-
import { useTokens } from './index'
8+
9+
// Types
510
import type { TokenCollection } from './index'
611

712
describe('useTokens', () => {

packages/0/src/composables/useTokens/index.ts

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ interface FlattenedToken {
3737

3838
/**
3939
* Flattens a nested collection of tokens into a flat array of tokens.
40-
* Each token is represented by an object containing its ID and value.
41-
*
40+
* Each token is represented by an object containing its ID & value.
4241
* @param tokens The collection of tokens to flatten.
4342
* @param prefix An optional prefix to prepend to each token ID.
4443
* @returns An array of flattened tokens, each with an ID and value.
@@ -62,44 +61,40 @@ function flatten (tokens: TokenCollection, prefix = ''): FlattenedToken[] {
6261
}
6362

6463
/**
65-
* Resolves token aliases within a collection of tokens.
66-
* This function replaces aliases in the tokens with their actual values,
67-
* handling circular references and invalid formats gracefully.
68-
*
69-
* @see Inspired by https://www.designtokens.org/tr/drafts/format/#aliases-references
64+
* Resolves token aliases within a collection of tokens
7065
* @param tokens The collection of tokens to resolve.
71-
* @returns A new collection of tokens with resolved aliases.
72-
* @throws Will log warnings for circular references or invalid alias formats.
66+
* @returns A new collection of dereferenced tokens
7367
*/
74-
function resolveAliases (tokens: Record<string, TokenValue>): Record<string, string> {
68+
function dereference (tokens: Record<string, TokenValue>): Record<string, string> {
7569
const resolved: Record<string, string> = {}
7670
const resolving = new Set<string>()
7771

72+
function isTokenAlias (value: any): value is TokenAlias {
73+
return typeof value === 'object' && value !== null && '$value' in value
74+
}
75+
7876
function resolve (key: string, value: TokenValue): string {
79-
const isTokenAlias = (v: any): v is TokenAlias => typeof v === 'object' && v !== null && '$value' in v
80-
const ref = isTokenAlias(value) ? value.$value : value
81-
82-
if (typeof ref !== 'string' || !ref.startsWith('{') || !ref.endsWith('}')) {
83-
if (isTokenAlias(value)) {
84-
console.warn(`Invalid alias format for "${key}": ${ref}`)
85-
}
86-
return ref
77+
const reference = isTokenAlias(value) ? value.$value : value
78+
79+
if (typeof reference !== 'string' || !reference.startsWith('{') || !reference.endsWith('}')) {
80+
if (isTokenAlias(value)) console.warn(`Invalid alias format for "${key}": ${reference}`)
81+
return reference
8782
}
8883

89-
const aliasPath = ref.slice(1, -1)
90-
if (resolving.has(aliasPath)) {
91-
console.warn(`Circular reference detected for "${key}": ${aliasPath}`)
92-
return ref
84+
const alias = reference.slice(1, -1)
85+
if (resolving.has(alias)) {
86+
console.warn(`Circular reference detected for "${key}": ${alias}`)
87+
return reference
9388
}
9489

95-
if (!(aliasPath in tokens)) {
96-
console.warn(`Alias not found for "${key}": ${aliasPath}`)
97-
return ref
90+
if (!(alias in tokens)) {
91+
console.warn(`Alias not found for "${key}": ${alias}`)
92+
return reference
9893
}
9994

100-
resolving.add(aliasPath)
101-
const result = resolve(aliasPath, tokens[aliasPath])
102-
resolving.delete(aliasPath)
95+
resolving.add(alias)
96+
const result = resolve(alias, tokens[alias])
97+
resolving.delete(alias)
10398

10499
return result
105100
}
@@ -112,15 +107,15 @@ function resolveAliases (tokens: Record<string, TokenValue>): Record<string, str
112107
}
113108

114109
/**
115-
* Creates a token registrar for managing tokens within a specific namespace.
116-
* This function provides a way to register, unregister, and resolve tokens,
117-
* allowing for dynamic token management in applications.
110+
* Creates a token registrar for managing data structures / aliases
111+
* @param namespace The namespace for the token registrar context
112+
* @param tokens An optional collection of tokens to initialize
113+
* @template Z The available methods for the token's context.
114+
* @template E The structure of the registry token tickets.
115+
* @returns A trinity of provide/inject methods & context
118116
*
119-
* @param namespace The namespace for the token registrar context.
120-
* @param tokens An optional collection of tokens to initialize the registrar with.
121-
* @template Z The type of the tokens managed by the registrar.
122-
* @template E The type of the token context.
123-
* @returns A tuple containing the inject function, provide function, and the token context.
117+
* @see Inspired by https://www.designtokens.org/tr/drafts/format/#aliases-references
118+
* @see https://0.vuetifyjs.com/composables/registration/use-tokens
124119
*/
125120
export function useTokens<
126121
Z extends TokenContext,
@@ -138,7 +133,7 @@ export function useTokens<
138133
collection.set(id, value)
139134
}
140135

141-
const resolved = computed(() => resolveAliases(Object.fromEntries(collection.entries())))
136+
const resolved = computed(() => dereference(Object.fromEntries(collection.entries())))
142137

143138
for (const { id, value } of flattened) {
144139
const resolvedValue = resolved.value[id] || (typeof value === 'string' ? value : value.$value)

0 commit comments

Comments
 (0)