@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
package/dist/convert.js
ADDED
|
@@ -0,0 +1,2244 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/roles/area/constants.ts
|
|
4
|
+
var AREA_UNITS = {
|
|
5
|
+
// SI Units (todos exatos)
|
|
6
|
+
square_kilometer: {
|
|
7
|
+
factor: 1e6,
|
|
8
|
+
symbol: "km\xB2",
|
|
9
|
+
singular: "square kilometer",
|
|
10
|
+
plural: "square kilometers"
|
|
11
|
+
},
|
|
12
|
+
hectare: {
|
|
13
|
+
factor: 1e4,
|
|
14
|
+
symbol: "ha",
|
|
15
|
+
singular: "hectare",
|
|
16
|
+
plural: "hectares"
|
|
17
|
+
},
|
|
18
|
+
are: {
|
|
19
|
+
factor: 100,
|
|
20
|
+
symbol: "a",
|
|
21
|
+
singular: "are",
|
|
22
|
+
plural: "ares"
|
|
23
|
+
},
|
|
24
|
+
square_meter: {
|
|
25
|
+
factor: 1,
|
|
26
|
+
symbol: "m\xB2",
|
|
27
|
+
singular: "square meter",
|
|
28
|
+
plural: "square meters"
|
|
29
|
+
},
|
|
30
|
+
square_decimeter: {
|
|
31
|
+
factor: 0.01,
|
|
32
|
+
symbol: "dm\xB2",
|
|
33
|
+
singular: "square decimeter",
|
|
34
|
+
plural: "square decimeters"
|
|
35
|
+
},
|
|
36
|
+
square_centimeter: {
|
|
37
|
+
factor: 1e-4,
|
|
38
|
+
symbol: "cm\xB2",
|
|
39
|
+
singular: "square centimeter",
|
|
40
|
+
plural: "square centimeters"
|
|
41
|
+
},
|
|
42
|
+
square_millimeter: {
|
|
43
|
+
factor: 1e-6,
|
|
44
|
+
symbol: "mm\xB2",
|
|
45
|
+
singular: "square millimeter",
|
|
46
|
+
plural: "square millimeters"
|
|
47
|
+
},
|
|
48
|
+
// Imperial/US (todos exatos, derivados de comprimento² desde 1959)
|
|
49
|
+
square_mile: {
|
|
50
|
+
factor: 2589988110336e-6,
|
|
51
|
+
symbol: "mi\xB2",
|
|
52
|
+
singular: "square mile",
|
|
53
|
+
plural: "square miles"
|
|
54
|
+
},
|
|
55
|
+
acre: {
|
|
56
|
+
factor: 4046.8564224,
|
|
57
|
+
symbol: "ac",
|
|
58
|
+
singular: "acre",
|
|
59
|
+
plural: "acres"
|
|
60
|
+
},
|
|
61
|
+
square_yard: {
|
|
62
|
+
factor: 0.83612736,
|
|
63
|
+
symbol: "yd\xB2",
|
|
64
|
+
singular: "square yard",
|
|
65
|
+
plural: "square yards"
|
|
66
|
+
},
|
|
67
|
+
square_foot: {
|
|
68
|
+
factor: 0.09290304,
|
|
69
|
+
symbol: "ft\xB2",
|
|
70
|
+
singular: "square foot",
|
|
71
|
+
plural: "square feet"
|
|
72
|
+
},
|
|
73
|
+
square_inch: {
|
|
74
|
+
factor: 64516e-8,
|
|
75
|
+
symbol: "in\xB2",
|
|
76
|
+
singular: "square inch",
|
|
77
|
+
plural: "square inches"
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
Object.fromEntries(
|
|
81
|
+
Object.entries(AREA_UNITS).map(([unit, config]) => [
|
|
82
|
+
unit,
|
|
83
|
+
config.factor ?? 1
|
|
84
|
+
])
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
// src/roles/area/convert.ts
|
|
88
|
+
var AREA_BASE = "square_meter";
|
|
89
|
+
function toBaseArea(variant, value) {
|
|
90
|
+
const unit = AREA_UNITS[variant];
|
|
91
|
+
if (!unit) {
|
|
92
|
+
throw new Error(`Unknown area variant: ${variant}`);
|
|
93
|
+
}
|
|
94
|
+
return value * (unit.factor ?? 1);
|
|
95
|
+
}
|
|
96
|
+
function fromBaseArea(variant, baseValue) {
|
|
97
|
+
const unit = AREA_UNITS[variant];
|
|
98
|
+
if (!unit) {
|
|
99
|
+
throw new Error(`Unknown area variant: ${variant}`);
|
|
100
|
+
}
|
|
101
|
+
return baseValue / (unit.factor ?? 1);
|
|
102
|
+
}
|
|
103
|
+
function convertArea(from, to, value) {
|
|
104
|
+
if (from === to) {
|
|
105
|
+
return value;
|
|
106
|
+
}
|
|
107
|
+
const baseValue = toBaseArea(from, value);
|
|
108
|
+
return fromBaseArea(to, baseValue);
|
|
109
|
+
}
|
|
110
|
+
function tryConvertArea(from, to, value) {
|
|
111
|
+
try {
|
|
112
|
+
const result = convertArea(from, to, value);
|
|
113
|
+
return { ok: true, value: result };
|
|
114
|
+
} catch (err) {
|
|
115
|
+
return { ok: false, error: String(err) };
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
function hasAreaVariant(variant) {
|
|
119
|
+
return variant in AREA_UNITS;
|
|
120
|
+
}
|
|
121
|
+
function getAreaVariants() {
|
|
122
|
+
return Object.keys(AREA_UNITS);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// src/roles/length/constants.ts
|
|
126
|
+
var LENGTH_UNITS = {
|
|
127
|
+
// SI Units (todos exatos)
|
|
128
|
+
kilometer: {
|
|
129
|
+
factor: 1e3,
|
|
130
|
+
symbol: "km",
|
|
131
|
+
singular: "kilometer",
|
|
132
|
+
plural: "kilometers"
|
|
133
|
+
},
|
|
134
|
+
hectometer: {
|
|
135
|
+
factor: 100,
|
|
136
|
+
symbol: "hm",
|
|
137
|
+
singular: "hectometer",
|
|
138
|
+
plural: "hectometers"
|
|
139
|
+
},
|
|
140
|
+
decameter: {
|
|
141
|
+
factor: 10,
|
|
142
|
+
symbol: "dam",
|
|
143
|
+
singular: "decameter",
|
|
144
|
+
plural: "decameters"
|
|
145
|
+
},
|
|
146
|
+
meter: {
|
|
147
|
+
factor: 1,
|
|
148
|
+
symbol: "m",
|
|
149
|
+
singular: "meter",
|
|
150
|
+
plural: "meters"
|
|
151
|
+
},
|
|
152
|
+
decimeter: {
|
|
153
|
+
factor: 0.1,
|
|
154
|
+
symbol: "dm",
|
|
155
|
+
singular: "decimeter",
|
|
156
|
+
plural: "decimeters"
|
|
157
|
+
},
|
|
158
|
+
centimeter: {
|
|
159
|
+
factor: 0.01,
|
|
160
|
+
symbol: "cm",
|
|
161
|
+
singular: "centimeter",
|
|
162
|
+
plural: "centimeters"
|
|
163
|
+
},
|
|
164
|
+
millimeter: {
|
|
165
|
+
factor: 1e-3,
|
|
166
|
+
symbol: "mm",
|
|
167
|
+
singular: "millimeter",
|
|
168
|
+
plural: "millimeters"
|
|
169
|
+
},
|
|
170
|
+
micrometer: {
|
|
171
|
+
factor: 1e-6,
|
|
172
|
+
symbol: "\u03BCm",
|
|
173
|
+
singular: "micrometer",
|
|
174
|
+
plural: "micrometers"
|
|
175
|
+
},
|
|
176
|
+
nanometer: {
|
|
177
|
+
factor: 1e-9,
|
|
178
|
+
symbol: "nm",
|
|
179
|
+
singular: "nanometer",
|
|
180
|
+
plural: "nanometers"
|
|
181
|
+
},
|
|
182
|
+
// Imperial/US (exatos desde 1959)
|
|
183
|
+
inch: {
|
|
184
|
+
factor: 0.0254,
|
|
185
|
+
symbol: "in",
|
|
186
|
+
singular: "inch",
|
|
187
|
+
plural: "inches"
|
|
188
|
+
},
|
|
189
|
+
foot: {
|
|
190
|
+
factor: 0.3048,
|
|
191
|
+
symbol: "ft",
|
|
192
|
+
singular: "foot",
|
|
193
|
+
plural: "feet"
|
|
194
|
+
},
|
|
195
|
+
yard: {
|
|
196
|
+
factor: 0.9144,
|
|
197
|
+
symbol: "yd",
|
|
198
|
+
singular: "yard",
|
|
199
|
+
plural: "yards"
|
|
200
|
+
},
|
|
201
|
+
mile: {
|
|
202
|
+
factor: 1609.344,
|
|
203
|
+
symbol: "mi",
|
|
204
|
+
singular: "mile",
|
|
205
|
+
plural: "miles"
|
|
206
|
+
},
|
|
207
|
+
// Nautical (exato por definição internacional)
|
|
208
|
+
nautical_mile: {
|
|
209
|
+
factor: 1852,
|
|
210
|
+
symbol: "nmi",
|
|
211
|
+
singular: "nautical mile",
|
|
212
|
+
plural: "nautical miles"
|
|
213
|
+
},
|
|
214
|
+
// Other (derivados, portanto exatos)
|
|
215
|
+
fathom: {
|
|
216
|
+
factor: 1.8288,
|
|
217
|
+
// 6 feet
|
|
218
|
+
symbol: "ftm",
|
|
219
|
+
singular: "fathom",
|
|
220
|
+
plural: "fathoms"
|
|
221
|
+
},
|
|
222
|
+
furlong: {
|
|
223
|
+
factor: 201.168,
|
|
224
|
+
// 660 feet = 1/8 mile
|
|
225
|
+
symbol: "fur",
|
|
226
|
+
singular: "furlong",
|
|
227
|
+
plural: "furlongs"
|
|
228
|
+
},
|
|
229
|
+
league: {
|
|
230
|
+
factor: 4828.032,
|
|
231
|
+
// 3 miles
|
|
232
|
+
symbol: "lea",
|
|
233
|
+
singular: "league",
|
|
234
|
+
plural: "leagues"
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
Object.fromEntries(
|
|
238
|
+
Object.entries(LENGTH_UNITS).map(([unit, config]) => [
|
|
239
|
+
unit,
|
|
240
|
+
config.factor ?? 1
|
|
241
|
+
])
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
// src/roles/length/convert.ts
|
|
245
|
+
var LENGTH_BASE = "meter";
|
|
246
|
+
function toBaseLength(variant, value) {
|
|
247
|
+
const unit = LENGTH_UNITS[variant];
|
|
248
|
+
if (!unit) {
|
|
249
|
+
throw new Error(`Unknown length variant: ${variant}`);
|
|
250
|
+
}
|
|
251
|
+
return value * (unit.factor ?? 1);
|
|
252
|
+
}
|
|
253
|
+
function fromBaseLength(variant, baseValue) {
|
|
254
|
+
const unit = LENGTH_UNITS[variant];
|
|
255
|
+
if (!unit) {
|
|
256
|
+
throw new Error(`Unknown length variant: ${variant}`);
|
|
257
|
+
}
|
|
258
|
+
return baseValue / (unit.factor ?? 1);
|
|
259
|
+
}
|
|
260
|
+
function convertLength(from, to, value) {
|
|
261
|
+
if (from === to) {
|
|
262
|
+
return value;
|
|
263
|
+
}
|
|
264
|
+
const baseValue = toBaseLength(from, value);
|
|
265
|
+
return fromBaseLength(to, baseValue);
|
|
266
|
+
}
|
|
267
|
+
function tryConvertLength(from, to, value) {
|
|
268
|
+
try {
|
|
269
|
+
const result = convertLength(from, to, value);
|
|
270
|
+
return { ok: true, value: result };
|
|
271
|
+
} catch (err) {
|
|
272
|
+
return { ok: false, error: String(err) };
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
function hasLengthVariant(variant) {
|
|
276
|
+
return variant in LENGTH_UNITS;
|
|
277
|
+
}
|
|
278
|
+
function getLengthVariants() {
|
|
279
|
+
return Object.keys(LENGTH_UNITS);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// src/roles/mass/constants.ts
|
|
283
|
+
var MASS_UNITS = {
|
|
284
|
+
// ===========================================================================
|
|
285
|
+
// SI / Metric
|
|
286
|
+
// ===========================================================================
|
|
287
|
+
metric_ton: {
|
|
288
|
+
factor: 1e3,
|
|
289
|
+
symbol: "t",
|
|
290
|
+
singular: "metric ton",
|
|
291
|
+
plural: "metric tons"
|
|
292
|
+
},
|
|
293
|
+
kilogram: {
|
|
294
|
+
factor: 1,
|
|
295
|
+
symbol: "kg",
|
|
296
|
+
singular: "kilogram",
|
|
297
|
+
plural: "kilograms"
|
|
298
|
+
},
|
|
299
|
+
hectogram: {
|
|
300
|
+
factor: 0.1,
|
|
301
|
+
symbol: "hg",
|
|
302
|
+
singular: "hectogram",
|
|
303
|
+
plural: "hectograms"
|
|
304
|
+
},
|
|
305
|
+
decagram: {
|
|
306
|
+
factor: 0.01,
|
|
307
|
+
symbol: "dag",
|
|
308
|
+
singular: "decagram",
|
|
309
|
+
plural: "decagrams"
|
|
310
|
+
},
|
|
311
|
+
gram: {
|
|
312
|
+
factor: 1e-3,
|
|
313
|
+
symbol: "g",
|
|
314
|
+
singular: "gram",
|
|
315
|
+
plural: "grams"
|
|
316
|
+
},
|
|
317
|
+
decigram: {
|
|
318
|
+
factor: 1e-4,
|
|
319
|
+
symbol: "dg",
|
|
320
|
+
singular: "decigram",
|
|
321
|
+
plural: "decigrams"
|
|
322
|
+
},
|
|
323
|
+
centigram: {
|
|
324
|
+
factor: 1e-5,
|
|
325
|
+
symbol: "cg",
|
|
326
|
+
singular: "centigram",
|
|
327
|
+
plural: "centigrams"
|
|
328
|
+
},
|
|
329
|
+
milligram: {
|
|
330
|
+
factor: 1e-6,
|
|
331
|
+
symbol: "mg",
|
|
332
|
+
singular: "milligram",
|
|
333
|
+
plural: "milligrams"
|
|
334
|
+
},
|
|
335
|
+
microgram: {
|
|
336
|
+
factor: 1e-9,
|
|
337
|
+
symbol: "\u03BCg",
|
|
338
|
+
singular: "microgram",
|
|
339
|
+
plural: "micrograms"
|
|
340
|
+
},
|
|
341
|
+
// ===========================================================================
|
|
342
|
+
// Avoirdupois (US/UK) - Sistema padrão para peso comum
|
|
343
|
+
// ===========================================================================
|
|
344
|
+
long_ton: {
|
|
345
|
+
factor: 1016.0469088,
|
|
346
|
+
// 2240 lb (exato)
|
|
347
|
+
symbol: "long tn",
|
|
348
|
+
singular: "long ton",
|
|
349
|
+
plural: "long tons"
|
|
350
|
+
},
|
|
351
|
+
short_ton: {
|
|
352
|
+
factor: 907.18474,
|
|
353
|
+
// 2000 lb (exato)
|
|
354
|
+
symbol: "sh tn",
|
|
355
|
+
singular: "short ton",
|
|
356
|
+
plural: "short tons"
|
|
357
|
+
},
|
|
358
|
+
stone: {
|
|
359
|
+
factor: 6.35029318,
|
|
360
|
+
// 14 lb (exato)
|
|
361
|
+
symbol: "st",
|
|
362
|
+
singular: "stone",
|
|
363
|
+
plural: "stone"
|
|
364
|
+
// stone não muda no plural em inglês
|
|
365
|
+
},
|
|
366
|
+
pound: {
|
|
367
|
+
factor: 0.45359237,
|
|
368
|
+
// Exato desde 1959
|
|
369
|
+
symbol: "lb",
|
|
370
|
+
singular: "pound",
|
|
371
|
+
plural: "pounds"
|
|
372
|
+
},
|
|
373
|
+
ounce: {
|
|
374
|
+
factor: 0.028349523125,
|
|
375
|
+
// 1/16 lb (exato)
|
|
376
|
+
symbol: "oz",
|
|
377
|
+
singular: "ounce",
|
|
378
|
+
plural: "ounces"
|
|
379
|
+
},
|
|
380
|
+
dram: {
|
|
381
|
+
factor: 0.0017718451953125,
|
|
382
|
+
// 1/16 oz (exato)
|
|
383
|
+
symbol: "dr",
|
|
384
|
+
singular: "dram",
|
|
385
|
+
plural: "drams"
|
|
386
|
+
},
|
|
387
|
+
grain: {
|
|
388
|
+
factor: 6479891e-11,
|
|
389
|
+
// 1/7000 lb (exato)
|
|
390
|
+
symbol: "gr",
|
|
391
|
+
singular: "grain",
|
|
392
|
+
plural: "grains"
|
|
393
|
+
},
|
|
394
|
+
// ===========================================================================
|
|
395
|
+
// Troy (metais preciosos)
|
|
396
|
+
// ===========================================================================
|
|
397
|
+
troy_pound: {
|
|
398
|
+
factor: 0.3732417216,
|
|
399
|
+
// 12 troy oz (exato)
|
|
400
|
+
symbol: "lb t",
|
|
401
|
+
singular: "troy pound",
|
|
402
|
+
plural: "troy pounds"
|
|
403
|
+
},
|
|
404
|
+
troy_ounce: {
|
|
405
|
+
factor: 0.0311034768,
|
|
406
|
+
// 480 grains (exato)
|
|
407
|
+
symbol: "oz t",
|
|
408
|
+
singular: "troy ounce",
|
|
409
|
+
plural: "troy ounces"
|
|
410
|
+
},
|
|
411
|
+
pennyweight: {
|
|
412
|
+
factor: 0.00155517384,
|
|
413
|
+
// 1/20 troy oz (exato)
|
|
414
|
+
symbol: "dwt",
|
|
415
|
+
singular: "pennyweight",
|
|
416
|
+
plural: "pennyweights"
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
Object.fromEntries(
|
|
420
|
+
Object.entries(MASS_UNITS).map(([unit, config]) => [
|
|
421
|
+
unit,
|
|
422
|
+
config.factor ?? 1
|
|
423
|
+
])
|
|
424
|
+
);
|
|
425
|
+
|
|
426
|
+
// src/roles/mass/convert.ts
|
|
427
|
+
var MASS_BASE = "kilogram";
|
|
428
|
+
function toBaseMass(variant, value) {
|
|
429
|
+
const unit = MASS_UNITS[variant];
|
|
430
|
+
if (!unit) {
|
|
431
|
+
throw new Error(`Unknown mass variant: ${variant}`);
|
|
432
|
+
}
|
|
433
|
+
return value * (unit.factor ?? 1);
|
|
434
|
+
}
|
|
435
|
+
function fromBaseMass(variant, baseValue) {
|
|
436
|
+
const unit = MASS_UNITS[variant];
|
|
437
|
+
if (!unit) {
|
|
438
|
+
throw new Error(`Unknown mass variant: ${variant}`);
|
|
439
|
+
}
|
|
440
|
+
return baseValue / (unit.factor ?? 1);
|
|
441
|
+
}
|
|
442
|
+
function convertMass(from, to, value) {
|
|
443
|
+
if (from === to) {
|
|
444
|
+
return value;
|
|
445
|
+
}
|
|
446
|
+
const baseValue = toBaseMass(from, value);
|
|
447
|
+
return fromBaseMass(to, baseValue);
|
|
448
|
+
}
|
|
449
|
+
function tryConvertMass(from, to, value) {
|
|
450
|
+
try {
|
|
451
|
+
const result = convertMass(from, to, value);
|
|
452
|
+
return { ok: true, value: result };
|
|
453
|
+
} catch (err) {
|
|
454
|
+
return { ok: false, error: String(err) };
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
function hasMassVariant(variant) {
|
|
458
|
+
return variant in MASS_UNITS;
|
|
459
|
+
}
|
|
460
|
+
function getMassVariants() {
|
|
461
|
+
return Object.keys(MASS_UNITS);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// src/roles/temperature/constants.ts
|
|
465
|
+
var TEMPERATURE_UNITS = {
|
|
466
|
+
celsius: {
|
|
467
|
+
symbol: "\xB0C",
|
|
468
|
+
singular: "degree Celsius",
|
|
469
|
+
plural: "degrees Celsius",
|
|
470
|
+
toBase: (c) => c,
|
|
471
|
+
fromBase: (c) => c,
|
|
472
|
+
noSpace: true
|
|
473
|
+
// 25°C não 25 °C
|
|
474
|
+
},
|
|
475
|
+
fahrenheit: {
|
|
476
|
+
symbol: "\xB0F",
|
|
477
|
+
singular: "degree Fahrenheit",
|
|
478
|
+
plural: "degrees Fahrenheit",
|
|
479
|
+
toBase: (f) => (f - 32) * (5 / 9),
|
|
480
|
+
fromBase: (c) => c * (9 / 5) + 32,
|
|
481
|
+
noSpace: true
|
|
482
|
+
// 77°F não 77 °F
|
|
483
|
+
},
|
|
484
|
+
kelvin: {
|
|
485
|
+
symbol: "K",
|
|
486
|
+
singular: "kelvin",
|
|
487
|
+
plural: "kelvins",
|
|
488
|
+
toBase: (k) => k - 273.15,
|
|
489
|
+
fromBase: (c) => c + 273.15
|
|
490
|
+
// Kelvin usa espaço: "273 K" (convenção SI)
|
|
491
|
+
},
|
|
492
|
+
rankine: {
|
|
493
|
+
symbol: "\xB0R",
|
|
494
|
+
singular: "degree Rankine",
|
|
495
|
+
plural: "degrees Rankine",
|
|
496
|
+
toBase: (r) => (r - 491.67) * (5 / 9),
|
|
497
|
+
fromBase: (c) => (c + 273.15) * (9 / 5),
|
|
498
|
+
noSpace: true
|
|
499
|
+
// 500°R não 500 °R
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
|
|
503
|
+
// src/roles/temperature/convert.ts
|
|
504
|
+
var TEMPERATURE_BASE = "celsius";
|
|
505
|
+
function toBaseTemperature(variant, value) {
|
|
506
|
+
const unit = TEMPERATURE_UNITS[variant];
|
|
507
|
+
if (!unit) {
|
|
508
|
+
throw new Error(`Unknown temperature variant: ${variant}`);
|
|
509
|
+
}
|
|
510
|
+
if (unit.toBase) {
|
|
511
|
+
return unit.toBase(value);
|
|
512
|
+
}
|
|
513
|
+
return value * (unit.factor ?? 1);
|
|
514
|
+
}
|
|
515
|
+
function fromBaseTemperature(variant, baseValue) {
|
|
516
|
+
const unit = TEMPERATURE_UNITS[variant];
|
|
517
|
+
if (!unit) {
|
|
518
|
+
throw new Error(`Unknown temperature variant: ${variant}`);
|
|
519
|
+
}
|
|
520
|
+
if (unit.fromBase) {
|
|
521
|
+
return unit.fromBase(baseValue);
|
|
522
|
+
}
|
|
523
|
+
return baseValue / (unit.factor ?? 1);
|
|
524
|
+
}
|
|
525
|
+
function convertTemperature(from, to, value) {
|
|
526
|
+
if (from === to) {
|
|
527
|
+
return value;
|
|
528
|
+
}
|
|
529
|
+
const baseValue = toBaseTemperature(from, value);
|
|
530
|
+
return fromBaseTemperature(to, baseValue);
|
|
531
|
+
}
|
|
532
|
+
function tryConvertTemperature(from, to, value) {
|
|
533
|
+
try {
|
|
534
|
+
const result = convertTemperature(from, to, value);
|
|
535
|
+
return { ok: true, value: result };
|
|
536
|
+
} catch (err) {
|
|
537
|
+
return { ok: false, error: String(err) };
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
function hasTemperatureVariant(variant) {
|
|
541
|
+
return variant in TEMPERATURE_UNITS;
|
|
542
|
+
}
|
|
543
|
+
function getTemperatureVariants() {
|
|
544
|
+
return Object.keys(TEMPERATURE_UNITS);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
// src/roles/volume/constants.ts
|
|
548
|
+
var VOLUME_UNITS = {
|
|
549
|
+
// SI / Métrico
|
|
550
|
+
cubic_meter: {
|
|
551
|
+
factor: 1e3,
|
|
552
|
+
symbol: "m\xB3",
|
|
553
|
+
singular: "cubic meter",
|
|
554
|
+
plural: "cubic meters"
|
|
555
|
+
},
|
|
556
|
+
cubic_decimeter: {
|
|
557
|
+
factor: 1,
|
|
558
|
+
symbol: "dm\xB3",
|
|
559
|
+
singular: "cubic decimeter",
|
|
560
|
+
plural: "cubic decimeters"
|
|
561
|
+
},
|
|
562
|
+
cubic_centimeter: {
|
|
563
|
+
factor: 1e-3,
|
|
564
|
+
symbol: "cm\xB3",
|
|
565
|
+
singular: "cubic centimeter",
|
|
566
|
+
plural: "cubic centimeters"
|
|
567
|
+
},
|
|
568
|
+
cubic_millimeter: {
|
|
569
|
+
factor: 1e-6,
|
|
570
|
+
symbol: "mm\xB3",
|
|
571
|
+
singular: "cubic millimeter",
|
|
572
|
+
plural: "cubic millimeters"
|
|
573
|
+
},
|
|
574
|
+
hectoliter: {
|
|
575
|
+
factor: 100,
|
|
576
|
+
symbol: "hL",
|
|
577
|
+
singular: "hectoliter",
|
|
578
|
+
plural: "hectoliters"
|
|
579
|
+
},
|
|
580
|
+
decaliter: {
|
|
581
|
+
factor: 10,
|
|
582
|
+
symbol: "daL",
|
|
583
|
+
singular: "decaliter",
|
|
584
|
+
plural: "decaliters"
|
|
585
|
+
},
|
|
586
|
+
liter: {
|
|
587
|
+
factor: 1,
|
|
588
|
+
symbol: "L",
|
|
589
|
+
singular: "liter",
|
|
590
|
+
plural: "liters"
|
|
591
|
+
},
|
|
592
|
+
deciliter: {
|
|
593
|
+
factor: 0.1,
|
|
594
|
+
symbol: "dL",
|
|
595
|
+
singular: "deciliter",
|
|
596
|
+
plural: "deciliters"
|
|
597
|
+
},
|
|
598
|
+
centiliter: {
|
|
599
|
+
factor: 0.01,
|
|
600
|
+
symbol: "cL",
|
|
601
|
+
singular: "centiliter",
|
|
602
|
+
plural: "centiliters"
|
|
603
|
+
},
|
|
604
|
+
milliliter: {
|
|
605
|
+
factor: 1e-3,
|
|
606
|
+
symbol: "mL",
|
|
607
|
+
singular: "milliliter",
|
|
608
|
+
plural: "milliliters"
|
|
609
|
+
},
|
|
610
|
+
microliter: {
|
|
611
|
+
factor: 1e-6,
|
|
612
|
+
symbol: "\u03BCL",
|
|
613
|
+
singular: "microliter",
|
|
614
|
+
plural: "microliters"
|
|
615
|
+
},
|
|
616
|
+
// US Customary (baseado em 1 gallon = 231 in³ = 3.785411784 L)
|
|
617
|
+
gallon_us: {
|
|
618
|
+
factor: 3.785411784,
|
|
619
|
+
symbol: "gal",
|
|
620
|
+
singular: "gallon",
|
|
621
|
+
plural: "gallons"
|
|
622
|
+
},
|
|
623
|
+
quart_us: {
|
|
624
|
+
factor: 0.946352946,
|
|
625
|
+
// gallon/4
|
|
626
|
+
symbol: "qt",
|
|
627
|
+
singular: "quart",
|
|
628
|
+
plural: "quarts"
|
|
629
|
+
},
|
|
630
|
+
pint_us: {
|
|
631
|
+
factor: 0.473176473,
|
|
632
|
+
// gallon/8
|
|
633
|
+
symbol: "pt",
|
|
634
|
+
singular: "pint",
|
|
635
|
+
plural: "pints"
|
|
636
|
+
},
|
|
637
|
+
cup_us: {
|
|
638
|
+
factor: 0.2365882365,
|
|
639
|
+
// gallon/16
|
|
640
|
+
symbol: "cup",
|
|
641
|
+
singular: "cup",
|
|
642
|
+
plural: "cups"
|
|
643
|
+
},
|
|
644
|
+
fluid_ounce_us: {
|
|
645
|
+
factor: 0.0295735295625,
|
|
646
|
+
// gallon/128
|
|
647
|
+
symbol: "fl oz",
|
|
648
|
+
singular: "fluid ounce",
|
|
649
|
+
plural: "fluid ounces"
|
|
650
|
+
},
|
|
651
|
+
tablespoon_us: {
|
|
652
|
+
factor: 0.01478676478125,
|
|
653
|
+
// fl oz/2
|
|
654
|
+
symbol: "tbsp",
|
|
655
|
+
singular: "tablespoon",
|
|
656
|
+
plural: "tablespoons"
|
|
657
|
+
},
|
|
658
|
+
teaspoon_us: {
|
|
659
|
+
factor: 0.00492892159375,
|
|
660
|
+
// tbsp/3
|
|
661
|
+
symbol: "tsp",
|
|
662
|
+
singular: "teaspoon",
|
|
663
|
+
plural: "teaspoons"
|
|
664
|
+
},
|
|
665
|
+
// Imperial UK (1 gallon UK = 4.54609 L exato)
|
|
666
|
+
gallon_uk: {
|
|
667
|
+
factor: 4.54609,
|
|
668
|
+
symbol: "gal (UK)",
|
|
669
|
+
singular: "gallon (UK)",
|
|
670
|
+
plural: "gallons (UK)"
|
|
671
|
+
},
|
|
672
|
+
quart_uk: {
|
|
673
|
+
factor: 1.1365225,
|
|
674
|
+
// gallon/4
|
|
675
|
+
symbol: "qt (UK)",
|
|
676
|
+
singular: "quart (UK)",
|
|
677
|
+
plural: "quarts (UK)"
|
|
678
|
+
},
|
|
679
|
+
pint_uk: {
|
|
680
|
+
factor: 0.56826125,
|
|
681
|
+
// gallon/8
|
|
682
|
+
symbol: "pt (UK)",
|
|
683
|
+
singular: "pint (UK)",
|
|
684
|
+
plural: "pints (UK)"
|
|
685
|
+
},
|
|
686
|
+
fluid_ounce_uk: {
|
|
687
|
+
factor: 0.0284130625,
|
|
688
|
+
// gallon/160
|
|
689
|
+
symbol: "fl oz (UK)",
|
|
690
|
+
singular: "fluid ounce (UK)",
|
|
691
|
+
plural: "fluid ounces (UK)"
|
|
692
|
+
},
|
|
693
|
+
// Other
|
|
694
|
+
barrel_oil: {
|
|
695
|
+
factor: 158.987294928,
|
|
696
|
+
// 42 US gallons (petroleum)
|
|
697
|
+
symbol: "bbl",
|
|
698
|
+
singular: "barrel",
|
|
699
|
+
plural: "barrels"
|
|
700
|
+
},
|
|
701
|
+
cubic_inch: {
|
|
702
|
+
factor: 0.016387064,
|
|
703
|
+
// (0.0254)³ × 1000
|
|
704
|
+
symbol: "in\xB3",
|
|
705
|
+
singular: "cubic inch",
|
|
706
|
+
plural: "cubic inches"
|
|
707
|
+
},
|
|
708
|
+
cubic_foot: {
|
|
709
|
+
factor: 28.316846592,
|
|
710
|
+
// (0.3048)³ × 1000
|
|
711
|
+
symbol: "ft\xB3",
|
|
712
|
+
singular: "cubic foot",
|
|
713
|
+
plural: "cubic feet"
|
|
714
|
+
},
|
|
715
|
+
cubic_yard: {
|
|
716
|
+
factor: 764.554857984,
|
|
717
|
+
// (0.9144)³ × 1000
|
|
718
|
+
symbol: "yd\xB3",
|
|
719
|
+
singular: "cubic yard",
|
|
720
|
+
plural: "cubic yards"
|
|
721
|
+
}
|
|
722
|
+
};
|
|
723
|
+
Object.fromEntries(
|
|
724
|
+
Object.entries(VOLUME_UNITS).map(([unit, config]) => [
|
|
725
|
+
unit,
|
|
726
|
+
config.factor ?? 1
|
|
727
|
+
])
|
|
728
|
+
);
|
|
729
|
+
|
|
730
|
+
// src/roles/volume/convert.ts
|
|
731
|
+
var VOLUME_BASE = "liter";
|
|
732
|
+
function toBaseVolume(variant, value) {
|
|
733
|
+
const unit = VOLUME_UNITS[variant];
|
|
734
|
+
if (!unit) {
|
|
735
|
+
throw new Error(`Unknown volume variant: ${variant}`);
|
|
736
|
+
}
|
|
737
|
+
return value * (unit.factor ?? 1);
|
|
738
|
+
}
|
|
739
|
+
function fromBaseVolume(variant, baseValue) {
|
|
740
|
+
const unit = VOLUME_UNITS[variant];
|
|
741
|
+
if (!unit) {
|
|
742
|
+
throw new Error(`Unknown volume variant: ${variant}`);
|
|
743
|
+
}
|
|
744
|
+
return baseValue / (unit.factor ?? 1);
|
|
745
|
+
}
|
|
746
|
+
function convertVolume(from, to, value) {
|
|
747
|
+
if (from === to) {
|
|
748
|
+
return value;
|
|
749
|
+
}
|
|
750
|
+
const baseValue = toBaseVolume(from, value);
|
|
751
|
+
return fromBaseVolume(to, baseValue);
|
|
752
|
+
}
|
|
753
|
+
function tryConvertVolume(from, to, value) {
|
|
754
|
+
try {
|
|
755
|
+
const result = convertVolume(from, to, value);
|
|
756
|
+
return { ok: true, value: result };
|
|
757
|
+
} catch (err) {
|
|
758
|
+
return { ok: false, error: String(err) };
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
function hasVolumeVariant(variant) {
|
|
762
|
+
return variant in VOLUME_UNITS;
|
|
763
|
+
}
|
|
764
|
+
function getVolumeVariants() {
|
|
765
|
+
return Object.keys(VOLUME_UNITS);
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
// src/roles/speed/constants.ts
|
|
769
|
+
var SPEED_UNITS = {
|
|
770
|
+
// SI Units
|
|
771
|
+
meter_per_second: {
|
|
772
|
+
factor: 1,
|
|
773
|
+
symbol: "m/s",
|
|
774
|
+
singular: "meter per second",
|
|
775
|
+
plural: "meters per second"
|
|
776
|
+
},
|
|
777
|
+
kilometer_per_hour: {
|
|
778
|
+
factor: 1e3 / 3600,
|
|
779
|
+
// 0.277777...
|
|
780
|
+
symbol: "km/h",
|
|
781
|
+
singular: "kilometer per hour",
|
|
782
|
+
plural: "kilometers per hour"
|
|
783
|
+
},
|
|
784
|
+
// Imperial/US (exatos desde 1959)
|
|
785
|
+
mile_per_hour: {
|
|
786
|
+
factor: 0.44704,
|
|
787
|
+
// 1609.344 / 3600 (exato)
|
|
788
|
+
symbol: "mph",
|
|
789
|
+
singular: "mile per hour",
|
|
790
|
+
plural: "miles per hour"
|
|
791
|
+
},
|
|
792
|
+
foot_per_second: {
|
|
793
|
+
factor: 0.3048,
|
|
794
|
+
// exato
|
|
795
|
+
symbol: "ft/s",
|
|
796
|
+
singular: "foot per second",
|
|
797
|
+
plural: "feet per second"
|
|
798
|
+
},
|
|
799
|
+
// Nautical (exato)
|
|
800
|
+
knot: {
|
|
801
|
+
factor: 1852 / 3600,
|
|
802
|
+
// 0.514444...
|
|
803
|
+
symbol: "kn",
|
|
804
|
+
singular: "knot",
|
|
805
|
+
plural: "knots"
|
|
806
|
+
},
|
|
807
|
+
// Scientific
|
|
808
|
+
mach: {
|
|
809
|
+
factor: 340.29,
|
|
810
|
+
// velocidade do som ao nível do mar, 15°C (aproximado)
|
|
811
|
+
symbol: "Ma",
|
|
812
|
+
singular: "mach",
|
|
813
|
+
plural: "mach"
|
|
814
|
+
},
|
|
815
|
+
speed_of_light: {
|
|
816
|
+
factor: 299792458,
|
|
817
|
+
// exato por definição SI
|
|
818
|
+
symbol: "c",
|
|
819
|
+
singular: "speed of light",
|
|
820
|
+
plural: "speed of light"
|
|
821
|
+
}
|
|
822
|
+
};
|
|
823
|
+
Object.fromEntries(
|
|
824
|
+
Object.entries(SPEED_UNITS).map(([unit, config]) => [
|
|
825
|
+
unit,
|
|
826
|
+
config.factor ?? 1
|
|
827
|
+
])
|
|
828
|
+
);
|
|
829
|
+
|
|
830
|
+
// src/roles/speed/convert.ts
|
|
831
|
+
var SPEED_BASE = "meter_per_second";
|
|
832
|
+
function toBaseSpeed(variant, value) {
|
|
833
|
+
const unit = SPEED_UNITS[variant];
|
|
834
|
+
if (!unit) {
|
|
835
|
+
throw new Error(`Unknown speed variant: ${variant}`);
|
|
836
|
+
}
|
|
837
|
+
return value * (unit.factor ?? 1);
|
|
838
|
+
}
|
|
839
|
+
function fromBaseSpeed(variant, baseValue) {
|
|
840
|
+
const unit = SPEED_UNITS[variant];
|
|
841
|
+
if (!unit) {
|
|
842
|
+
throw new Error(`Unknown speed variant: ${variant}`);
|
|
843
|
+
}
|
|
844
|
+
return baseValue / (unit.factor ?? 1);
|
|
845
|
+
}
|
|
846
|
+
function convertSpeed(from, to, value) {
|
|
847
|
+
if (from === to) {
|
|
848
|
+
return value;
|
|
849
|
+
}
|
|
850
|
+
const baseValue = toBaseSpeed(from, value);
|
|
851
|
+
return fromBaseSpeed(to, baseValue);
|
|
852
|
+
}
|
|
853
|
+
function tryConvertSpeed(from, to, value) {
|
|
854
|
+
try {
|
|
855
|
+
const result = convertSpeed(from, to, value);
|
|
856
|
+
return { ok: true, value: result };
|
|
857
|
+
} catch (err) {
|
|
858
|
+
return { ok: false, error: String(err) };
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
function hasSpeedVariant(variant) {
|
|
862
|
+
return variant in SPEED_UNITS;
|
|
863
|
+
}
|
|
864
|
+
function getSpeedVariants() {
|
|
865
|
+
return Object.keys(SPEED_UNITS);
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
// src/roles/energy/constants.ts
|
|
869
|
+
var ENERGY_UNITS = {
|
|
870
|
+
// SI Units (todos exatos)
|
|
871
|
+
gigajoule: {
|
|
872
|
+
factor: 1e9,
|
|
873
|
+
symbol: "GJ",
|
|
874
|
+
singular: "gigajoule",
|
|
875
|
+
plural: "gigajoules"
|
|
876
|
+
},
|
|
877
|
+
megajoule: {
|
|
878
|
+
factor: 1e6,
|
|
879
|
+
symbol: "MJ",
|
|
880
|
+
singular: "megajoule",
|
|
881
|
+
plural: "megajoules"
|
|
882
|
+
},
|
|
883
|
+
kilojoule: {
|
|
884
|
+
factor: 1e3,
|
|
885
|
+
symbol: "kJ",
|
|
886
|
+
singular: "kilojoule",
|
|
887
|
+
plural: "kilojoules"
|
|
888
|
+
},
|
|
889
|
+
joule: {
|
|
890
|
+
factor: 1,
|
|
891
|
+
symbol: "J",
|
|
892
|
+
singular: "joule",
|
|
893
|
+
plural: "joules"
|
|
894
|
+
},
|
|
895
|
+
millijoule: {
|
|
896
|
+
factor: 1e-3,
|
|
897
|
+
symbol: "mJ",
|
|
898
|
+
singular: "millijoule",
|
|
899
|
+
plural: "millijoules"
|
|
900
|
+
},
|
|
901
|
+
// Calories (International Table - IT)
|
|
902
|
+
// 1 cal (IT) = 4.1868 J (exato por definição)
|
|
903
|
+
calorie: {
|
|
904
|
+
factor: 4.1868,
|
|
905
|
+
symbol: "cal",
|
|
906
|
+
singular: "calorie",
|
|
907
|
+
plural: "calories"
|
|
908
|
+
},
|
|
909
|
+
// 1 kcal = 1000 cal = 4186.8 J (= 1 "food Calorie")
|
|
910
|
+
kilocalorie: {
|
|
911
|
+
factor: 4186.8,
|
|
912
|
+
symbol: "kcal",
|
|
913
|
+
singular: "kilocalorie",
|
|
914
|
+
plural: "kilocalories"
|
|
915
|
+
},
|
|
916
|
+
// Watt-hour (exatos)
|
|
917
|
+
// 1 Wh = 1 W × 3600 s = 3600 J
|
|
918
|
+
watt_hour: {
|
|
919
|
+
factor: 3600,
|
|
920
|
+
symbol: "Wh",
|
|
921
|
+
singular: "watt-hour",
|
|
922
|
+
plural: "watt-hours"
|
|
923
|
+
},
|
|
924
|
+
kilowatt_hour: {
|
|
925
|
+
factor: 36e5,
|
|
926
|
+
symbol: "kWh",
|
|
927
|
+
singular: "kilowatt-hour",
|
|
928
|
+
plural: "kilowatt-hours"
|
|
929
|
+
},
|
|
930
|
+
megawatt_hour: {
|
|
931
|
+
factor: 36e8,
|
|
932
|
+
symbol: "MWh",
|
|
933
|
+
singular: "megawatt-hour",
|
|
934
|
+
plural: "megawatt-hours"
|
|
935
|
+
},
|
|
936
|
+
gigawatt_hour: {
|
|
937
|
+
factor: 36e11,
|
|
938
|
+
symbol: "GWh",
|
|
939
|
+
singular: "gigawatt-hour",
|
|
940
|
+
plural: "gigawatt-hours"
|
|
941
|
+
},
|
|
942
|
+
// BTU (International Table)
|
|
943
|
+
// 1 BTU (IT) = 1055.05585262 J (definição)
|
|
944
|
+
btu: {
|
|
945
|
+
factor: 1055.05585262,
|
|
946
|
+
symbol: "BTU",
|
|
947
|
+
singular: "BTU",
|
|
948
|
+
plural: "BTUs"
|
|
949
|
+
},
|
|
950
|
+
// 1 therm = 100,000 BTU (IT)
|
|
951
|
+
therm: {
|
|
952
|
+
factor: 105505585262e-3,
|
|
953
|
+
symbol: "thm",
|
|
954
|
+
singular: "therm",
|
|
955
|
+
plural: "therms"
|
|
956
|
+
},
|
|
957
|
+
// Scientific
|
|
958
|
+
// 1 eV = 1.602176634e-19 J (SI 2019, exato por definição)
|
|
959
|
+
electronvolt: {
|
|
960
|
+
factor: 1602176634e-28,
|
|
961
|
+
symbol: "eV",
|
|
962
|
+
singular: "electronvolt",
|
|
963
|
+
plural: "electronvolts"
|
|
964
|
+
},
|
|
965
|
+
kiloelectronvolt: {
|
|
966
|
+
factor: 1602176634e-25,
|
|
967
|
+
symbol: "keV",
|
|
968
|
+
singular: "kiloelectronvolt",
|
|
969
|
+
plural: "kiloelectronvolts"
|
|
970
|
+
},
|
|
971
|
+
megaelectronvolt: {
|
|
972
|
+
factor: 1602176634e-22,
|
|
973
|
+
symbol: "MeV",
|
|
974
|
+
singular: "megaelectronvolt",
|
|
975
|
+
plural: "megaelectronvolts"
|
|
976
|
+
},
|
|
977
|
+
// 1 erg = 1e-7 J (CGS, exato)
|
|
978
|
+
erg: {
|
|
979
|
+
factor: 1e-7,
|
|
980
|
+
symbol: "erg",
|
|
981
|
+
singular: "erg",
|
|
982
|
+
plural: "ergs"
|
|
983
|
+
},
|
|
984
|
+
// Mechanical
|
|
985
|
+
// 1 ft⋅lbf = 1.3558179483314004 J (derivado de foot e pound-force)
|
|
986
|
+
foot_pound: {
|
|
987
|
+
factor: 1.3558179483314003,
|
|
988
|
+
symbol: "ft\u22C5lbf",
|
|
989
|
+
singular: "foot-pound",
|
|
990
|
+
plural: "foot-pounds"
|
|
991
|
+
}
|
|
992
|
+
};
|
|
993
|
+
Object.fromEntries(
|
|
994
|
+
Object.entries(ENERGY_UNITS).map(([unit, config]) => [
|
|
995
|
+
unit,
|
|
996
|
+
config.factor ?? 1
|
|
997
|
+
])
|
|
998
|
+
);
|
|
999
|
+
|
|
1000
|
+
// src/roles/energy/convert.ts
|
|
1001
|
+
var ENERGY_BASE = "joule";
|
|
1002
|
+
function toBaseEnergy(variant, value) {
|
|
1003
|
+
const unit = ENERGY_UNITS[variant];
|
|
1004
|
+
if (!unit) {
|
|
1005
|
+
throw new Error(`Unknown energy variant: ${variant}`);
|
|
1006
|
+
}
|
|
1007
|
+
return value * (unit.factor ?? 1);
|
|
1008
|
+
}
|
|
1009
|
+
function fromBaseEnergy(variant, baseValue) {
|
|
1010
|
+
const unit = ENERGY_UNITS[variant];
|
|
1011
|
+
if (!unit) {
|
|
1012
|
+
throw new Error(`Unknown energy variant: ${variant}`);
|
|
1013
|
+
}
|
|
1014
|
+
return baseValue / (unit.factor ?? 1);
|
|
1015
|
+
}
|
|
1016
|
+
function convertEnergy(from, to, value) {
|
|
1017
|
+
if (from === to) {
|
|
1018
|
+
return value;
|
|
1019
|
+
}
|
|
1020
|
+
const baseValue = toBaseEnergy(from, value);
|
|
1021
|
+
return fromBaseEnergy(to, baseValue);
|
|
1022
|
+
}
|
|
1023
|
+
function tryConvertEnergy(from, to, value) {
|
|
1024
|
+
try {
|
|
1025
|
+
const result = convertEnergy(from, to, value);
|
|
1026
|
+
return { ok: true, value: result };
|
|
1027
|
+
} catch (err) {
|
|
1028
|
+
return { ok: false, error: String(err) };
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
function hasEnergyVariant(variant) {
|
|
1032
|
+
return variant in ENERGY_UNITS;
|
|
1033
|
+
}
|
|
1034
|
+
function getEnergyVariants() {
|
|
1035
|
+
return Object.keys(ENERGY_UNITS);
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
// src/roles/power/constants.ts
|
|
1039
|
+
var POWER_UNITS = {
|
|
1040
|
+
// SI Units (todos exatos)
|
|
1041
|
+
gigawatt: {
|
|
1042
|
+
factor: 1e9,
|
|
1043
|
+
symbol: "GW",
|
|
1044
|
+
singular: "gigawatt",
|
|
1045
|
+
plural: "gigawatts"
|
|
1046
|
+
},
|
|
1047
|
+
megawatt: {
|
|
1048
|
+
factor: 1e6,
|
|
1049
|
+
symbol: "MW",
|
|
1050
|
+
singular: "megawatt",
|
|
1051
|
+
plural: "megawatts"
|
|
1052
|
+
},
|
|
1053
|
+
kilowatt: {
|
|
1054
|
+
factor: 1e3,
|
|
1055
|
+
symbol: "kW",
|
|
1056
|
+
singular: "kilowatt",
|
|
1057
|
+
plural: "kilowatts"
|
|
1058
|
+
},
|
|
1059
|
+
watt: {
|
|
1060
|
+
factor: 1,
|
|
1061
|
+
symbol: "W",
|
|
1062
|
+
singular: "watt",
|
|
1063
|
+
plural: "watts"
|
|
1064
|
+
},
|
|
1065
|
+
milliwatt: {
|
|
1066
|
+
factor: 1e-3,
|
|
1067
|
+
symbol: "mW",
|
|
1068
|
+
singular: "milliwatt",
|
|
1069
|
+
plural: "milliwatts"
|
|
1070
|
+
},
|
|
1071
|
+
microwatt: {
|
|
1072
|
+
factor: 1e-6,
|
|
1073
|
+
symbol: "\u03BCW",
|
|
1074
|
+
singular: "microwatt",
|
|
1075
|
+
plural: "microwatts"
|
|
1076
|
+
},
|
|
1077
|
+
// Horsepower variants
|
|
1078
|
+
// Mechanical (imperial): 550 ft⋅lbf/s
|
|
1079
|
+
// = 550 × 0.3048 m × 4.4482216152605 N / s = 745.69987158227 W
|
|
1080
|
+
horsepower_mechanical: {
|
|
1081
|
+
factor: 745.69987158227,
|
|
1082
|
+
symbol: "hp",
|
|
1083
|
+
singular: "horsepower",
|
|
1084
|
+
plural: "horsepower"
|
|
1085
|
+
},
|
|
1086
|
+
// Metric (PS, CV, pk): 75 kgf⋅m/s = 75 × 9.80665 W = 735.49875 W (exato)
|
|
1087
|
+
horsepower_metric: {
|
|
1088
|
+
factor: 735.49875,
|
|
1089
|
+
symbol: "PS",
|
|
1090
|
+
singular: "metric horsepower",
|
|
1091
|
+
plural: "metric horsepower"
|
|
1092
|
+
},
|
|
1093
|
+
// Electrical: 746 W (exato por definição)
|
|
1094
|
+
horsepower_electric: {
|
|
1095
|
+
factor: 746,
|
|
1096
|
+
symbol: "hp(E)",
|
|
1097
|
+
singular: "electric horsepower",
|
|
1098
|
+
plural: "electric horsepower"
|
|
1099
|
+
},
|
|
1100
|
+
// Boiler: 33,475 BTU/h = 9809.5 W
|
|
1101
|
+
horsepower_boiler: {
|
|
1102
|
+
factor: 9809.5,
|
|
1103
|
+
symbol: "hp(S)",
|
|
1104
|
+
singular: "boiler horsepower",
|
|
1105
|
+
plural: "boiler horsepower"
|
|
1106
|
+
},
|
|
1107
|
+
// BTU-based
|
|
1108
|
+
// 1 BTU/h = 1055.05585262 J / 3600 s = 0.29307107017222 W
|
|
1109
|
+
btu_per_hour: {
|
|
1110
|
+
factor: 0.29307107017222,
|
|
1111
|
+
symbol: "BTU/h",
|
|
1112
|
+
singular: "BTU per hour",
|
|
1113
|
+
plural: "BTUs per hour"
|
|
1114
|
+
},
|
|
1115
|
+
// 1 BTU/s = 1055.05585262 W
|
|
1116
|
+
btu_per_second: {
|
|
1117
|
+
factor: 1055.05585262,
|
|
1118
|
+
symbol: "BTU/s",
|
|
1119
|
+
singular: "BTU per second",
|
|
1120
|
+
plural: "BTUs per second"
|
|
1121
|
+
},
|
|
1122
|
+
// Other
|
|
1123
|
+
// 1 ton of refrigeration = 12000 BTU/h = 3516.8528420667 W
|
|
1124
|
+
ton_of_refrigeration: {
|
|
1125
|
+
factor: 3516.8528420667,
|
|
1126
|
+
symbol: "TR",
|
|
1127
|
+
singular: "ton of refrigeration",
|
|
1128
|
+
plural: "tons of refrigeration"
|
|
1129
|
+
},
|
|
1130
|
+
// 1 ft⋅lbf/s = 1.3558179483314004 W
|
|
1131
|
+
foot_pound_per_second: {
|
|
1132
|
+
factor: 1.3558179483314003,
|
|
1133
|
+
symbol: "ft\u22C5lbf/s",
|
|
1134
|
+
singular: "foot-pound per second",
|
|
1135
|
+
plural: "foot-pounds per second"
|
|
1136
|
+
},
|
|
1137
|
+
// 1 cal/s = 4.1868 W
|
|
1138
|
+
calorie_per_second: {
|
|
1139
|
+
factor: 4.1868,
|
|
1140
|
+
symbol: "cal/s",
|
|
1141
|
+
singular: "calorie per second",
|
|
1142
|
+
plural: "calories per second"
|
|
1143
|
+
},
|
|
1144
|
+
// 1 kcal/h = 4186.8 / 3600 = 1.163 W
|
|
1145
|
+
kilocalorie_per_hour: {
|
|
1146
|
+
factor: 1.163,
|
|
1147
|
+
symbol: "kcal/h",
|
|
1148
|
+
singular: "kilocalorie per hour",
|
|
1149
|
+
plural: "kilocalories per hour"
|
|
1150
|
+
}
|
|
1151
|
+
};
|
|
1152
|
+
Object.fromEntries(
|
|
1153
|
+
Object.entries(POWER_UNITS).map(([unit, config]) => [
|
|
1154
|
+
unit,
|
|
1155
|
+
config.factor ?? 1
|
|
1156
|
+
])
|
|
1157
|
+
);
|
|
1158
|
+
|
|
1159
|
+
// src/roles/power/convert.ts
|
|
1160
|
+
var POWER_BASE = "watt";
|
|
1161
|
+
function toBasePower(variant, value) {
|
|
1162
|
+
const config = POWER_UNITS[variant];
|
|
1163
|
+
if (!config) {
|
|
1164
|
+
throw new Error(`Unknown power unit: ${variant}`);
|
|
1165
|
+
}
|
|
1166
|
+
return value * (config.factor ?? 1);
|
|
1167
|
+
}
|
|
1168
|
+
function fromBasePower(variant, baseValue) {
|
|
1169
|
+
const config = POWER_UNITS[variant];
|
|
1170
|
+
if (!config) {
|
|
1171
|
+
throw new Error(`Unknown power unit: ${variant}`);
|
|
1172
|
+
}
|
|
1173
|
+
return baseValue / (config.factor ?? 1);
|
|
1174
|
+
}
|
|
1175
|
+
function convertPower(from, to, value) {
|
|
1176
|
+
if (from === to) {
|
|
1177
|
+
return value;
|
|
1178
|
+
}
|
|
1179
|
+
const baseValue = toBasePower(from, value);
|
|
1180
|
+
return fromBasePower(to, baseValue);
|
|
1181
|
+
}
|
|
1182
|
+
function tryConvertPower(from, to, value) {
|
|
1183
|
+
try {
|
|
1184
|
+
return { ok: true, value: convertPower(from, to, value) };
|
|
1185
|
+
} catch (e) {
|
|
1186
|
+
return { ok: false, error: e.message };
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
function hasPowerVariant(variant) {
|
|
1190
|
+
return variant in POWER_UNITS;
|
|
1191
|
+
}
|
|
1192
|
+
function getPowerVariants() {
|
|
1193
|
+
return Object.keys(POWER_UNITS);
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
// src/roles/pressure/constants.ts
|
|
1197
|
+
var PRESSURE_UNITS = {
|
|
1198
|
+
// SI Units (todos exatos)
|
|
1199
|
+
megapascal: {
|
|
1200
|
+
factor: 1e6,
|
|
1201
|
+
symbol: "MPa",
|
|
1202
|
+
singular: "megapascal",
|
|
1203
|
+
plural: "megapascals"
|
|
1204
|
+
},
|
|
1205
|
+
kilopascal: {
|
|
1206
|
+
factor: 1e3,
|
|
1207
|
+
symbol: "kPa",
|
|
1208
|
+
singular: "kilopascal",
|
|
1209
|
+
plural: "kilopascals"
|
|
1210
|
+
},
|
|
1211
|
+
hectopascal: {
|
|
1212
|
+
factor: 100,
|
|
1213
|
+
symbol: "hPa",
|
|
1214
|
+
singular: "hectopascal",
|
|
1215
|
+
plural: "hectopascals"
|
|
1216
|
+
},
|
|
1217
|
+
pascal: {
|
|
1218
|
+
factor: 1,
|
|
1219
|
+
symbol: "Pa",
|
|
1220
|
+
singular: "pascal",
|
|
1221
|
+
plural: "pascals"
|
|
1222
|
+
},
|
|
1223
|
+
// Bar (exatos por definição)
|
|
1224
|
+
bar: {
|
|
1225
|
+
factor: 1e5,
|
|
1226
|
+
symbol: "bar",
|
|
1227
|
+
singular: "bar",
|
|
1228
|
+
plural: "bar"
|
|
1229
|
+
},
|
|
1230
|
+
millibar: {
|
|
1231
|
+
factor: 100,
|
|
1232
|
+
// = 1 hPa
|
|
1233
|
+
symbol: "mbar",
|
|
1234
|
+
singular: "millibar",
|
|
1235
|
+
plural: "millibar"
|
|
1236
|
+
},
|
|
1237
|
+
// Atmosphere (exato, CGPM 1954)
|
|
1238
|
+
atmosphere: {
|
|
1239
|
+
factor: 101325,
|
|
1240
|
+
symbol: "atm",
|
|
1241
|
+
singular: "atmosphere",
|
|
1242
|
+
plural: "atmospheres"
|
|
1243
|
+
},
|
|
1244
|
+
// Mercury column
|
|
1245
|
+
torr: {
|
|
1246
|
+
factor: 101325 / 760,
|
|
1247
|
+
// ≈ 133.322368421
|
|
1248
|
+
symbol: "Torr",
|
|
1249
|
+
singular: "torr",
|
|
1250
|
+
plural: "torr"
|
|
1251
|
+
},
|
|
1252
|
+
mmhg: {
|
|
1253
|
+
factor: 133.322387415,
|
|
1254
|
+
// Convenção NIST
|
|
1255
|
+
symbol: "mmHg",
|
|
1256
|
+
singular: "millimeter of mercury",
|
|
1257
|
+
plural: "millimeters of mercury"
|
|
1258
|
+
},
|
|
1259
|
+
inhg: {
|
|
1260
|
+
factor: 3386.389,
|
|
1261
|
+
// Polegadas de mercúrio
|
|
1262
|
+
symbol: "inHg",
|
|
1263
|
+
singular: "inch of mercury",
|
|
1264
|
+
plural: "inches of mercury"
|
|
1265
|
+
},
|
|
1266
|
+
// Imperial (derivados de lb/in²)
|
|
1267
|
+
psi: {
|
|
1268
|
+
factor: 6894.757293168,
|
|
1269
|
+
// 1 lbf/in²
|
|
1270
|
+
symbol: "psi",
|
|
1271
|
+
singular: "pound per square inch",
|
|
1272
|
+
plural: "pounds per square inch"
|
|
1273
|
+
},
|
|
1274
|
+
ksi: {
|
|
1275
|
+
factor: 6894757293168e-6,
|
|
1276
|
+
// 1000 psi
|
|
1277
|
+
symbol: "ksi",
|
|
1278
|
+
singular: "kilopound per square inch",
|
|
1279
|
+
plural: "kilopounds per square inch"
|
|
1280
|
+
},
|
|
1281
|
+
// Water column
|
|
1282
|
+
cmh2o: {
|
|
1283
|
+
factor: 98.0665,
|
|
1284
|
+
// cm de água a 4°C
|
|
1285
|
+
symbol: "cmH\u2082O",
|
|
1286
|
+
singular: "centimeter of water",
|
|
1287
|
+
plural: "centimeters of water"
|
|
1288
|
+
},
|
|
1289
|
+
inh2o: {
|
|
1290
|
+
factor: 249.08891,
|
|
1291
|
+
// polegadas de água
|
|
1292
|
+
symbol: "inH\u2082O",
|
|
1293
|
+
singular: "inch of water",
|
|
1294
|
+
plural: "inches of water"
|
|
1295
|
+
}
|
|
1296
|
+
};
|
|
1297
|
+
Object.fromEntries(
|
|
1298
|
+
Object.entries(PRESSURE_UNITS).map(([unit, config]) => [
|
|
1299
|
+
unit,
|
|
1300
|
+
config.factor ?? 1
|
|
1301
|
+
])
|
|
1302
|
+
);
|
|
1303
|
+
|
|
1304
|
+
// src/roles/pressure/convert.ts
|
|
1305
|
+
var PRESSURE_BASE = "pascal";
|
|
1306
|
+
function toBasePressure(variant, value) {
|
|
1307
|
+
const config = PRESSURE_UNITS[variant];
|
|
1308
|
+
if (!config) {
|
|
1309
|
+
throw new Error(`Unknown pressure unit: ${variant}`);
|
|
1310
|
+
}
|
|
1311
|
+
return value * (config.factor ?? 1);
|
|
1312
|
+
}
|
|
1313
|
+
function fromBasePressure(variant, baseValue) {
|
|
1314
|
+
const config = PRESSURE_UNITS[variant];
|
|
1315
|
+
if (!config) {
|
|
1316
|
+
throw new Error(`Unknown pressure unit: ${variant}`);
|
|
1317
|
+
}
|
|
1318
|
+
return baseValue / (config.factor ?? 1);
|
|
1319
|
+
}
|
|
1320
|
+
function convertPressure(from, to, value) {
|
|
1321
|
+
if (from === to) {
|
|
1322
|
+
return value;
|
|
1323
|
+
}
|
|
1324
|
+
const baseValue = toBasePressure(from, value);
|
|
1325
|
+
return fromBasePressure(to, baseValue);
|
|
1326
|
+
}
|
|
1327
|
+
function tryConvertPressure(from, to, value) {
|
|
1328
|
+
try {
|
|
1329
|
+
return { ok: true, value: convertPressure(from, to, value) };
|
|
1330
|
+
} catch (e) {
|
|
1331
|
+
return { ok: false, error: e.message };
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
function hasPressureVariant(variant) {
|
|
1335
|
+
return variant in PRESSURE_UNITS;
|
|
1336
|
+
}
|
|
1337
|
+
function getPressureVariants() {
|
|
1338
|
+
return Object.keys(PRESSURE_UNITS);
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
// src/roles/frequency/constants.ts
|
|
1342
|
+
var FREQUENCY_UNITS = {
|
|
1343
|
+
// SI Units (todos exatos)
|
|
1344
|
+
terahertz: {
|
|
1345
|
+
factor: 1e12,
|
|
1346
|
+
symbol: "THz",
|
|
1347
|
+
singular: "terahertz",
|
|
1348
|
+
plural: "terahertz"
|
|
1349
|
+
},
|
|
1350
|
+
gigahertz: {
|
|
1351
|
+
factor: 1e9,
|
|
1352
|
+
symbol: "GHz",
|
|
1353
|
+
singular: "gigahertz",
|
|
1354
|
+
plural: "gigahertz"
|
|
1355
|
+
},
|
|
1356
|
+
megahertz: {
|
|
1357
|
+
factor: 1e6,
|
|
1358
|
+
symbol: "MHz",
|
|
1359
|
+
singular: "megahertz",
|
|
1360
|
+
plural: "megahertz"
|
|
1361
|
+
},
|
|
1362
|
+
kilohertz: {
|
|
1363
|
+
factor: 1e3,
|
|
1364
|
+
symbol: "kHz",
|
|
1365
|
+
singular: "kilohertz",
|
|
1366
|
+
plural: "kilohertz"
|
|
1367
|
+
},
|
|
1368
|
+
hertz: {
|
|
1369
|
+
factor: 1,
|
|
1370
|
+
symbol: "Hz",
|
|
1371
|
+
singular: "hertz",
|
|
1372
|
+
plural: "hertz"
|
|
1373
|
+
},
|
|
1374
|
+
millihertz: {
|
|
1375
|
+
factor: 1e-3,
|
|
1376
|
+
symbol: "mHz",
|
|
1377
|
+
singular: "millihertz",
|
|
1378
|
+
plural: "millihertz"
|
|
1379
|
+
},
|
|
1380
|
+
microhertz: {
|
|
1381
|
+
factor: 1e-6,
|
|
1382
|
+
symbol: "\u03BCHz",
|
|
1383
|
+
singular: "microhertz",
|
|
1384
|
+
plural: "microhertz"
|
|
1385
|
+
},
|
|
1386
|
+
// Other units
|
|
1387
|
+
rpm: {
|
|
1388
|
+
factor: 1 / 60,
|
|
1389
|
+
// 1 RPM = 1/60 Hz
|
|
1390
|
+
symbol: "rpm",
|
|
1391
|
+
singular: "revolution per minute",
|
|
1392
|
+
plural: "revolutions per minute"
|
|
1393
|
+
},
|
|
1394
|
+
bpm: {
|
|
1395
|
+
factor: 1 / 60,
|
|
1396
|
+
// 1 BPM = 1/60 Hz
|
|
1397
|
+
symbol: "bpm",
|
|
1398
|
+
singular: "beat per minute",
|
|
1399
|
+
plural: "beats per minute"
|
|
1400
|
+
},
|
|
1401
|
+
radians_per_second: {
|
|
1402
|
+
factor: 1 / (2 * Math.PI),
|
|
1403
|
+
// 1 rad/s = 1/(2π) Hz ≈ 0.159154943
|
|
1404
|
+
symbol: "rad/s",
|
|
1405
|
+
singular: "radian per second",
|
|
1406
|
+
plural: "radians per second"
|
|
1407
|
+
},
|
|
1408
|
+
cycles_per_minute: {
|
|
1409
|
+
factor: 1 / 60,
|
|
1410
|
+
// same as RPM
|
|
1411
|
+
symbol: "cpm",
|
|
1412
|
+
singular: "cycle per minute",
|
|
1413
|
+
plural: "cycles per minute"
|
|
1414
|
+
}
|
|
1415
|
+
};
|
|
1416
|
+
Object.fromEntries(
|
|
1417
|
+
Object.entries(FREQUENCY_UNITS).map(([unit, config]) => [
|
|
1418
|
+
unit,
|
|
1419
|
+
config.factor ?? 1
|
|
1420
|
+
])
|
|
1421
|
+
);
|
|
1422
|
+
|
|
1423
|
+
// src/roles/frequency/convert.ts
|
|
1424
|
+
var FREQUENCY_BASE = "hertz";
|
|
1425
|
+
function toBaseFrequency(variant, value) {
|
|
1426
|
+
const config = FREQUENCY_UNITS[variant];
|
|
1427
|
+
if (!config) {
|
|
1428
|
+
throw new Error(`Unknown frequency unit: ${variant}`);
|
|
1429
|
+
}
|
|
1430
|
+
return value * (config.factor ?? 1);
|
|
1431
|
+
}
|
|
1432
|
+
function fromBaseFrequency(variant, baseValue) {
|
|
1433
|
+
const config = FREQUENCY_UNITS[variant];
|
|
1434
|
+
if (!config) {
|
|
1435
|
+
throw new Error(`Unknown frequency unit: ${variant}`);
|
|
1436
|
+
}
|
|
1437
|
+
return baseValue / (config.factor ?? 1);
|
|
1438
|
+
}
|
|
1439
|
+
function convertFrequency(from, to, value) {
|
|
1440
|
+
if (from === to) {
|
|
1441
|
+
return value;
|
|
1442
|
+
}
|
|
1443
|
+
const baseValue = toBaseFrequency(from, value);
|
|
1444
|
+
return fromBaseFrequency(to, baseValue);
|
|
1445
|
+
}
|
|
1446
|
+
function tryConvertFrequency(from, to, value) {
|
|
1447
|
+
try {
|
|
1448
|
+
return { ok: true, value: convertFrequency(from, to, value) };
|
|
1449
|
+
} catch (e) {
|
|
1450
|
+
return { ok: false, error: e.message };
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
function hasFrequencyVariant(variant) {
|
|
1454
|
+
return variant in FREQUENCY_UNITS;
|
|
1455
|
+
}
|
|
1456
|
+
function getFrequencyVariants() {
|
|
1457
|
+
return Object.keys(FREQUENCY_UNITS);
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
// src/roles/angle/constants.ts
|
|
1461
|
+
var DEGREES_PER_RADIAN = 180 / Math.PI;
|
|
1462
|
+
var DEGREES_PER_MILLIRADIAN = 180 / (1e3 * Math.PI);
|
|
1463
|
+
var ANGLE_UNITS = {
|
|
1464
|
+
// Volta completa
|
|
1465
|
+
turn: {
|
|
1466
|
+
factor: 360,
|
|
1467
|
+
symbol: "tr",
|
|
1468
|
+
singular: "turn",
|
|
1469
|
+
plural: "turns"
|
|
1470
|
+
},
|
|
1471
|
+
// Base
|
|
1472
|
+
degree: {
|
|
1473
|
+
factor: 1,
|
|
1474
|
+
symbol: "\xB0",
|
|
1475
|
+
singular: "degree",
|
|
1476
|
+
plural: "degrees",
|
|
1477
|
+
noSpace: true
|
|
1478
|
+
// 45° não 45 °
|
|
1479
|
+
},
|
|
1480
|
+
// Subdivisões do grau
|
|
1481
|
+
arcminute: {
|
|
1482
|
+
factor: 1 / 60,
|
|
1483
|
+
// 0.016666...
|
|
1484
|
+
symbol: "\u2032",
|
|
1485
|
+
singular: "arcminute",
|
|
1486
|
+
plural: "arcminutes",
|
|
1487
|
+
noSpace: true
|
|
1488
|
+
// 30′ não 30 ′
|
|
1489
|
+
},
|
|
1490
|
+
arcsecond: {
|
|
1491
|
+
factor: 1 / 3600,
|
|
1492
|
+
// 0.000277...
|
|
1493
|
+
symbol: "\u2033",
|
|
1494
|
+
singular: "arcsecond",
|
|
1495
|
+
plural: "arcseconds",
|
|
1496
|
+
noSpace: true
|
|
1497
|
+
// 45″ não 45 ″
|
|
1498
|
+
},
|
|
1499
|
+
milliarcsecond: {
|
|
1500
|
+
factor: 1 / 36e5,
|
|
1501
|
+
// 2.777...e-7
|
|
1502
|
+
symbol: "mas",
|
|
1503
|
+
singular: "milliarcsecond",
|
|
1504
|
+
plural: "milliarcseconds"
|
|
1505
|
+
},
|
|
1506
|
+
// Radianos (unidade SI)
|
|
1507
|
+
radian: {
|
|
1508
|
+
factor: DEGREES_PER_RADIAN,
|
|
1509
|
+
// 180/π ≈ 57.2958
|
|
1510
|
+
symbol: "rad",
|
|
1511
|
+
singular: "radian",
|
|
1512
|
+
plural: "radians"
|
|
1513
|
+
},
|
|
1514
|
+
milliradian: {
|
|
1515
|
+
factor: DEGREES_PER_MILLIRADIAN,
|
|
1516
|
+
// 180/(1000π) ≈ 0.0573
|
|
1517
|
+
symbol: "mrad",
|
|
1518
|
+
singular: "milliradian",
|
|
1519
|
+
plural: "milliradians"
|
|
1520
|
+
},
|
|
1521
|
+
// Gradiano (gon)
|
|
1522
|
+
gradian: {
|
|
1523
|
+
factor: 0.9,
|
|
1524
|
+
// 360/400
|
|
1525
|
+
symbol: "gon",
|
|
1526
|
+
singular: "gradian",
|
|
1527
|
+
plural: "gradians"
|
|
1528
|
+
}
|
|
1529
|
+
};
|
|
1530
|
+
Object.fromEntries(
|
|
1531
|
+
Object.entries(ANGLE_UNITS).map(([unit, config]) => [
|
|
1532
|
+
unit,
|
|
1533
|
+
config.factor ?? 1
|
|
1534
|
+
])
|
|
1535
|
+
);
|
|
1536
|
+
|
|
1537
|
+
// src/roles/angle/convert.ts
|
|
1538
|
+
var ANGLE_BASE = "degree";
|
|
1539
|
+
function toBaseAngle(variant, value) {
|
|
1540
|
+
const config = ANGLE_UNITS[variant];
|
|
1541
|
+
if (!config) {
|
|
1542
|
+
throw new Error(`Unknown angle unit: ${variant}`);
|
|
1543
|
+
}
|
|
1544
|
+
return value * (config.factor ?? 1);
|
|
1545
|
+
}
|
|
1546
|
+
function fromBaseAngle(variant, baseValue) {
|
|
1547
|
+
const config = ANGLE_UNITS[variant];
|
|
1548
|
+
if (!config) {
|
|
1549
|
+
throw new Error(`Unknown angle unit: ${variant}`);
|
|
1550
|
+
}
|
|
1551
|
+
return baseValue / (config.factor ?? 1);
|
|
1552
|
+
}
|
|
1553
|
+
function convertAngle(from, to, value) {
|
|
1554
|
+
if (from === to) {
|
|
1555
|
+
return value;
|
|
1556
|
+
}
|
|
1557
|
+
const baseValue = toBaseAngle(from, value);
|
|
1558
|
+
return fromBaseAngle(to, baseValue);
|
|
1559
|
+
}
|
|
1560
|
+
function tryConvertAngle(from, to, value) {
|
|
1561
|
+
try {
|
|
1562
|
+
return { ok: true, value: convertAngle(from, to, value) };
|
|
1563
|
+
} catch (e) {
|
|
1564
|
+
return { ok: false, error: e.message };
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
function hasAngleVariant(variant) {
|
|
1568
|
+
return variant in ANGLE_UNITS;
|
|
1569
|
+
}
|
|
1570
|
+
function getAngleVariants() {
|
|
1571
|
+
return Object.keys(ANGLE_UNITS);
|
|
1572
|
+
}
|
|
1573
|
+
|
|
1574
|
+
// src/roles/time/constants.ts
|
|
1575
|
+
var TIME_UNITS = {
|
|
1576
|
+
// SI prefixes (exatos)
|
|
1577
|
+
nanosecond: {
|
|
1578
|
+
factor: 1e-9,
|
|
1579
|
+
symbol: "ns",
|
|
1580
|
+
singular: "nanosecond",
|
|
1581
|
+
plural: "nanoseconds"
|
|
1582
|
+
},
|
|
1583
|
+
microsecond: {
|
|
1584
|
+
factor: 1e-6,
|
|
1585
|
+
symbol: "\u03BCs",
|
|
1586
|
+
singular: "microsecond",
|
|
1587
|
+
plural: "microseconds"
|
|
1588
|
+
},
|
|
1589
|
+
millisecond: {
|
|
1590
|
+
factor: 1e-3,
|
|
1591
|
+
symbol: "ms",
|
|
1592
|
+
singular: "millisecond",
|
|
1593
|
+
plural: "milliseconds"
|
|
1594
|
+
},
|
|
1595
|
+
second: {
|
|
1596
|
+
factor: 1,
|
|
1597
|
+
symbol: "s",
|
|
1598
|
+
singular: "second",
|
|
1599
|
+
plural: "seconds"
|
|
1600
|
+
},
|
|
1601
|
+
// Common (exatos)
|
|
1602
|
+
minute: {
|
|
1603
|
+
factor: 60,
|
|
1604
|
+
symbol: "min",
|
|
1605
|
+
singular: "minute",
|
|
1606
|
+
plural: "minutes"
|
|
1607
|
+
},
|
|
1608
|
+
hour: {
|
|
1609
|
+
factor: 3600,
|
|
1610
|
+
symbol: "h",
|
|
1611
|
+
singular: "hour",
|
|
1612
|
+
plural: "hours"
|
|
1613
|
+
},
|
|
1614
|
+
day: {
|
|
1615
|
+
factor: 86400,
|
|
1616
|
+
symbol: "d",
|
|
1617
|
+
singular: "day",
|
|
1618
|
+
plural: "days"
|
|
1619
|
+
},
|
|
1620
|
+
week: {
|
|
1621
|
+
factor: 604800,
|
|
1622
|
+
symbol: "wk",
|
|
1623
|
+
singular: "week",
|
|
1624
|
+
plural: "weeks"
|
|
1625
|
+
},
|
|
1626
|
+
// Calendar (aproximados - baseados no ano Gregoriano)
|
|
1627
|
+
month: {
|
|
1628
|
+
factor: 2629746,
|
|
1629
|
+
// 31556952 / 12
|
|
1630
|
+
symbol: "mo",
|
|
1631
|
+
singular: "month",
|
|
1632
|
+
plural: "months"
|
|
1633
|
+
},
|
|
1634
|
+
year: {
|
|
1635
|
+
factor: 31556952,
|
|
1636
|
+
// 365.2425 * 86400
|
|
1637
|
+
symbol: "yr",
|
|
1638
|
+
singular: "year",
|
|
1639
|
+
plural: "years"
|
|
1640
|
+
},
|
|
1641
|
+
decade: {
|
|
1642
|
+
factor: 315569520,
|
|
1643
|
+
// 10 * year
|
|
1644
|
+
symbol: "dec",
|
|
1645
|
+
singular: "decade",
|
|
1646
|
+
plural: "decades"
|
|
1647
|
+
},
|
|
1648
|
+
century: {
|
|
1649
|
+
factor: 3155695200,
|
|
1650
|
+
// 100 * year
|
|
1651
|
+
symbol: "c",
|
|
1652
|
+
singular: "century",
|
|
1653
|
+
plural: "centuries"
|
|
1654
|
+
},
|
|
1655
|
+
millennium: {
|
|
1656
|
+
factor: 31556952e3,
|
|
1657
|
+
// 1000 * year
|
|
1658
|
+
symbol: "ky",
|
|
1659
|
+
singular: "millennium",
|
|
1660
|
+
plural: "millennia"
|
|
1661
|
+
}
|
|
1662
|
+
};
|
|
1663
|
+
Object.fromEntries(
|
|
1664
|
+
Object.entries(TIME_UNITS).map(([unit, config]) => [
|
|
1665
|
+
unit,
|
|
1666
|
+
config.factor ?? 1
|
|
1667
|
+
])
|
|
1668
|
+
);
|
|
1669
|
+
|
|
1670
|
+
// src/roles/time/convert.ts
|
|
1671
|
+
var TIME_BASE = "second";
|
|
1672
|
+
function toBaseTime(variant, value) {
|
|
1673
|
+
const config = TIME_UNITS[variant];
|
|
1674
|
+
if (!config) {
|
|
1675
|
+
throw new Error(`Unknown time unit: ${variant}`);
|
|
1676
|
+
}
|
|
1677
|
+
return value * (config.factor ?? 1);
|
|
1678
|
+
}
|
|
1679
|
+
function fromBaseTime(variant, baseValue) {
|
|
1680
|
+
const config = TIME_UNITS[variant];
|
|
1681
|
+
if (!config) {
|
|
1682
|
+
throw new Error(`Unknown time unit: ${variant}`);
|
|
1683
|
+
}
|
|
1684
|
+
return baseValue / (config.factor ?? 1);
|
|
1685
|
+
}
|
|
1686
|
+
function convertTime(from, to, value) {
|
|
1687
|
+
if (from === to) {
|
|
1688
|
+
return value;
|
|
1689
|
+
}
|
|
1690
|
+
const baseValue = toBaseTime(from, value);
|
|
1691
|
+
return fromBaseTime(to, baseValue);
|
|
1692
|
+
}
|
|
1693
|
+
function tryConvertTime(from, to, value) {
|
|
1694
|
+
try {
|
|
1695
|
+
return { ok: true, value: convertTime(from, to, value) };
|
|
1696
|
+
} catch (e) {
|
|
1697
|
+
return { ok: false, error: e.message };
|
|
1698
|
+
}
|
|
1699
|
+
}
|
|
1700
|
+
function hasTimeVariant(variant) {
|
|
1701
|
+
return variant in TIME_UNITS;
|
|
1702
|
+
}
|
|
1703
|
+
function getTimeVariants() {
|
|
1704
|
+
return Object.keys(TIME_UNITS);
|
|
1705
|
+
}
|
|
1706
|
+
|
|
1707
|
+
// src/roles/digital/constants.ts
|
|
1708
|
+
var DIGITAL_UNITS = {
|
|
1709
|
+
// Fundamental
|
|
1710
|
+
bit: {
|
|
1711
|
+
factor: 0.125,
|
|
1712
|
+
// 1/8 byte
|
|
1713
|
+
symbol: "b",
|
|
1714
|
+
singular: "bit",
|
|
1715
|
+
plural: "bits"
|
|
1716
|
+
},
|
|
1717
|
+
byte: {
|
|
1718
|
+
factor: 1,
|
|
1719
|
+
symbol: "B",
|
|
1720
|
+
singular: "byte",
|
|
1721
|
+
plural: "bytes"
|
|
1722
|
+
},
|
|
1723
|
+
// IEC Binary (base 1024) - RAM, cache, file systems
|
|
1724
|
+
kibibyte: {
|
|
1725
|
+
factor: 1024,
|
|
1726
|
+
// 2^10
|
|
1727
|
+
symbol: "KiB",
|
|
1728
|
+
singular: "kibibyte",
|
|
1729
|
+
plural: "kibibytes"
|
|
1730
|
+
},
|
|
1731
|
+
mebibyte: {
|
|
1732
|
+
factor: 1048576,
|
|
1733
|
+
// 2^20
|
|
1734
|
+
symbol: "MiB",
|
|
1735
|
+
singular: "mebibyte",
|
|
1736
|
+
plural: "mebibytes"
|
|
1737
|
+
},
|
|
1738
|
+
gibibyte: {
|
|
1739
|
+
factor: 1073741824,
|
|
1740
|
+
// 2^30
|
|
1741
|
+
symbol: "GiB",
|
|
1742
|
+
singular: "gibibyte",
|
|
1743
|
+
plural: "gibibytes"
|
|
1744
|
+
},
|
|
1745
|
+
tebibyte: {
|
|
1746
|
+
factor: 1099511627776,
|
|
1747
|
+
// 2^40
|
|
1748
|
+
symbol: "TiB",
|
|
1749
|
+
singular: "tebibyte",
|
|
1750
|
+
plural: "tebibytes"
|
|
1751
|
+
},
|
|
1752
|
+
pebibyte: {
|
|
1753
|
+
factor: 1125899906842624,
|
|
1754
|
+
// 2^50
|
|
1755
|
+
symbol: "PiB",
|
|
1756
|
+
singular: "pebibyte",
|
|
1757
|
+
plural: "pebibytes"
|
|
1758
|
+
},
|
|
1759
|
+
exbibyte: {
|
|
1760
|
+
factor: 1152921504606847e3,
|
|
1761
|
+
// 2^60
|
|
1762
|
+
symbol: "EiB",
|
|
1763
|
+
singular: "exbibyte",
|
|
1764
|
+
plural: "exbibytes"
|
|
1765
|
+
},
|
|
1766
|
+
// SI Decimal (base 1000) - HD marketing, network speeds
|
|
1767
|
+
kilobyte: {
|
|
1768
|
+
factor: 1e3,
|
|
1769
|
+
// 10^3
|
|
1770
|
+
symbol: "kB",
|
|
1771
|
+
singular: "kilobyte",
|
|
1772
|
+
plural: "kilobytes"
|
|
1773
|
+
},
|
|
1774
|
+
megabyte: {
|
|
1775
|
+
factor: 1e6,
|
|
1776
|
+
// 10^6
|
|
1777
|
+
symbol: "MB",
|
|
1778
|
+
singular: "megabyte",
|
|
1779
|
+
plural: "megabytes"
|
|
1780
|
+
},
|
|
1781
|
+
gigabyte: {
|
|
1782
|
+
factor: 1e9,
|
|
1783
|
+
// 10^9
|
|
1784
|
+
symbol: "GB",
|
|
1785
|
+
singular: "gigabyte",
|
|
1786
|
+
plural: "gigabytes"
|
|
1787
|
+
},
|
|
1788
|
+
terabyte: {
|
|
1789
|
+
factor: 1e12,
|
|
1790
|
+
// 10^12
|
|
1791
|
+
symbol: "TB",
|
|
1792
|
+
singular: "terabyte",
|
|
1793
|
+
plural: "terabytes"
|
|
1794
|
+
},
|
|
1795
|
+
petabyte: {
|
|
1796
|
+
factor: 1e15,
|
|
1797
|
+
// 10^15
|
|
1798
|
+
symbol: "PB",
|
|
1799
|
+
singular: "petabyte",
|
|
1800
|
+
plural: "petabytes"
|
|
1801
|
+
},
|
|
1802
|
+
exabyte: {
|
|
1803
|
+
factor: 1e18,
|
|
1804
|
+
// 10^18
|
|
1805
|
+
symbol: "EB",
|
|
1806
|
+
singular: "exabyte",
|
|
1807
|
+
plural: "exabytes"
|
|
1808
|
+
}
|
|
1809
|
+
};
|
|
1810
|
+
Object.fromEntries(
|
|
1811
|
+
Object.entries(DIGITAL_UNITS).map(([unit, config]) => [
|
|
1812
|
+
unit,
|
|
1813
|
+
config.factor ?? 1
|
|
1814
|
+
])
|
|
1815
|
+
);
|
|
1816
|
+
|
|
1817
|
+
// src/roles/digital/convert.ts
|
|
1818
|
+
var DIGITAL_BASE = "byte";
|
|
1819
|
+
function toBaseDigital(variant, value) {
|
|
1820
|
+
const config = DIGITAL_UNITS[variant];
|
|
1821
|
+
if (!config) {
|
|
1822
|
+
throw new Error(`Unknown digital unit: ${variant}`);
|
|
1823
|
+
}
|
|
1824
|
+
return value * (config.factor ?? 1);
|
|
1825
|
+
}
|
|
1826
|
+
function fromBaseDigital(variant, baseValue) {
|
|
1827
|
+
const config = DIGITAL_UNITS[variant];
|
|
1828
|
+
if (!config) {
|
|
1829
|
+
throw new Error(`Unknown digital unit: ${variant}`);
|
|
1830
|
+
}
|
|
1831
|
+
return baseValue / (config.factor ?? 1);
|
|
1832
|
+
}
|
|
1833
|
+
function convertDigital(from, to, value) {
|
|
1834
|
+
if (from === to) {
|
|
1835
|
+
return value;
|
|
1836
|
+
}
|
|
1837
|
+
const baseValue = toBaseDigital(from, value);
|
|
1838
|
+
return fromBaseDigital(to, baseValue);
|
|
1839
|
+
}
|
|
1840
|
+
function tryConvertDigital(from, to, value) {
|
|
1841
|
+
try {
|
|
1842
|
+
return { ok: true, value: convertDigital(from, to, value) };
|
|
1843
|
+
} catch (e) {
|
|
1844
|
+
return { ok: false, error: e.message };
|
|
1845
|
+
}
|
|
1846
|
+
}
|
|
1847
|
+
function hasDigitalVariant(variant) {
|
|
1848
|
+
return variant in DIGITAL_UNITS;
|
|
1849
|
+
}
|
|
1850
|
+
function getDigitalVariants() {
|
|
1851
|
+
return Object.keys(DIGITAL_UNITS);
|
|
1852
|
+
}
|
|
1853
|
+
|
|
1854
|
+
// src/roles/color/types.ts
|
|
1855
|
+
function clamp(value, min, max) {
|
|
1856
|
+
return Math.min(Math.max(value, min), max);
|
|
1857
|
+
}
|
|
1858
|
+
function normalizeRgbChannel(value) {
|
|
1859
|
+
return clamp(Math.round(value), 0, 255);
|
|
1860
|
+
}
|
|
1861
|
+
function normalizeAlpha(value) {
|
|
1862
|
+
if (value === void 0) return 1;
|
|
1863
|
+
return clamp(value, 0, 1);
|
|
1864
|
+
}
|
|
1865
|
+
function rgbToHsl(r, g, b) {
|
|
1866
|
+
r /= 255;
|
|
1867
|
+
g /= 255;
|
|
1868
|
+
b /= 255;
|
|
1869
|
+
const max = Math.max(r, g, b);
|
|
1870
|
+
const min = Math.min(r, g, b);
|
|
1871
|
+
const l = (max + min) / 2;
|
|
1872
|
+
if (max === min) {
|
|
1873
|
+
return { h: 0, s: 0, l: l * 100 };
|
|
1874
|
+
}
|
|
1875
|
+
const d = max - min;
|
|
1876
|
+
const s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
1877
|
+
let h;
|
|
1878
|
+
switch (max) {
|
|
1879
|
+
case r:
|
|
1880
|
+
h = ((g - b) / d + (g < b ? 6 : 0)) / 6;
|
|
1881
|
+
break;
|
|
1882
|
+
case g:
|
|
1883
|
+
h = ((b - r) / d + 2) / 6;
|
|
1884
|
+
break;
|
|
1885
|
+
default:
|
|
1886
|
+
h = ((r - g) / d + 4) / 6;
|
|
1887
|
+
break;
|
|
1888
|
+
}
|
|
1889
|
+
return {
|
|
1890
|
+
h: Math.round(h * 360),
|
|
1891
|
+
s: Math.round(s * 100),
|
|
1892
|
+
l: Math.round(l * 100)
|
|
1893
|
+
};
|
|
1894
|
+
}
|
|
1895
|
+
function hslToRgb(h, s, l) {
|
|
1896
|
+
h /= 360;
|
|
1897
|
+
s /= 100;
|
|
1898
|
+
l /= 100;
|
|
1899
|
+
if (s === 0) {
|
|
1900
|
+
const gray = Math.round(l * 255);
|
|
1901
|
+
return { r: gray, g: gray, b: gray };
|
|
1902
|
+
}
|
|
1903
|
+
const hue2rgb = (p2, q2, t) => {
|
|
1904
|
+
if (t < 0) t += 1;
|
|
1905
|
+
if (t > 1) t -= 1;
|
|
1906
|
+
if (t < 1 / 6) return p2 + (q2 - p2) * 6 * t;
|
|
1907
|
+
if (t < 1 / 2) return q2;
|
|
1908
|
+
if (t < 2 / 3) return p2 + (q2 - p2) * (2 / 3 - t) * 6;
|
|
1909
|
+
return p2;
|
|
1910
|
+
};
|
|
1911
|
+
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
|
1912
|
+
const p = 2 * l - q;
|
|
1913
|
+
return {
|
|
1914
|
+
r: Math.round(hue2rgb(p, q, h + 1 / 3) * 255),
|
|
1915
|
+
g: Math.round(hue2rgb(p, q, h) * 255),
|
|
1916
|
+
b: Math.round(hue2rgb(p, q, h - 1 / 3) * 255)
|
|
1917
|
+
};
|
|
1918
|
+
}
|
|
1919
|
+
|
|
1920
|
+
// src/roles/color/convert.ts
|
|
1921
|
+
function hexToRgba(hex) {
|
|
1922
|
+
let h = hex.trim();
|
|
1923
|
+
if (h.startsWith("#")) h = h.slice(1);
|
|
1924
|
+
let r, g, b, a;
|
|
1925
|
+
if (h.length === 3) {
|
|
1926
|
+
r = parseInt(h[0] + h[0], 16);
|
|
1927
|
+
g = parseInt(h[1] + h[1], 16);
|
|
1928
|
+
b = parseInt(h[2] + h[2], 16);
|
|
1929
|
+
a = 1;
|
|
1930
|
+
} else if (h.length === 4) {
|
|
1931
|
+
r = parseInt(h[0] + h[0], 16);
|
|
1932
|
+
g = parseInt(h[1] + h[1], 16);
|
|
1933
|
+
b = parseInt(h[2] + h[2], 16);
|
|
1934
|
+
a = parseInt(h[3] + h[3], 16) / 255;
|
|
1935
|
+
} else if (h.length === 6) {
|
|
1936
|
+
r = parseInt(h.slice(0, 2), 16);
|
|
1937
|
+
g = parseInt(h.slice(2, 4), 16);
|
|
1938
|
+
b = parseInt(h.slice(4, 6), 16);
|
|
1939
|
+
a = 1;
|
|
1940
|
+
} else if (h.length === 8) {
|
|
1941
|
+
r = parseInt(h.slice(0, 2), 16);
|
|
1942
|
+
g = parseInt(h.slice(2, 4), 16);
|
|
1943
|
+
b = parseInt(h.slice(4, 6), 16);
|
|
1944
|
+
a = parseInt(h.slice(6, 8), 16) / 255;
|
|
1945
|
+
} else {
|
|
1946
|
+
throw new Error(`Invalid hex color: ${hex}`);
|
|
1947
|
+
}
|
|
1948
|
+
return { r, g, b, a };
|
|
1949
|
+
}
|
|
1950
|
+
function rgbObjectToRgba(rgb) {
|
|
1951
|
+
return {
|
|
1952
|
+
r: normalizeRgbChannel(rgb.r),
|
|
1953
|
+
g: normalizeRgbChannel(rgb.g),
|
|
1954
|
+
b: normalizeRgbChannel(rgb.b),
|
|
1955
|
+
a: normalizeAlpha(rgb.a)
|
|
1956
|
+
};
|
|
1957
|
+
}
|
|
1958
|
+
function rgbStringToRgba(rgb) {
|
|
1959
|
+
const match = rgb.match(/rgba?\s*\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*(?:,\s*([\d.]+)\s*)?\)/i);
|
|
1960
|
+
if (!match) {
|
|
1961
|
+
throw new Error(`Invalid RGB string: ${rgb}`);
|
|
1962
|
+
}
|
|
1963
|
+
return {
|
|
1964
|
+
r: normalizeRgbChannel(parseFloat(match[1])),
|
|
1965
|
+
g: normalizeRgbChannel(parseFloat(match[2])),
|
|
1966
|
+
b: normalizeRgbChannel(parseFloat(match[3])),
|
|
1967
|
+
a: match[4] !== void 0 ? normalizeAlpha(parseFloat(match[4])) : 1
|
|
1968
|
+
};
|
|
1969
|
+
}
|
|
1970
|
+
function hslObjectToRgba(hsl) {
|
|
1971
|
+
const { r, g, b } = hslToRgb(hsl.h, hsl.s, hsl.l);
|
|
1972
|
+
return {
|
|
1973
|
+
r,
|
|
1974
|
+
g,
|
|
1975
|
+
b,
|
|
1976
|
+
a: normalizeAlpha(hsl.a)
|
|
1977
|
+
};
|
|
1978
|
+
}
|
|
1979
|
+
function hslStringToRgba(hsl) {
|
|
1980
|
+
const match = hsl.match(/hsla?\s*\(\s*([\d.]+)\s*,\s*([\d.]+)%?\s*,\s*([\d.]+)%?\s*(?:,\s*([\d.]+)\s*)?\)/i);
|
|
1981
|
+
if (!match) {
|
|
1982
|
+
throw new Error(`Invalid HSL string: ${hsl}`);
|
|
1983
|
+
}
|
|
1984
|
+
const h = parseFloat(match[1]);
|
|
1985
|
+
const s = parseFloat(match[2]);
|
|
1986
|
+
const l = parseFloat(match[3]);
|
|
1987
|
+
const a = match[4] !== void 0 ? parseFloat(match[4]) : 1;
|
|
1988
|
+
const { r, g, b } = hslToRgb(h, s, l);
|
|
1989
|
+
return { r, g, b, a: normalizeAlpha(a) };
|
|
1990
|
+
}
|
|
1991
|
+
function toBaseColor(variant, value) {
|
|
1992
|
+
switch (variant) {
|
|
1993
|
+
case "hex":
|
|
1994
|
+
return hexToRgba(value);
|
|
1995
|
+
case "rgb_object":
|
|
1996
|
+
return rgbObjectToRgba(value);
|
|
1997
|
+
case "rgb_string":
|
|
1998
|
+
return rgbStringToRgba(value);
|
|
1999
|
+
case "hsl_object":
|
|
2000
|
+
return hslObjectToRgba(value);
|
|
2001
|
+
case "hsl_string":
|
|
2002
|
+
return hslStringToRgba(value);
|
|
2003
|
+
default:
|
|
2004
|
+
throw new Error(`Unknown color variant: ${variant}`);
|
|
2005
|
+
}
|
|
2006
|
+
}
|
|
2007
|
+
function rgbaToHex(rgba, includeAlpha = false) {
|
|
2008
|
+
const r = rgba.r.toString(16).padStart(2, "0");
|
|
2009
|
+
const g = rgba.g.toString(16).padStart(2, "0");
|
|
2010
|
+
const b = rgba.b.toString(16).padStart(2, "0");
|
|
2011
|
+
if (includeAlpha || rgba.a < 1) {
|
|
2012
|
+
const a = Math.round(rgba.a * 255).toString(16).padStart(2, "0");
|
|
2013
|
+
return `#${r}${g}${b}${a}`;
|
|
2014
|
+
}
|
|
2015
|
+
return `#${r}${g}${b}`;
|
|
2016
|
+
}
|
|
2017
|
+
function rgbaToRgbObject(rgba) {
|
|
2018
|
+
if (rgba.a < 1) {
|
|
2019
|
+
return { r: rgba.r, g: rgba.g, b: rgba.b, a: rgba.a };
|
|
2020
|
+
}
|
|
2021
|
+
return { r: rgba.r, g: rgba.g, b: rgba.b };
|
|
2022
|
+
}
|
|
2023
|
+
function rgbaToRgbString(rgba) {
|
|
2024
|
+
if (rgba.a < 1) {
|
|
2025
|
+
return `rgba(${rgba.r}, ${rgba.g}, ${rgba.b}, ${rgba.a})`;
|
|
2026
|
+
}
|
|
2027
|
+
return `rgb(${rgba.r}, ${rgba.g}, ${rgba.b})`;
|
|
2028
|
+
}
|
|
2029
|
+
function rgbaToHslObject(rgba) {
|
|
2030
|
+
const { h, s, l } = rgbToHsl(rgba.r, rgba.g, rgba.b);
|
|
2031
|
+
if (rgba.a < 1) {
|
|
2032
|
+
return { h, s, l, a: rgba.a };
|
|
2033
|
+
}
|
|
2034
|
+
return { h, s, l };
|
|
2035
|
+
}
|
|
2036
|
+
function rgbaToHslString(rgba) {
|
|
2037
|
+
const { h, s, l } = rgbToHsl(rgba.r, rgba.g, rgba.b);
|
|
2038
|
+
if (rgba.a < 1) {
|
|
2039
|
+
return `hsla(${h}, ${s}%, ${l}%, ${rgba.a})`;
|
|
2040
|
+
}
|
|
2041
|
+
return `hsl(${h}, ${s}%, ${l}%)`;
|
|
2042
|
+
}
|
|
2043
|
+
function fromBaseColor(variant, rgba) {
|
|
2044
|
+
switch (variant) {
|
|
2045
|
+
case "hex":
|
|
2046
|
+
return rgbaToHex(rgba);
|
|
2047
|
+
case "rgb_object":
|
|
2048
|
+
return rgbaToRgbObject(rgba);
|
|
2049
|
+
case "rgb_string":
|
|
2050
|
+
return rgbaToRgbString(rgba);
|
|
2051
|
+
case "hsl_object":
|
|
2052
|
+
return rgbaToHslObject(rgba);
|
|
2053
|
+
case "hsl_string":
|
|
2054
|
+
return rgbaToHslString(rgba);
|
|
2055
|
+
default:
|
|
2056
|
+
throw new Error(`Unknown color variant: ${variant}`);
|
|
2057
|
+
}
|
|
2058
|
+
}
|
|
2059
|
+
function convertColor(from, to, value) {
|
|
2060
|
+
if (from === to) {
|
|
2061
|
+
return value;
|
|
2062
|
+
}
|
|
2063
|
+
const rgba = toBaseColor(from, value);
|
|
2064
|
+
return fromBaseColor(to, rgba);
|
|
2065
|
+
}
|
|
2066
|
+
function tryConvertColor(from, to, value) {
|
|
2067
|
+
try {
|
|
2068
|
+
const result = convertColor(from, to, value);
|
|
2069
|
+
return { ok: true, value: result };
|
|
2070
|
+
} catch (error) {
|
|
2071
|
+
return {
|
|
2072
|
+
ok: false,
|
|
2073
|
+
error: error instanceof Error ? error.message : String(error)
|
|
2074
|
+
};
|
|
2075
|
+
}
|
|
2076
|
+
}
|
|
2077
|
+
|
|
2078
|
+
// src/roles/date/convert.ts
|
|
2079
|
+
function isoToBase(value) {
|
|
2080
|
+
const date = new Date(value);
|
|
2081
|
+
if (isNaN(date.getTime())) {
|
|
2082
|
+
throw new Error(`Invalid ISO date: ${value}`);
|
|
2083
|
+
}
|
|
2084
|
+
return date.getTime();
|
|
2085
|
+
}
|
|
2086
|
+
function timestampToBase(value) {
|
|
2087
|
+
return value;
|
|
2088
|
+
}
|
|
2089
|
+
function epochToBase(value) {
|
|
2090
|
+
return value * 1e3;
|
|
2091
|
+
}
|
|
2092
|
+
function toBaseDate(variant, value) {
|
|
2093
|
+
switch (variant) {
|
|
2094
|
+
case "iso":
|
|
2095
|
+
return isoToBase(value);
|
|
2096
|
+
case "timestamp":
|
|
2097
|
+
return timestampToBase(value);
|
|
2098
|
+
case "epoch":
|
|
2099
|
+
return epochToBase(value);
|
|
2100
|
+
default:
|
|
2101
|
+
throw new Error(`Unknown date variant: ${variant}`);
|
|
2102
|
+
}
|
|
2103
|
+
}
|
|
2104
|
+
function baseToIso(base) {
|
|
2105
|
+
const date = new Date(base);
|
|
2106
|
+
if (isNaN(date.getTime())) {
|
|
2107
|
+
throw new Error(`Invalid timestamp: ${base}`);
|
|
2108
|
+
}
|
|
2109
|
+
return date.toISOString();
|
|
2110
|
+
}
|
|
2111
|
+
function baseToTimestamp(base) {
|
|
2112
|
+
return base;
|
|
2113
|
+
}
|
|
2114
|
+
function baseToEpoch(base) {
|
|
2115
|
+
return Math.floor(base / 1e3);
|
|
2116
|
+
}
|
|
2117
|
+
function fromBaseDate(variant, base) {
|
|
2118
|
+
switch (variant) {
|
|
2119
|
+
case "iso":
|
|
2120
|
+
return baseToIso(base);
|
|
2121
|
+
case "timestamp":
|
|
2122
|
+
return baseToTimestamp(base);
|
|
2123
|
+
case "epoch":
|
|
2124
|
+
return baseToEpoch(base);
|
|
2125
|
+
default:
|
|
2126
|
+
throw new Error(`Unknown date variant: ${variant}`);
|
|
2127
|
+
}
|
|
2128
|
+
}
|
|
2129
|
+
function convertDate(from, to, value) {
|
|
2130
|
+
if (from === to) {
|
|
2131
|
+
return value;
|
|
2132
|
+
}
|
|
2133
|
+
const base = toBaseDate(from, value);
|
|
2134
|
+
return fromBaseDate(to, base);
|
|
2135
|
+
}
|
|
2136
|
+
function tryConvertDate(from, to, value) {
|
|
2137
|
+
try {
|
|
2138
|
+
const result = convertDate(from, to, value);
|
|
2139
|
+
return { ok: true, value: result };
|
|
2140
|
+
} catch (error) {
|
|
2141
|
+
return {
|
|
2142
|
+
ok: false,
|
|
2143
|
+
error: error instanceof Error ? error.message : String(error)
|
|
2144
|
+
};
|
|
2145
|
+
}
|
|
2146
|
+
}
|
|
2147
|
+
|
|
2148
|
+
exports.ANGLE_BASE = ANGLE_BASE;
|
|
2149
|
+
exports.AREA_BASE = AREA_BASE;
|
|
2150
|
+
exports.DIGITAL_BASE = DIGITAL_BASE;
|
|
2151
|
+
exports.ENERGY_BASE = ENERGY_BASE;
|
|
2152
|
+
exports.FREQUENCY_BASE = FREQUENCY_BASE;
|
|
2153
|
+
exports.LENGTH_BASE = LENGTH_BASE;
|
|
2154
|
+
exports.MASS_BASE = MASS_BASE;
|
|
2155
|
+
exports.POWER_BASE = POWER_BASE;
|
|
2156
|
+
exports.PRESSURE_BASE = PRESSURE_BASE;
|
|
2157
|
+
exports.SPEED_BASE = SPEED_BASE;
|
|
2158
|
+
exports.TEMPERATURE_BASE = TEMPERATURE_BASE;
|
|
2159
|
+
exports.TIME_BASE = TIME_BASE;
|
|
2160
|
+
exports.VOLUME_BASE = VOLUME_BASE;
|
|
2161
|
+
exports.convertAngle = convertAngle;
|
|
2162
|
+
exports.convertArea = convertArea;
|
|
2163
|
+
exports.convertColor = convertColor;
|
|
2164
|
+
exports.convertDate = convertDate;
|
|
2165
|
+
exports.convertDigital = convertDigital;
|
|
2166
|
+
exports.convertEnergy = convertEnergy;
|
|
2167
|
+
exports.convertFrequency = convertFrequency;
|
|
2168
|
+
exports.convertLength = convertLength;
|
|
2169
|
+
exports.convertMass = convertMass;
|
|
2170
|
+
exports.convertPower = convertPower;
|
|
2171
|
+
exports.convertPressure = convertPressure;
|
|
2172
|
+
exports.convertSpeed = convertSpeed;
|
|
2173
|
+
exports.convertTemperature = convertTemperature;
|
|
2174
|
+
exports.convertTime = convertTime;
|
|
2175
|
+
exports.convertVolume = convertVolume;
|
|
2176
|
+
exports.fromBaseAngle = fromBaseAngle;
|
|
2177
|
+
exports.fromBaseArea = fromBaseArea;
|
|
2178
|
+
exports.fromBaseDigital = fromBaseDigital;
|
|
2179
|
+
exports.fromBaseEnergy = fromBaseEnergy;
|
|
2180
|
+
exports.fromBaseFrequency = fromBaseFrequency;
|
|
2181
|
+
exports.fromBaseLength = fromBaseLength;
|
|
2182
|
+
exports.fromBaseMass = fromBaseMass;
|
|
2183
|
+
exports.fromBasePower = fromBasePower;
|
|
2184
|
+
exports.fromBasePressure = fromBasePressure;
|
|
2185
|
+
exports.fromBaseSpeed = fromBaseSpeed;
|
|
2186
|
+
exports.fromBaseTemperature = fromBaseTemperature;
|
|
2187
|
+
exports.fromBaseTime = fromBaseTime;
|
|
2188
|
+
exports.fromBaseVolume = fromBaseVolume;
|
|
2189
|
+
exports.getAngleVariants = getAngleVariants;
|
|
2190
|
+
exports.getAreaVariants = getAreaVariants;
|
|
2191
|
+
exports.getDigitalVariants = getDigitalVariants;
|
|
2192
|
+
exports.getEnergyVariants = getEnergyVariants;
|
|
2193
|
+
exports.getFrequencyVariants = getFrequencyVariants;
|
|
2194
|
+
exports.getLengthVariants = getLengthVariants;
|
|
2195
|
+
exports.getMassVariants = getMassVariants;
|
|
2196
|
+
exports.getPowerVariants = getPowerVariants;
|
|
2197
|
+
exports.getPressureVariants = getPressureVariants;
|
|
2198
|
+
exports.getSpeedVariants = getSpeedVariants;
|
|
2199
|
+
exports.getTemperatureVariants = getTemperatureVariants;
|
|
2200
|
+
exports.getTimeVariants = getTimeVariants;
|
|
2201
|
+
exports.getVolumeVariants = getVolumeVariants;
|
|
2202
|
+
exports.hasAngleVariant = hasAngleVariant;
|
|
2203
|
+
exports.hasAreaVariant = hasAreaVariant;
|
|
2204
|
+
exports.hasDigitalVariant = hasDigitalVariant;
|
|
2205
|
+
exports.hasEnergyVariant = hasEnergyVariant;
|
|
2206
|
+
exports.hasFrequencyVariant = hasFrequencyVariant;
|
|
2207
|
+
exports.hasLengthVariant = hasLengthVariant;
|
|
2208
|
+
exports.hasMassVariant = hasMassVariant;
|
|
2209
|
+
exports.hasPowerVariant = hasPowerVariant;
|
|
2210
|
+
exports.hasPressureVariant = hasPressureVariant;
|
|
2211
|
+
exports.hasSpeedVariant = hasSpeedVariant;
|
|
2212
|
+
exports.hasTemperatureVariant = hasTemperatureVariant;
|
|
2213
|
+
exports.hasTimeVariant = hasTimeVariant;
|
|
2214
|
+
exports.hasVolumeVariant = hasVolumeVariant;
|
|
2215
|
+
exports.toBaseAngle = toBaseAngle;
|
|
2216
|
+
exports.toBaseArea = toBaseArea;
|
|
2217
|
+
exports.toBaseDigital = toBaseDigital;
|
|
2218
|
+
exports.toBaseEnergy = toBaseEnergy;
|
|
2219
|
+
exports.toBaseFrequency = toBaseFrequency;
|
|
2220
|
+
exports.toBaseLength = toBaseLength;
|
|
2221
|
+
exports.toBaseMass = toBaseMass;
|
|
2222
|
+
exports.toBasePower = toBasePower;
|
|
2223
|
+
exports.toBasePressure = toBasePressure;
|
|
2224
|
+
exports.toBaseSpeed = toBaseSpeed;
|
|
2225
|
+
exports.toBaseTemperature = toBaseTemperature;
|
|
2226
|
+
exports.toBaseTime = toBaseTime;
|
|
2227
|
+
exports.toBaseVolume = toBaseVolume;
|
|
2228
|
+
exports.tryConvertAngle = tryConvertAngle;
|
|
2229
|
+
exports.tryConvertArea = tryConvertArea;
|
|
2230
|
+
exports.tryConvertColor = tryConvertColor;
|
|
2231
|
+
exports.tryConvertDate = tryConvertDate;
|
|
2232
|
+
exports.tryConvertDigital = tryConvertDigital;
|
|
2233
|
+
exports.tryConvertEnergy = tryConvertEnergy;
|
|
2234
|
+
exports.tryConvertFrequency = tryConvertFrequency;
|
|
2235
|
+
exports.tryConvertLength = tryConvertLength;
|
|
2236
|
+
exports.tryConvertMass = tryConvertMass;
|
|
2237
|
+
exports.tryConvertPower = tryConvertPower;
|
|
2238
|
+
exports.tryConvertPressure = tryConvertPressure;
|
|
2239
|
+
exports.tryConvertSpeed = tryConvertSpeed;
|
|
2240
|
+
exports.tryConvertTemperature = tryConvertTemperature;
|
|
2241
|
+
exports.tryConvertTime = tryConvertTime;
|
|
2242
|
+
exports.tryConvertVolume = tryConvertVolume;
|
|
2243
|
+
//# sourceMappingURL=convert.js.map
|
|
2244
|
+
//# sourceMappingURL=convert.js.map
|