@attrx/role-morphic 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +151 -84
- package/dist/cast.d.mts +678 -0
- package/dist/cast.d.ts +678 -0
- package/dist/cast.js +3569 -0
- package/dist/cast.js.map +1 -0
- package/dist/cast.mjs +3536 -0
- package/dist/cast.mjs.map +1 -0
- package/dist/constants-BZdBwuvJ.d.mts +168 -0
- package/dist/constants-BZdBwuvJ.d.ts +168 -0
- package/dist/convert.d.mts +1157 -0
- package/dist/convert.d.ts +1157 -0
- package/dist/convert.js +2244 -0
- package/dist/convert.js.map +1 -0
- package/dist/convert.mjs +2148 -0
- package/dist/convert.mjs.map +1 -0
- package/dist/format-Be15LzfS.d.ts +668 -0
- package/dist/format-o4Y3jPH-.d.mts +668 -0
- package/dist/format.d.mts +3 -0
- package/dist/format.d.ts +3 -0
- package/dist/format.js +2303 -0
- package/dist/format.js.map +1 -0
- package/dist/format.mjs +2286 -0
- package/dist/format.mjs.map +1 -0
- package/dist/index.d.mts +200 -601
- package/dist/index.d.ts +200 -601
- package/dist/index.js +5536 -2493
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +5349 -2494
- package/dist/index.mjs.map +1 -1
- package/dist/types-mbeS1e-k.d.mts +312 -0
- package/dist/types-mbeS1e-k.d.ts +312 -0
- package/dist/validate.d.mts +884 -0
- package/dist/validate.d.ts +884 -0
- package/dist/validate.js +713 -0
- package/dist/validate.js.map +1 -0
- package/dist/validate.mjs +669 -0
- package/dist/validate.mjs.map +1 -0
- package/package.json +21 -1
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RoleMorphic Types
|
|
3
|
+
*
|
|
4
|
+
* Sistema de tipos para conversão polimórfica de valores.
|
|
5
|
+
* Um valor pode assumir múltiplas formas (variantes) mantendo sua identidade semântica.
|
|
6
|
+
*
|
|
7
|
+
* Os 4 Pilares:
|
|
8
|
+
* - Cast: Normaliza input sujo para o tipo esperado
|
|
9
|
+
* - Validate: Verifica regras semânticas do valor
|
|
10
|
+
* - Convert: Transforma entre variantes (via base)
|
|
11
|
+
* - Format: Apresenta o valor como string legível
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Função que converte um valor para o formato base da role
|
|
15
|
+
*/
|
|
16
|
+
type ToBaseFn<TValue = unknown, TBase = unknown> = (value: TValue) => TBase;
|
|
17
|
+
/**
|
|
18
|
+
* Função que converte do formato base para uma variante específica
|
|
19
|
+
*/
|
|
20
|
+
type FromBaseFn<TBase = unknown, TValue = unknown> = (base: TBase) => TValue;
|
|
21
|
+
/**
|
|
22
|
+
* Função que tenta normalizar um input desconhecido para o tipo esperado
|
|
23
|
+
* Retorna null se não conseguir fazer o cast
|
|
24
|
+
*/
|
|
25
|
+
type CastFn<TValue = unknown> = (input: unknown) => TValue | null;
|
|
26
|
+
/**
|
|
27
|
+
* Resultado de uma operação de cast
|
|
28
|
+
*/
|
|
29
|
+
type CastResult<T> = {
|
|
30
|
+
ok: true;
|
|
31
|
+
value: T;
|
|
32
|
+
} | {
|
|
33
|
+
ok: false;
|
|
34
|
+
error: string;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Regras de validação para uma variante
|
|
38
|
+
*/
|
|
39
|
+
type ValidationRule = {
|
|
40
|
+
/** Valor mínimo permitido (para números) */
|
|
41
|
+
min?: number;
|
|
42
|
+
/** Valor máximo permitido (para números) */
|
|
43
|
+
max?: number;
|
|
44
|
+
/** Deve ser inteiro (para números) */
|
|
45
|
+
integer?: boolean;
|
|
46
|
+
/** Pattern regex (para strings) */
|
|
47
|
+
pattern?: RegExp;
|
|
48
|
+
/** Função de validação customizada - retorna mensagem de erro ou null se válido */
|
|
49
|
+
custom?: (value: unknown) => string | null;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Resultado de uma validação
|
|
53
|
+
*/
|
|
54
|
+
type ValidationResult = {
|
|
55
|
+
valid: boolean;
|
|
56
|
+
errors: string[];
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Opções base de formatação (compartilhadas por todas as roles)
|
|
60
|
+
*/
|
|
61
|
+
type BaseFormatOptions = {
|
|
62
|
+
/** Número de casas decimais */
|
|
63
|
+
decimals?: number;
|
|
64
|
+
/** Locale para formatação (ex: 'pt-BR', 'en-US') */
|
|
65
|
+
locale?: string;
|
|
66
|
+
/** Tipo de notação */
|
|
67
|
+
notation?: "standard" | "scientific" | "compact";
|
|
68
|
+
/** Usar nome completo ao invés de símbolo */
|
|
69
|
+
verbose?: boolean;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Opções de formatação genéricas
|
|
73
|
+
* @deprecated Use BaseFormatOptions ou tipos específicos (ColorFormatOptions, DateFormatOptions)
|
|
74
|
+
*/
|
|
75
|
+
type FormatOptions = BaseFormatOptions;
|
|
76
|
+
/**
|
|
77
|
+
* Função de formatação customizada
|
|
78
|
+
*/
|
|
79
|
+
type FormatFn<TValue = unknown> = (value: TValue, options?: BaseFormatOptions) => string;
|
|
80
|
+
/**
|
|
81
|
+
* Especificação de formatação para uma variante
|
|
82
|
+
*/
|
|
83
|
+
type FormatSpec<TValue = unknown> = {
|
|
84
|
+
/** Símbolo da unidade (ex: "m²", "ha", "°C") */
|
|
85
|
+
symbol: string;
|
|
86
|
+
/** Nome singular (ex: "meter", "hectare") */
|
|
87
|
+
singular?: string;
|
|
88
|
+
/** Nome plural (ex: "meters", "hectares") */
|
|
89
|
+
plural?: string;
|
|
90
|
+
/** Formatador customizado (override do padrão) */
|
|
91
|
+
formatter?: FormatFn<TValue>;
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Definição de uma variante dentro de uma role
|
|
95
|
+
*
|
|
96
|
+
* Cada variante implementa os 4 pilares:
|
|
97
|
+
* - Convert: toBase/fromBase (obrigatório)
|
|
98
|
+
* - Cast: cast (opcional, usa default por type)
|
|
99
|
+
* - Validate: validate (opcional)
|
|
100
|
+
* - Format: format (opcional)
|
|
101
|
+
*/
|
|
102
|
+
type VariantSpec<TValue = unknown, TBase = unknown> = {
|
|
103
|
+
/** Tipo primitivo do valor nesta variante */
|
|
104
|
+
type: "string" | "number" | "boolean" | "object" | "array";
|
|
105
|
+
/** Converte o valor desta variante para o formato base */
|
|
106
|
+
toBase: ToBaseFn<TValue, TBase>;
|
|
107
|
+
/** Converte do formato base para esta variante */
|
|
108
|
+
fromBase: FromBaseFn<TBase, TValue>;
|
|
109
|
+
/** Tenta normalizar um input desconhecido para o tipo esperado */
|
|
110
|
+
cast?: CastFn<TValue>;
|
|
111
|
+
/** Regras de validação semântica */
|
|
112
|
+
validate?: ValidationRule;
|
|
113
|
+
/** Especificação de formatação */
|
|
114
|
+
format?: FormatSpec<TValue>;
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Definição completa de uma Role
|
|
118
|
+
*
|
|
119
|
+
* Uma Role representa um conceito semântico (cor, área, data) que pode
|
|
120
|
+
* ser expresso em múltiplas variantes (hex, rgb, hsl para cor).
|
|
121
|
+
*/
|
|
122
|
+
type RoleSpec<TBase = unknown> = {
|
|
123
|
+
/** Nome da variante que serve como pivot para todas as conversões */
|
|
124
|
+
base: string;
|
|
125
|
+
/** Mapa de variantes disponíveis para esta role */
|
|
126
|
+
variants: Record<string, VariantSpec<unknown, TBase>>;
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* Identificador de variante parseado
|
|
130
|
+
*
|
|
131
|
+
* "color:rgb:object" => { role: "color", variant: "rgb:object" }
|
|
132
|
+
*/
|
|
133
|
+
type ParsedVariantId = {
|
|
134
|
+
role: string;
|
|
135
|
+
variant: string;
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* Resultado de uma conversão que pode falhar
|
|
139
|
+
*/
|
|
140
|
+
type ConversionResult<T> = {
|
|
141
|
+
ok: true;
|
|
142
|
+
value: T;
|
|
143
|
+
} | {
|
|
144
|
+
ok: false;
|
|
145
|
+
error: string;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Resultado de validação de reversibilidade
|
|
149
|
+
*/
|
|
150
|
+
type ReversibilityResult = {
|
|
151
|
+
reversible: boolean;
|
|
152
|
+
original: unknown;
|
|
153
|
+
converted: unknown;
|
|
154
|
+
reconverted: unknown;
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Função que transforma um valor de uma role para outra
|
|
158
|
+
*/
|
|
159
|
+
type MorpherFn<TFrom = unknown, TTo = unknown> = (value: TFrom) => TTo;
|
|
160
|
+
/**
|
|
161
|
+
* Chave de um morpher: "fromRole:fromVariant->toRole:toVariant"
|
|
162
|
+
*/
|
|
163
|
+
type MorpherKey = `${string}->${string}`;
|
|
164
|
+
/**
|
|
165
|
+
* Definição de um morpher
|
|
166
|
+
*/
|
|
167
|
+
type MorpherSpec<TFrom = unknown, TTo = unknown> = {
|
|
168
|
+
from: string;
|
|
169
|
+
to: string;
|
|
170
|
+
transform: MorpherFn<TFrom, TTo>;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Temperature Role - Constants
|
|
175
|
+
*
|
|
176
|
+
* Fórmulas de conversão, símbolos e aliases para unidades de temperatura.
|
|
177
|
+
*
|
|
178
|
+
* IMPORTANTE: Temperatura usa FÓRMULAS, não fatores multiplicativos.
|
|
179
|
+
* Isso porque as escalas têm offsets diferentes (0°C ≠ 0°F ≠ 0K).
|
|
180
|
+
*
|
|
181
|
+
* Fontes:
|
|
182
|
+
* - NIST SP 811 (2008)
|
|
183
|
+
* - SI Brochure, 9th Edition (2019)
|
|
184
|
+
*/
|
|
185
|
+
|
|
186
|
+
type TemperatureUnit = "celsius" | "fahrenheit" | "kelvin" | "rankine";
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Color Role - Types
|
|
190
|
+
*
|
|
191
|
+
* Tipos para representação de cores nas diferentes variantes.
|
|
192
|
+
*/
|
|
193
|
+
/**
|
|
194
|
+
* Tipo base para Color: objeto RGBA com valores 0-255 e alpha 0-1
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* const red: RGBA = { r: 255, g: 0, b: 0, a: 1 };
|
|
198
|
+
* const semiTransparent: RGBA = { r: 0, g: 0, b: 255, a: 0.5 };
|
|
199
|
+
*/
|
|
200
|
+
interface RGBA {
|
|
201
|
+
r: number;
|
|
202
|
+
g: number;
|
|
203
|
+
b: number;
|
|
204
|
+
a: number;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Variante Hex
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* "#ff0000" // RGB (6 chars)
|
|
211
|
+
* "#ff0000ff" // RGBA (8 chars)
|
|
212
|
+
* "#f00" // RGB shorthand (3 chars)
|
|
213
|
+
* "#f00f" // RGBA shorthand (4 chars)
|
|
214
|
+
*/
|
|
215
|
+
type ColorHex = string;
|
|
216
|
+
/**
|
|
217
|
+
* Variante RGB Object
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* { r: 255, g: 0, b: 0 }
|
|
221
|
+
* { r: 255, g: 0, b: 0, a: 0.5 }
|
|
222
|
+
*/
|
|
223
|
+
interface ColorRgbObject {
|
|
224
|
+
r: number;
|
|
225
|
+
g: number;
|
|
226
|
+
b: number;
|
|
227
|
+
a?: number;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Variante RGB String
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* "rgb(255, 0, 0)"
|
|
234
|
+
* "rgba(255, 0, 0, 0.5)"
|
|
235
|
+
*/
|
|
236
|
+
type ColorRgbString = string;
|
|
237
|
+
/**
|
|
238
|
+
* Variante HSL Object
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* { h: 0, s: 100, l: 50 } // Red
|
|
242
|
+
* { h: 0, s: 100, l: 50, a: 0.5 } // Semi-transparent red
|
|
243
|
+
*/
|
|
244
|
+
interface ColorHslObject {
|
|
245
|
+
h: number;
|
|
246
|
+
s: number;
|
|
247
|
+
l: number;
|
|
248
|
+
a?: number;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Variante HSL String
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* "hsl(0, 100%, 50%)"
|
|
255
|
+
* "hsla(0, 100%, 50%, 0.5)"
|
|
256
|
+
*/
|
|
257
|
+
type ColorHslString = string;
|
|
258
|
+
type ColorVariant = "hex" | "rgb_object" | "rgb_string" | "hsl_object" | "hsl_string";
|
|
259
|
+
|
|
260
|
+
interface ColorFormatOptions extends BaseFormatOptions {
|
|
261
|
+
/** Se true, usa letras maiúsculas para hex */
|
|
262
|
+
uppercase?: boolean;
|
|
263
|
+
/** Se true, inclui alpha mesmo quando é 1 */
|
|
264
|
+
includeAlpha?: boolean;
|
|
265
|
+
/** Se true, usa formato compacto (shorthand hex, sem espaços em rgb) */
|
|
266
|
+
compact?: boolean;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Date Role - Types
|
|
271
|
+
*
|
|
272
|
+
* Tipos para representação de datas nas diferentes variantes.
|
|
273
|
+
*/
|
|
274
|
+
/**
|
|
275
|
+
* Tipo base para Date: timestamp em milissegundos (como JavaScript Date.getTime())
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* const ts: DateTimestamp = 1733425800000; // 2024-12-05T19:30:00.000Z
|
|
279
|
+
*/
|
|
280
|
+
type DateTimestamp = number;
|
|
281
|
+
/**
|
|
282
|
+
* Variante ISO 8601
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* "2024-12-05T19:30:00.000Z"
|
|
286
|
+
* "2024-12-05T19:30:00.000-03:00"
|
|
287
|
+
* "2024-12-05"
|
|
288
|
+
*/
|
|
289
|
+
type DateIso = string;
|
|
290
|
+
/**
|
|
291
|
+
* Variante Epoch (segundos desde 1970-01-01T00:00:00Z)
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* 1733425800 // 2024-12-05T19:30:00Z
|
|
295
|
+
*/
|
|
296
|
+
type DateEpoch = number;
|
|
297
|
+
type DateVariant = "iso" | "timestamp" | "epoch";
|
|
298
|
+
|
|
299
|
+
interface DateFormatOptions extends BaseFormatOptions {
|
|
300
|
+
/** Estilo de data: "full", "long", "medium", "short" */
|
|
301
|
+
dateStyle?: "full" | "long" | "medium" | "short";
|
|
302
|
+
/** Estilo de hora: "full", "long", "medium", "short" */
|
|
303
|
+
timeStyle?: "full" | "long" | "medium" | "short";
|
|
304
|
+
/** Timezone (ex: "America/Sao_Paulo", "UTC") */
|
|
305
|
+
timeZone?: string;
|
|
306
|
+
/** Se true, mostra apenas data (sem hora) */
|
|
307
|
+
dateOnly?: boolean;
|
|
308
|
+
/** Se true, mostra apenas hora (sem data) */
|
|
309
|
+
timeOnly?: boolean;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export type { BaseFormatOptions as B, ConversionResult as C, DateTimestamp as D, FromBaseFn as F, MorpherSpec as M, ParsedVariantId as P, RoleSpec as R, TemperatureUnit as T, ValidationResult as V, VariantSpec as a, ReversibilityResult as b, DateFormatOptions as c, RGBA as d, ColorFormatOptions as e, ToBaseFn as f, MorpherFn as g, MorpherKey as h, FormatOptions as i, FormatSpec as j, FormatFn as k, ValidationRule as l, CastFn as m, CastResult as n, DateVariant as o, DateIso as p, DateEpoch as q, ColorVariant as r, ColorHex as s, ColorRgbObject as t, ColorRgbString as u, ColorHslObject as v, ColorHslString as w };
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RoleMorphic Types
|
|
3
|
+
*
|
|
4
|
+
* Sistema de tipos para conversão polimórfica de valores.
|
|
5
|
+
* Um valor pode assumir múltiplas formas (variantes) mantendo sua identidade semântica.
|
|
6
|
+
*
|
|
7
|
+
* Os 4 Pilares:
|
|
8
|
+
* - Cast: Normaliza input sujo para o tipo esperado
|
|
9
|
+
* - Validate: Verifica regras semânticas do valor
|
|
10
|
+
* - Convert: Transforma entre variantes (via base)
|
|
11
|
+
* - Format: Apresenta o valor como string legível
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Função que converte um valor para o formato base da role
|
|
15
|
+
*/
|
|
16
|
+
type ToBaseFn<TValue = unknown, TBase = unknown> = (value: TValue) => TBase;
|
|
17
|
+
/**
|
|
18
|
+
* Função que converte do formato base para uma variante específica
|
|
19
|
+
*/
|
|
20
|
+
type FromBaseFn<TBase = unknown, TValue = unknown> = (base: TBase) => TValue;
|
|
21
|
+
/**
|
|
22
|
+
* Função que tenta normalizar um input desconhecido para o tipo esperado
|
|
23
|
+
* Retorna null se não conseguir fazer o cast
|
|
24
|
+
*/
|
|
25
|
+
type CastFn<TValue = unknown> = (input: unknown) => TValue | null;
|
|
26
|
+
/**
|
|
27
|
+
* Resultado de uma operação de cast
|
|
28
|
+
*/
|
|
29
|
+
type CastResult<T> = {
|
|
30
|
+
ok: true;
|
|
31
|
+
value: T;
|
|
32
|
+
} | {
|
|
33
|
+
ok: false;
|
|
34
|
+
error: string;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Regras de validação para uma variante
|
|
38
|
+
*/
|
|
39
|
+
type ValidationRule = {
|
|
40
|
+
/** Valor mínimo permitido (para números) */
|
|
41
|
+
min?: number;
|
|
42
|
+
/** Valor máximo permitido (para números) */
|
|
43
|
+
max?: number;
|
|
44
|
+
/** Deve ser inteiro (para números) */
|
|
45
|
+
integer?: boolean;
|
|
46
|
+
/** Pattern regex (para strings) */
|
|
47
|
+
pattern?: RegExp;
|
|
48
|
+
/** Função de validação customizada - retorna mensagem de erro ou null se válido */
|
|
49
|
+
custom?: (value: unknown) => string | null;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Resultado de uma validação
|
|
53
|
+
*/
|
|
54
|
+
type ValidationResult = {
|
|
55
|
+
valid: boolean;
|
|
56
|
+
errors: string[];
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Opções base de formatação (compartilhadas por todas as roles)
|
|
60
|
+
*/
|
|
61
|
+
type BaseFormatOptions = {
|
|
62
|
+
/** Número de casas decimais */
|
|
63
|
+
decimals?: number;
|
|
64
|
+
/** Locale para formatação (ex: 'pt-BR', 'en-US') */
|
|
65
|
+
locale?: string;
|
|
66
|
+
/** Tipo de notação */
|
|
67
|
+
notation?: "standard" | "scientific" | "compact";
|
|
68
|
+
/** Usar nome completo ao invés de símbolo */
|
|
69
|
+
verbose?: boolean;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Opções de formatação genéricas
|
|
73
|
+
* @deprecated Use BaseFormatOptions ou tipos específicos (ColorFormatOptions, DateFormatOptions)
|
|
74
|
+
*/
|
|
75
|
+
type FormatOptions = BaseFormatOptions;
|
|
76
|
+
/**
|
|
77
|
+
* Função de formatação customizada
|
|
78
|
+
*/
|
|
79
|
+
type FormatFn<TValue = unknown> = (value: TValue, options?: BaseFormatOptions) => string;
|
|
80
|
+
/**
|
|
81
|
+
* Especificação de formatação para uma variante
|
|
82
|
+
*/
|
|
83
|
+
type FormatSpec<TValue = unknown> = {
|
|
84
|
+
/** Símbolo da unidade (ex: "m²", "ha", "°C") */
|
|
85
|
+
symbol: string;
|
|
86
|
+
/** Nome singular (ex: "meter", "hectare") */
|
|
87
|
+
singular?: string;
|
|
88
|
+
/** Nome plural (ex: "meters", "hectares") */
|
|
89
|
+
plural?: string;
|
|
90
|
+
/** Formatador customizado (override do padrão) */
|
|
91
|
+
formatter?: FormatFn<TValue>;
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Definição de uma variante dentro de uma role
|
|
95
|
+
*
|
|
96
|
+
* Cada variante implementa os 4 pilares:
|
|
97
|
+
* - Convert: toBase/fromBase (obrigatório)
|
|
98
|
+
* - Cast: cast (opcional, usa default por type)
|
|
99
|
+
* - Validate: validate (opcional)
|
|
100
|
+
* - Format: format (opcional)
|
|
101
|
+
*/
|
|
102
|
+
type VariantSpec<TValue = unknown, TBase = unknown> = {
|
|
103
|
+
/** Tipo primitivo do valor nesta variante */
|
|
104
|
+
type: "string" | "number" | "boolean" | "object" | "array";
|
|
105
|
+
/** Converte o valor desta variante para o formato base */
|
|
106
|
+
toBase: ToBaseFn<TValue, TBase>;
|
|
107
|
+
/** Converte do formato base para esta variante */
|
|
108
|
+
fromBase: FromBaseFn<TBase, TValue>;
|
|
109
|
+
/** Tenta normalizar um input desconhecido para o tipo esperado */
|
|
110
|
+
cast?: CastFn<TValue>;
|
|
111
|
+
/** Regras de validação semântica */
|
|
112
|
+
validate?: ValidationRule;
|
|
113
|
+
/** Especificação de formatação */
|
|
114
|
+
format?: FormatSpec<TValue>;
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Definição completa de uma Role
|
|
118
|
+
*
|
|
119
|
+
* Uma Role representa um conceito semântico (cor, área, data) que pode
|
|
120
|
+
* ser expresso em múltiplas variantes (hex, rgb, hsl para cor).
|
|
121
|
+
*/
|
|
122
|
+
type RoleSpec<TBase = unknown> = {
|
|
123
|
+
/** Nome da variante que serve como pivot para todas as conversões */
|
|
124
|
+
base: string;
|
|
125
|
+
/** Mapa de variantes disponíveis para esta role */
|
|
126
|
+
variants: Record<string, VariantSpec<unknown, TBase>>;
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* Identificador de variante parseado
|
|
130
|
+
*
|
|
131
|
+
* "color:rgb:object" => { role: "color", variant: "rgb:object" }
|
|
132
|
+
*/
|
|
133
|
+
type ParsedVariantId = {
|
|
134
|
+
role: string;
|
|
135
|
+
variant: string;
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* Resultado de uma conversão que pode falhar
|
|
139
|
+
*/
|
|
140
|
+
type ConversionResult<T> = {
|
|
141
|
+
ok: true;
|
|
142
|
+
value: T;
|
|
143
|
+
} | {
|
|
144
|
+
ok: false;
|
|
145
|
+
error: string;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Resultado de validação de reversibilidade
|
|
149
|
+
*/
|
|
150
|
+
type ReversibilityResult = {
|
|
151
|
+
reversible: boolean;
|
|
152
|
+
original: unknown;
|
|
153
|
+
converted: unknown;
|
|
154
|
+
reconverted: unknown;
|
|
155
|
+
};
|
|
156
|
+
/**
|
|
157
|
+
* Função que transforma um valor de uma role para outra
|
|
158
|
+
*/
|
|
159
|
+
type MorpherFn<TFrom = unknown, TTo = unknown> = (value: TFrom) => TTo;
|
|
160
|
+
/**
|
|
161
|
+
* Chave de um morpher: "fromRole:fromVariant->toRole:toVariant"
|
|
162
|
+
*/
|
|
163
|
+
type MorpherKey = `${string}->${string}`;
|
|
164
|
+
/**
|
|
165
|
+
* Definição de um morpher
|
|
166
|
+
*/
|
|
167
|
+
type MorpherSpec<TFrom = unknown, TTo = unknown> = {
|
|
168
|
+
from: string;
|
|
169
|
+
to: string;
|
|
170
|
+
transform: MorpherFn<TFrom, TTo>;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Temperature Role - Constants
|
|
175
|
+
*
|
|
176
|
+
* Fórmulas de conversão, símbolos e aliases para unidades de temperatura.
|
|
177
|
+
*
|
|
178
|
+
* IMPORTANTE: Temperatura usa FÓRMULAS, não fatores multiplicativos.
|
|
179
|
+
* Isso porque as escalas têm offsets diferentes (0°C ≠ 0°F ≠ 0K).
|
|
180
|
+
*
|
|
181
|
+
* Fontes:
|
|
182
|
+
* - NIST SP 811 (2008)
|
|
183
|
+
* - SI Brochure, 9th Edition (2019)
|
|
184
|
+
*/
|
|
185
|
+
|
|
186
|
+
type TemperatureUnit = "celsius" | "fahrenheit" | "kelvin" | "rankine";
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Color Role - Types
|
|
190
|
+
*
|
|
191
|
+
* Tipos para representação de cores nas diferentes variantes.
|
|
192
|
+
*/
|
|
193
|
+
/**
|
|
194
|
+
* Tipo base para Color: objeto RGBA com valores 0-255 e alpha 0-1
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* const red: RGBA = { r: 255, g: 0, b: 0, a: 1 };
|
|
198
|
+
* const semiTransparent: RGBA = { r: 0, g: 0, b: 255, a: 0.5 };
|
|
199
|
+
*/
|
|
200
|
+
interface RGBA {
|
|
201
|
+
r: number;
|
|
202
|
+
g: number;
|
|
203
|
+
b: number;
|
|
204
|
+
a: number;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Variante Hex
|
|
208
|
+
*
|
|
209
|
+
* @example
|
|
210
|
+
* "#ff0000" // RGB (6 chars)
|
|
211
|
+
* "#ff0000ff" // RGBA (8 chars)
|
|
212
|
+
* "#f00" // RGB shorthand (3 chars)
|
|
213
|
+
* "#f00f" // RGBA shorthand (4 chars)
|
|
214
|
+
*/
|
|
215
|
+
type ColorHex = string;
|
|
216
|
+
/**
|
|
217
|
+
* Variante RGB Object
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* { r: 255, g: 0, b: 0 }
|
|
221
|
+
* { r: 255, g: 0, b: 0, a: 0.5 }
|
|
222
|
+
*/
|
|
223
|
+
interface ColorRgbObject {
|
|
224
|
+
r: number;
|
|
225
|
+
g: number;
|
|
226
|
+
b: number;
|
|
227
|
+
a?: number;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Variante RGB String
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* "rgb(255, 0, 0)"
|
|
234
|
+
* "rgba(255, 0, 0, 0.5)"
|
|
235
|
+
*/
|
|
236
|
+
type ColorRgbString = string;
|
|
237
|
+
/**
|
|
238
|
+
* Variante HSL Object
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* { h: 0, s: 100, l: 50 } // Red
|
|
242
|
+
* { h: 0, s: 100, l: 50, a: 0.5 } // Semi-transparent red
|
|
243
|
+
*/
|
|
244
|
+
interface ColorHslObject {
|
|
245
|
+
h: number;
|
|
246
|
+
s: number;
|
|
247
|
+
l: number;
|
|
248
|
+
a?: number;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Variante HSL String
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* "hsl(0, 100%, 50%)"
|
|
255
|
+
* "hsla(0, 100%, 50%, 0.5)"
|
|
256
|
+
*/
|
|
257
|
+
type ColorHslString = string;
|
|
258
|
+
type ColorVariant = "hex" | "rgb_object" | "rgb_string" | "hsl_object" | "hsl_string";
|
|
259
|
+
|
|
260
|
+
interface ColorFormatOptions extends BaseFormatOptions {
|
|
261
|
+
/** Se true, usa letras maiúsculas para hex */
|
|
262
|
+
uppercase?: boolean;
|
|
263
|
+
/** Se true, inclui alpha mesmo quando é 1 */
|
|
264
|
+
includeAlpha?: boolean;
|
|
265
|
+
/** Se true, usa formato compacto (shorthand hex, sem espaços em rgb) */
|
|
266
|
+
compact?: boolean;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Date Role - Types
|
|
271
|
+
*
|
|
272
|
+
* Tipos para representação de datas nas diferentes variantes.
|
|
273
|
+
*/
|
|
274
|
+
/**
|
|
275
|
+
* Tipo base para Date: timestamp em milissegundos (como JavaScript Date.getTime())
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* const ts: DateTimestamp = 1733425800000; // 2024-12-05T19:30:00.000Z
|
|
279
|
+
*/
|
|
280
|
+
type DateTimestamp = number;
|
|
281
|
+
/**
|
|
282
|
+
* Variante ISO 8601
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* "2024-12-05T19:30:00.000Z"
|
|
286
|
+
* "2024-12-05T19:30:00.000-03:00"
|
|
287
|
+
* "2024-12-05"
|
|
288
|
+
*/
|
|
289
|
+
type DateIso = string;
|
|
290
|
+
/**
|
|
291
|
+
* Variante Epoch (segundos desde 1970-01-01T00:00:00Z)
|
|
292
|
+
*
|
|
293
|
+
* @example
|
|
294
|
+
* 1733425800 // 2024-12-05T19:30:00Z
|
|
295
|
+
*/
|
|
296
|
+
type DateEpoch = number;
|
|
297
|
+
type DateVariant = "iso" | "timestamp" | "epoch";
|
|
298
|
+
|
|
299
|
+
interface DateFormatOptions extends BaseFormatOptions {
|
|
300
|
+
/** Estilo de data: "full", "long", "medium", "short" */
|
|
301
|
+
dateStyle?: "full" | "long" | "medium" | "short";
|
|
302
|
+
/** Estilo de hora: "full", "long", "medium", "short" */
|
|
303
|
+
timeStyle?: "full" | "long" | "medium" | "short";
|
|
304
|
+
/** Timezone (ex: "America/Sao_Paulo", "UTC") */
|
|
305
|
+
timeZone?: string;
|
|
306
|
+
/** Se true, mostra apenas data (sem hora) */
|
|
307
|
+
dateOnly?: boolean;
|
|
308
|
+
/** Se true, mostra apenas hora (sem data) */
|
|
309
|
+
timeOnly?: boolean;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export type { BaseFormatOptions as B, ConversionResult as C, DateTimestamp as D, FromBaseFn as F, MorpherSpec as M, ParsedVariantId as P, RoleSpec as R, TemperatureUnit as T, ValidationResult as V, VariantSpec as a, ReversibilityResult as b, DateFormatOptions as c, RGBA as d, ColorFormatOptions as e, ToBaseFn as f, MorpherFn as g, MorpherKey as h, FormatOptions as i, FormatSpec as j, FormatFn as k, ValidationRule as l, CastFn as m, CastResult as n, DateVariant as o, DateIso as p, DateEpoch as q, ColorVariant as r, ColorHex as s, ColorRgbObject as t, ColorRgbString as u, ColorHslObject as v, ColorHslString as w };
|