@@ -37,8 +37,7 @@ interface FlattenedToken {
37
37
38
38
/**
39
39
* 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.
42
41
* @param tokens The collection of tokens to flatten.
43
42
* @param prefix An optional prefix to prepend to each token ID.
44
43
* @returns An array of flattened tokens, each with an ID and value.
@@ -62,44 +61,40 @@ function flatten (tokens: TokenCollection, prefix = ''): FlattenedToken[] {
62
61
}
63
62
64
63
/**
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
70
65
* @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
73
67
*/
74
- function resolveAliases ( tokens : Record < string , TokenValue > ) : Record < string , string > {
68
+ function dereference ( tokens : Record < string , TokenValue > ) : Record < string , string > {
75
69
const resolved : Record < string , string > = { }
76
70
const resolving = new Set < string > ( )
77
71
72
+ function isTokenAlias ( value : any ) : value is TokenAlias {
73
+ return typeof value === 'object' && value !== null && '$value' in value
74
+ }
75
+
78
76
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
87
82
}
88
83
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
93
88
}
94
89
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
98
93
}
99
94
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 )
103
98
104
99
return result
105
100
}
@@ -112,15 +107,15 @@ function resolveAliases (tokens: Record<string, TokenValue>): Record<string, str
112
107
}
113
108
114
109
/**
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
118
116
*
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
124
119
*/
125
120
export function useTokens <
126
121
Z extends TokenContext ,
@@ -138,7 +133,7 @@ export function useTokens<
138
133
collection . set ( id , value )
139
134
}
140
135
141
- const resolved = computed ( ( ) => resolveAliases ( Object . fromEntries ( collection . entries ( ) ) ) )
136
+ const resolved = computed ( ( ) => dereference ( Object . fromEntries ( collection . entries ( ) ) ) )
142
137
143
138
for ( const { id, value } of flattened ) {
144
139
const resolvedValue = resolved . value [ id ] || ( typeof value === 'string' ? value : value . $value )
0 commit comments