@attrx/role-morphic 0.1.0 → 0.2.1

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/dist/cast.mjs ADDED
@@ -0,0 +1,3536 @@
1
+ // src/roles/area/constants.ts
2
+ var AREA_UNITS = {
3
+ // SI Units (todos exatos)
4
+ square_kilometer: {
5
+ factor: 1e6,
6
+ symbol: "km\xB2",
7
+ singular: "square kilometer",
8
+ plural: "square kilometers"
9
+ },
10
+ hectare: {
11
+ factor: 1e4,
12
+ symbol: "ha",
13
+ singular: "hectare",
14
+ plural: "hectares"
15
+ },
16
+ are: {
17
+ factor: 100,
18
+ symbol: "a",
19
+ singular: "are",
20
+ plural: "ares"
21
+ },
22
+ square_meter: {
23
+ factor: 1,
24
+ symbol: "m\xB2",
25
+ singular: "square meter",
26
+ plural: "square meters"
27
+ },
28
+ square_decimeter: {
29
+ factor: 0.01,
30
+ symbol: "dm\xB2",
31
+ singular: "square decimeter",
32
+ plural: "square decimeters"
33
+ },
34
+ square_centimeter: {
35
+ factor: 1e-4,
36
+ symbol: "cm\xB2",
37
+ singular: "square centimeter",
38
+ plural: "square centimeters"
39
+ },
40
+ square_millimeter: {
41
+ factor: 1e-6,
42
+ symbol: "mm\xB2",
43
+ singular: "square millimeter",
44
+ plural: "square millimeters"
45
+ },
46
+ // Imperial/US (todos exatos, derivados de comprimento² desde 1959)
47
+ square_mile: {
48
+ factor: 2589988110336e-6,
49
+ symbol: "mi\xB2",
50
+ singular: "square mile",
51
+ plural: "square miles"
52
+ },
53
+ acre: {
54
+ factor: 4046.8564224,
55
+ symbol: "ac",
56
+ singular: "acre",
57
+ plural: "acres"
58
+ },
59
+ square_yard: {
60
+ factor: 0.83612736,
61
+ symbol: "yd\xB2",
62
+ singular: "square yard",
63
+ plural: "square yards"
64
+ },
65
+ square_foot: {
66
+ factor: 0.09290304,
67
+ symbol: "ft\xB2",
68
+ singular: "square foot",
69
+ plural: "square feet"
70
+ },
71
+ square_inch: {
72
+ factor: 64516e-8,
73
+ symbol: "in\xB2",
74
+ singular: "square inch",
75
+ plural: "square inches"
76
+ }
77
+ };
78
+ var AREA_ALIASES = {
79
+ // square_kilometer
80
+ "km\xB2": "square_kilometer",
81
+ km2: "square_kilometer",
82
+ // hectare
83
+ ha: "hectare",
84
+ // are
85
+ a: "are",
86
+ // square_meter
87
+ "m\xB2": "square_meter",
88
+ m2: "square_meter",
89
+ // square_decimeter
90
+ "dm\xB2": "square_decimeter",
91
+ dm2: "square_decimeter",
92
+ // square_centimeter
93
+ "cm\xB2": "square_centimeter",
94
+ cm2: "square_centimeter",
95
+ // square_millimeter
96
+ "mm\xB2": "square_millimeter",
97
+ mm2: "square_millimeter",
98
+ // square_mile
99
+ "mi\xB2": "square_mile",
100
+ mi2: "square_mile",
101
+ // acre
102
+ ac: "acre",
103
+ // square_yard
104
+ "yd\xB2": "square_yard",
105
+ yd2: "square_yard",
106
+ // square_foot
107
+ "ft\xB2": "square_foot",
108
+ ft2: "square_foot",
109
+ // square_inch
110
+ "in\xB2": "square_inch",
111
+ in2: "square_inch"
112
+ };
113
+ Object.fromEntries(
114
+ Object.entries(AREA_UNITS).map(([unit, config]) => [
115
+ unit,
116
+ config.factor ?? 1
117
+ ])
118
+ );
119
+
120
+ // src/roles/area/convert.ts
121
+ function toBaseArea(variant, value) {
122
+ const unit = AREA_UNITS[variant];
123
+ if (!unit) {
124
+ throw new Error(`Unknown area variant: ${variant}`);
125
+ }
126
+ return value * (unit.factor ?? 1);
127
+ }
128
+ function fromBaseArea(variant, baseValue) {
129
+ const unit = AREA_UNITS[variant];
130
+ if (!unit) {
131
+ throw new Error(`Unknown area variant: ${variant}`);
132
+ }
133
+ return baseValue / (unit.factor ?? 1);
134
+ }
135
+ function convertArea(from, to, value) {
136
+ if (from === to) {
137
+ return value;
138
+ }
139
+ const baseValue = toBaseArea(from, value);
140
+ return fromBaseArea(to, baseValue);
141
+ }
142
+
143
+ // src/roles/area/cast.ts
144
+ function castArea(input, targetVariant = "square_meter") {
145
+ if (typeof input === "number") {
146
+ return Number.isFinite(input) ? input : null;
147
+ }
148
+ if (typeof input === "string") {
149
+ return parseAreaString(input, targetVariant);
150
+ }
151
+ return null;
152
+ }
153
+ function tryCastArea(input, targetVariant = "square_meter") {
154
+ const result = castArea(input, targetVariant);
155
+ if (result === null) {
156
+ return {
157
+ ok: false,
158
+ error: `Cannot cast "${String(input)}" to area:${targetVariant}`
159
+ };
160
+ }
161
+ return { ok: true, value: result };
162
+ }
163
+ function parseAreaString(input, targetVariant) {
164
+ const trimmed = input.trim().toLowerCase();
165
+ const match = trimmed.match(/^([+-]?[\d.,\s]+)\s*(.*)$/);
166
+ if (!match) {
167
+ return null;
168
+ }
169
+ const [, numStr, unitStr] = match;
170
+ const numValue = parseNumber(numStr);
171
+ if (numValue === null) {
172
+ return null;
173
+ }
174
+ const cleanUnitStr = unitStr.trim();
175
+ if (cleanUnitStr) {
176
+ const detectedUnit = detectUnit(cleanUnitStr);
177
+ if (detectedUnit && detectedUnit !== targetVariant) {
178
+ return convertArea(detectedUnit, targetVariant, numValue);
179
+ }
180
+ }
181
+ return numValue;
182
+ }
183
+ function parseNumber(numStr) {
184
+ numStr = numStr.replace(/\s/g, "");
185
+ const lastComma = numStr.lastIndexOf(",");
186
+ const lastDot = numStr.lastIndexOf(".");
187
+ const hasComma = lastComma !== -1;
188
+ const hasDot = lastDot !== -1;
189
+ if (hasComma && hasDot) {
190
+ if (lastComma > lastDot) {
191
+ numStr = numStr.replace(/\./g, "").replace(",", ".");
192
+ } else {
193
+ numStr = numStr.replace(/,/g, "");
194
+ }
195
+ } else if (hasComma && !hasDot) {
196
+ const afterComma = numStr.split(",").slice(1);
197
+ const isThousandSep = afterComma.every((part) => part.length === 3);
198
+ if (isThousandSep) {
199
+ numStr = numStr.replace(/,/g, "");
200
+ } else {
201
+ numStr = numStr.replace(",", ".");
202
+ }
203
+ } else if (!hasComma && hasDot) {
204
+ const afterDot = numStr.split(".").slice(1);
205
+ if (afterDot.length > 1) {
206
+ numStr = numStr.replace(/\./g, "");
207
+ }
208
+ }
209
+ const value = parseFloat(numStr);
210
+ return isNaN(value) ? null : value;
211
+ }
212
+ function detectUnit(unitStr) {
213
+ const normalized = unitStr.toLowerCase().trim();
214
+ if (normalized in AREA_ALIASES) {
215
+ return AREA_ALIASES[normalized];
216
+ }
217
+ for (const [variant, config] of Object.entries(AREA_UNITS)) {
218
+ if (config.symbol.toLowerCase() === normalized || config.singular?.toLowerCase() === normalized || config.plural?.toLowerCase() === normalized || variant.toLowerCase() === normalized) {
219
+ return variant;
220
+ }
221
+ }
222
+ return null;
223
+ }
224
+
225
+ // src/roles/length/constants.ts
226
+ var LENGTH_UNITS = {
227
+ // SI Units (todos exatos)
228
+ kilometer: {
229
+ factor: 1e3,
230
+ symbol: "km",
231
+ singular: "kilometer",
232
+ plural: "kilometers"
233
+ },
234
+ hectometer: {
235
+ factor: 100,
236
+ symbol: "hm",
237
+ singular: "hectometer",
238
+ plural: "hectometers"
239
+ },
240
+ decameter: {
241
+ factor: 10,
242
+ symbol: "dam",
243
+ singular: "decameter",
244
+ plural: "decameters"
245
+ },
246
+ meter: {
247
+ factor: 1,
248
+ symbol: "m",
249
+ singular: "meter",
250
+ plural: "meters"
251
+ },
252
+ decimeter: {
253
+ factor: 0.1,
254
+ symbol: "dm",
255
+ singular: "decimeter",
256
+ plural: "decimeters"
257
+ },
258
+ centimeter: {
259
+ factor: 0.01,
260
+ symbol: "cm",
261
+ singular: "centimeter",
262
+ plural: "centimeters"
263
+ },
264
+ millimeter: {
265
+ factor: 1e-3,
266
+ symbol: "mm",
267
+ singular: "millimeter",
268
+ plural: "millimeters"
269
+ },
270
+ micrometer: {
271
+ factor: 1e-6,
272
+ symbol: "\u03BCm",
273
+ singular: "micrometer",
274
+ plural: "micrometers"
275
+ },
276
+ nanometer: {
277
+ factor: 1e-9,
278
+ symbol: "nm",
279
+ singular: "nanometer",
280
+ plural: "nanometers"
281
+ },
282
+ // Imperial/US (exatos desde 1959)
283
+ inch: {
284
+ factor: 0.0254,
285
+ symbol: "in",
286
+ singular: "inch",
287
+ plural: "inches"
288
+ },
289
+ foot: {
290
+ factor: 0.3048,
291
+ symbol: "ft",
292
+ singular: "foot",
293
+ plural: "feet"
294
+ },
295
+ yard: {
296
+ factor: 0.9144,
297
+ symbol: "yd",
298
+ singular: "yard",
299
+ plural: "yards"
300
+ },
301
+ mile: {
302
+ factor: 1609.344,
303
+ symbol: "mi",
304
+ singular: "mile",
305
+ plural: "miles"
306
+ },
307
+ // Nautical (exato por definição internacional)
308
+ nautical_mile: {
309
+ factor: 1852,
310
+ symbol: "nmi",
311
+ singular: "nautical mile",
312
+ plural: "nautical miles"
313
+ },
314
+ // Other (derivados, portanto exatos)
315
+ fathom: {
316
+ factor: 1.8288,
317
+ // 6 feet
318
+ symbol: "ftm",
319
+ singular: "fathom",
320
+ plural: "fathoms"
321
+ },
322
+ furlong: {
323
+ factor: 201.168,
324
+ // 660 feet = 1/8 mile
325
+ symbol: "fur",
326
+ singular: "furlong",
327
+ plural: "furlongs"
328
+ },
329
+ league: {
330
+ factor: 4828.032,
331
+ // 3 miles
332
+ symbol: "lea",
333
+ singular: "league",
334
+ plural: "leagues"
335
+ }
336
+ };
337
+ var LENGTH_ALIASES = {
338
+ // kilometer
339
+ km: "kilometer",
340
+ // hectometer
341
+ hm: "hectometer",
342
+ // decameter
343
+ dam: "decameter",
344
+ // meter
345
+ m: "meter",
346
+ // decimeter
347
+ dm: "decimeter",
348
+ // centimeter
349
+ cm: "centimeter",
350
+ // millimeter
351
+ mm: "millimeter",
352
+ // micrometer
353
+ "\u03BCm": "micrometer",
354
+ um: "micrometer",
355
+ // nanometer
356
+ nm: "nanometer",
357
+ // inch
358
+ in: "inch",
359
+ '"': "inch",
360
+ // foot
361
+ ft: "foot",
362
+ "'": "foot",
363
+ // yard
364
+ yd: "yard",
365
+ // mile
366
+ mi: "mile",
367
+ // nautical_mile
368
+ nmi: "nautical_mile",
369
+ // fathom
370
+ ftm: "fathom",
371
+ // furlong
372
+ fur: "furlong",
373
+ // league
374
+ lea: "league"
375
+ };
376
+ Object.fromEntries(
377
+ Object.entries(LENGTH_UNITS).map(([unit, config]) => [
378
+ unit,
379
+ config.factor ?? 1
380
+ ])
381
+ );
382
+
383
+ // src/roles/length/convert.ts
384
+ function toBaseLength(variant, value) {
385
+ const unit = LENGTH_UNITS[variant];
386
+ if (!unit) {
387
+ throw new Error(`Unknown length variant: ${variant}`);
388
+ }
389
+ return value * (unit.factor ?? 1);
390
+ }
391
+ function fromBaseLength(variant, baseValue) {
392
+ const unit = LENGTH_UNITS[variant];
393
+ if (!unit) {
394
+ throw new Error(`Unknown length variant: ${variant}`);
395
+ }
396
+ return baseValue / (unit.factor ?? 1);
397
+ }
398
+ function convertLength(from, to, value) {
399
+ if (from === to) {
400
+ return value;
401
+ }
402
+ const baseValue = toBaseLength(from, value);
403
+ return fromBaseLength(to, baseValue);
404
+ }
405
+
406
+ // src/roles/length/cast.ts
407
+ function castLength(input, targetVariant = "meter") {
408
+ if (typeof input === "number") {
409
+ return Number.isFinite(input) ? input : null;
410
+ }
411
+ if (typeof input === "string") {
412
+ return parseLengthString(input, targetVariant);
413
+ }
414
+ return null;
415
+ }
416
+ function tryCastLength(input, targetVariant = "meter") {
417
+ const result = castLength(input, targetVariant);
418
+ if (result === null) {
419
+ return {
420
+ ok: false,
421
+ error: `Cannot cast "${String(input)}" to length:${targetVariant}`
422
+ };
423
+ }
424
+ return { ok: true, value: result };
425
+ }
426
+ function parseLengthString(input, targetVariant) {
427
+ const trimmed = input.trim().toLowerCase();
428
+ const match = trimmed.match(/^([+-]?[\d.,\s]+)\s*(.*)$/);
429
+ if (!match) {
430
+ return null;
431
+ }
432
+ const [, numStr, unitStr] = match;
433
+ const numValue = parseNumber2(numStr);
434
+ if (numValue === null) {
435
+ return null;
436
+ }
437
+ const cleanUnitStr = unitStr.trim();
438
+ if (cleanUnitStr) {
439
+ const detectedUnit = detectUnit2(cleanUnitStr);
440
+ if (detectedUnit && detectedUnit !== targetVariant) {
441
+ return convertLength(detectedUnit, targetVariant, numValue);
442
+ }
443
+ }
444
+ return numValue;
445
+ }
446
+ function parseNumber2(numStr) {
447
+ numStr = numStr.replace(/\s/g, "");
448
+ const lastComma = numStr.lastIndexOf(",");
449
+ const lastDot = numStr.lastIndexOf(".");
450
+ const hasComma = lastComma !== -1;
451
+ const hasDot = lastDot !== -1;
452
+ if (hasComma && hasDot) {
453
+ if (lastComma > lastDot) {
454
+ numStr = numStr.replace(/\./g, "").replace(",", ".");
455
+ } else {
456
+ numStr = numStr.replace(/,/g, "");
457
+ }
458
+ } else if (hasComma && !hasDot) {
459
+ const afterComma = numStr.split(",").slice(1);
460
+ const isThousandSep = afterComma.every((part) => part.length === 3);
461
+ if (isThousandSep) {
462
+ numStr = numStr.replace(/,/g, "");
463
+ } else {
464
+ numStr = numStr.replace(",", ".");
465
+ }
466
+ } else if (!hasComma && hasDot) {
467
+ const afterDot = numStr.split(".").slice(1);
468
+ if (afterDot.length > 1) {
469
+ numStr = numStr.replace(/\./g, "");
470
+ }
471
+ }
472
+ const value = parseFloat(numStr);
473
+ return isNaN(value) ? null : value;
474
+ }
475
+ function detectUnit2(unitStr) {
476
+ const normalized = unitStr.toLowerCase().trim();
477
+ if (normalized in LENGTH_ALIASES) {
478
+ return LENGTH_ALIASES[normalized];
479
+ }
480
+ for (const [variant, config] of Object.entries(LENGTH_UNITS)) {
481
+ if (config.symbol.toLowerCase() === normalized || config.singular?.toLowerCase() === normalized || config.plural?.toLowerCase() === normalized || variant.toLowerCase() === normalized) {
482
+ return variant;
483
+ }
484
+ }
485
+ return null;
486
+ }
487
+
488
+ // src/roles/mass/constants.ts
489
+ var MASS_UNITS = {
490
+ // ===========================================================================
491
+ // SI / Metric
492
+ // ===========================================================================
493
+ metric_ton: {
494
+ factor: 1e3,
495
+ symbol: "t",
496
+ singular: "metric ton",
497
+ plural: "metric tons"
498
+ },
499
+ kilogram: {
500
+ factor: 1,
501
+ symbol: "kg",
502
+ singular: "kilogram",
503
+ plural: "kilograms"
504
+ },
505
+ hectogram: {
506
+ factor: 0.1,
507
+ symbol: "hg",
508
+ singular: "hectogram",
509
+ plural: "hectograms"
510
+ },
511
+ decagram: {
512
+ factor: 0.01,
513
+ symbol: "dag",
514
+ singular: "decagram",
515
+ plural: "decagrams"
516
+ },
517
+ gram: {
518
+ factor: 1e-3,
519
+ symbol: "g",
520
+ singular: "gram",
521
+ plural: "grams"
522
+ },
523
+ decigram: {
524
+ factor: 1e-4,
525
+ symbol: "dg",
526
+ singular: "decigram",
527
+ plural: "decigrams"
528
+ },
529
+ centigram: {
530
+ factor: 1e-5,
531
+ symbol: "cg",
532
+ singular: "centigram",
533
+ plural: "centigrams"
534
+ },
535
+ milligram: {
536
+ factor: 1e-6,
537
+ symbol: "mg",
538
+ singular: "milligram",
539
+ plural: "milligrams"
540
+ },
541
+ microgram: {
542
+ factor: 1e-9,
543
+ symbol: "\u03BCg",
544
+ singular: "microgram",
545
+ plural: "micrograms"
546
+ },
547
+ // ===========================================================================
548
+ // Avoirdupois (US/UK) - Sistema padrão para peso comum
549
+ // ===========================================================================
550
+ long_ton: {
551
+ factor: 1016.0469088,
552
+ // 2240 lb (exato)
553
+ symbol: "long tn",
554
+ singular: "long ton",
555
+ plural: "long tons"
556
+ },
557
+ short_ton: {
558
+ factor: 907.18474,
559
+ // 2000 lb (exato)
560
+ symbol: "sh tn",
561
+ singular: "short ton",
562
+ plural: "short tons"
563
+ },
564
+ stone: {
565
+ factor: 6.35029318,
566
+ // 14 lb (exato)
567
+ symbol: "st",
568
+ singular: "stone",
569
+ plural: "stone"
570
+ // stone não muda no plural em inglês
571
+ },
572
+ pound: {
573
+ factor: 0.45359237,
574
+ // Exato desde 1959
575
+ symbol: "lb",
576
+ singular: "pound",
577
+ plural: "pounds"
578
+ },
579
+ ounce: {
580
+ factor: 0.028349523125,
581
+ // 1/16 lb (exato)
582
+ symbol: "oz",
583
+ singular: "ounce",
584
+ plural: "ounces"
585
+ },
586
+ dram: {
587
+ factor: 0.0017718451953125,
588
+ // 1/16 oz (exato)
589
+ symbol: "dr",
590
+ singular: "dram",
591
+ plural: "drams"
592
+ },
593
+ grain: {
594
+ factor: 6479891e-11,
595
+ // 1/7000 lb (exato)
596
+ symbol: "gr",
597
+ singular: "grain",
598
+ plural: "grains"
599
+ },
600
+ // ===========================================================================
601
+ // Troy (metais preciosos)
602
+ // ===========================================================================
603
+ troy_pound: {
604
+ factor: 0.3732417216,
605
+ // 12 troy oz (exato)
606
+ symbol: "lb t",
607
+ singular: "troy pound",
608
+ plural: "troy pounds"
609
+ },
610
+ troy_ounce: {
611
+ factor: 0.0311034768,
612
+ // 480 grains (exato)
613
+ symbol: "oz t",
614
+ singular: "troy ounce",
615
+ plural: "troy ounces"
616
+ },
617
+ pennyweight: {
618
+ factor: 0.00155517384,
619
+ // 1/20 troy oz (exato)
620
+ symbol: "dwt",
621
+ singular: "pennyweight",
622
+ plural: "pennyweights"
623
+ }
624
+ };
625
+ var MASS_ALIASES = {
626
+ // metric_ton
627
+ t: "metric_ton",
628
+ // kilogram
629
+ kg: "kilogram",
630
+ // hectogram
631
+ hg: "hectogram",
632
+ // decagram
633
+ dag: "decagram",
634
+ // gram
635
+ g: "gram",
636
+ // decigram
637
+ dg: "decigram",
638
+ // centigram
639
+ cg: "centigram",
640
+ // milligram
641
+ mg: "milligram",
642
+ // microgram
643
+ "\u03BCg": "microgram",
644
+ ug: "microgram",
645
+ mcg: "microgram",
646
+ // long_ton
647
+ "long tn": "long_ton",
648
+ // short_ton
649
+ "sh tn": "short_ton",
650
+ // stone
651
+ st: "stone",
652
+ // pound
653
+ lb: "pound",
654
+ lbs: "pound",
655
+ "#": "pound",
656
+ // ounce
657
+ oz: "ounce",
658
+ // dram
659
+ dr: "dram",
660
+ // grain
661
+ gr: "grain",
662
+ // troy_pound
663
+ "lb t": "troy_pound",
664
+ // troy_ounce
665
+ "oz t": "troy_ounce",
666
+ ozt: "troy_ounce",
667
+ // pennyweight
668
+ dwt: "pennyweight"
669
+ };
670
+ Object.fromEntries(
671
+ Object.entries(MASS_UNITS).map(([unit, config]) => [
672
+ unit,
673
+ config.factor ?? 1
674
+ ])
675
+ );
676
+
677
+ // src/roles/mass/convert.ts
678
+ function toBaseMass(variant, value) {
679
+ const unit = MASS_UNITS[variant];
680
+ if (!unit) {
681
+ throw new Error(`Unknown mass variant: ${variant}`);
682
+ }
683
+ return value * (unit.factor ?? 1);
684
+ }
685
+ function fromBaseMass(variant, baseValue) {
686
+ const unit = MASS_UNITS[variant];
687
+ if (!unit) {
688
+ throw new Error(`Unknown mass variant: ${variant}`);
689
+ }
690
+ return baseValue / (unit.factor ?? 1);
691
+ }
692
+ function convertMass(from, to, value) {
693
+ if (from === to) {
694
+ return value;
695
+ }
696
+ const baseValue = toBaseMass(from, value);
697
+ return fromBaseMass(to, baseValue);
698
+ }
699
+
700
+ // src/roles/mass/cast.ts
701
+ function castMass(input, targetVariant = "kilogram") {
702
+ if (typeof input === "number") {
703
+ return Number.isFinite(input) ? input : null;
704
+ }
705
+ if (typeof input === "string") {
706
+ return parseMassString(input, targetVariant);
707
+ }
708
+ return null;
709
+ }
710
+ function tryCastMass(input, targetVariant = "kilogram") {
711
+ const result = castMass(input, targetVariant);
712
+ if (result === null) {
713
+ return {
714
+ ok: false,
715
+ error: `Cannot cast "${String(input)}" to mass:${targetVariant}`
716
+ };
717
+ }
718
+ return { ok: true, value: result };
719
+ }
720
+ function parseMassString(input, targetVariant) {
721
+ const trimmed = input.trim().toLowerCase();
722
+ const match = trimmed.match(/^([+-]?[\d.,\s]+)\s*(.*)$/);
723
+ if (!match) {
724
+ return null;
725
+ }
726
+ const [, numStr, unitStr] = match;
727
+ const numValue = parseNumber3(numStr);
728
+ if (numValue === null) {
729
+ return null;
730
+ }
731
+ const cleanUnitStr = unitStr.trim();
732
+ if (cleanUnitStr) {
733
+ const detectedUnit = detectUnit3(cleanUnitStr);
734
+ if (detectedUnit && detectedUnit !== targetVariant) {
735
+ return convertMass(detectedUnit, targetVariant, numValue);
736
+ }
737
+ }
738
+ return numValue;
739
+ }
740
+ function parseNumber3(numStr) {
741
+ numStr = numStr.replace(/\s/g, "");
742
+ const lastComma = numStr.lastIndexOf(",");
743
+ const lastDot = numStr.lastIndexOf(".");
744
+ const hasComma = lastComma !== -1;
745
+ const hasDot = lastDot !== -1;
746
+ if (hasComma && hasDot) {
747
+ if (lastComma > lastDot) {
748
+ numStr = numStr.replace(/\./g, "").replace(",", ".");
749
+ } else {
750
+ numStr = numStr.replace(/,/g, "");
751
+ }
752
+ } else if (hasComma && !hasDot) {
753
+ const afterComma = numStr.split(",").slice(1);
754
+ const isThousandSep = afterComma.every((part) => part.length === 3);
755
+ if (isThousandSep) {
756
+ numStr = numStr.replace(/,/g, "");
757
+ } else {
758
+ numStr = numStr.replace(",", ".");
759
+ }
760
+ } else if (!hasComma && hasDot) {
761
+ const afterDot = numStr.split(".").slice(1);
762
+ if (afterDot.length > 1) {
763
+ numStr = numStr.replace(/\./g, "");
764
+ }
765
+ }
766
+ const value = parseFloat(numStr);
767
+ return isNaN(value) ? null : value;
768
+ }
769
+ function detectUnit3(unitStr) {
770
+ const normalized = unitStr.toLowerCase().trim();
771
+ if (normalized in MASS_ALIASES) {
772
+ return MASS_ALIASES[normalized];
773
+ }
774
+ for (const [variant, config] of Object.entries(MASS_UNITS)) {
775
+ if (config.symbol.toLowerCase() === normalized || config.singular?.toLowerCase() === normalized || config.plural?.toLowerCase() === normalized || variant.toLowerCase() === normalized) {
776
+ return variant;
777
+ }
778
+ }
779
+ return null;
780
+ }
781
+
782
+ // src/roles/temperature/constants.ts
783
+ var TEMPERATURE_UNITS = {
784
+ celsius: {
785
+ symbol: "\xB0C",
786
+ singular: "degree Celsius",
787
+ plural: "degrees Celsius",
788
+ toBase: (c) => c,
789
+ fromBase: (c) => c,
790
+ noSpace: true
791
+ // 25°C não 25 °C
792
+ },
793
+ fahrenheit: {
794
+ symbol: "\xB0F",
795
+ singular: "degree Fahrenheit",
796
+ plural: "degrees Fahrenheit",
797
+ toBase: (f) => (f - 32) * (5 / 9),
798
+ fromBase: (c) => c * (9 / 5) + 32,
799
+ noSpace: true
800
+ // 77°F não 77 °F
801
+ },
802
+ kelvin: {
803
+ symbol: "K",
804
+ singular: "kelvin",
805
+ plural: "kelvins",
806
+ toBase: (k) => k - 273.15,
807
+ fromBase: (c) => c + 273.15
808
+ // Kelvin usa espaço: "273 K" (convenção SI)
809
+ },
810
+ rankine: {
811
+ symbol: "\xB0R",
812
+ singular: "degree Rankine",
813
+ plural: "degrees Rankine",
814
+ toBase: (r) => (r - 491.67) * (5 / 9),
815
+ fromBase: (c) => (c + 273.15) * (9 / 5),
816
+ noSpace: true
817
+ // 500°R não 500 °R
818
+ }
819
+ };
820
+ var TEMPERATURE_ALIASES = {
821
+ // celsius
822
+ c: "celsius",
823
+ "\xB0c": "celsius",
824
+ // fahrenheit
825
+ f: "fahrenheit",
826
+ "\xB0f": "fahrenheit",
827
+ // kelvin
828
+ k: "kelvin",
829
+ // rankine
830
+ r: "rankine",
831
+ "\xB0r": "rankine"
832
+ };
833
+
834
+ // src/roles/temperature/convert.ts
835
+ function toBaseTemperature(variant, value) {
836
+ const unit = TEMPERATURE_UNITS[variant];
837
+ if (!unit) {
838
+ throw new Error(`Unknown temperature variant: ${variant}`);
839
+ }
840
+ if (unit.toBase) {
841
+ return unit.toBase(value);
842
+ }
843
+ return value * (unit.factor ?? 1);
844
+ }
845
+ function fromBaseTemperature(variant, baseValue) {
846
+ const unit = TEMPERATURE_UNITS[variant];
847
+ if (!unit) {
848
+ throw new Error(`Unknown temperature variant: ${variant}`);
849
+ }
850
+ if (unit.fromBase) {
851
+ return unit.fromBase(baseValue);
852
+ }
853
+ return baseValue / (unit.factor ?? 1);
854
+ }
855
+ function convertTemperature(from, to, value) {
856
+ if (from === to) {
857
+ return value;
858
+ }
859
+ const baseValue = toBaseTemperature(from, value);
860
+ return fromBaseTemperature(to, baseValue);
861
+ }
862
+
863
+ // src/roles/temperature/cast.ts
864
+ function castTemperature(input, targetVariant = "celsius") {
865
+ if (typeof input === "number") {
866
+ return Number.isFinite(input) ? input : null;
867
+ }
868
+ if (typeof input === "string") {
869
+ return parseTemperatureString(input, targetVariant);
870
+ }
871
+ return null;
872
+ }
873
+ function tryCastTemperature(input, targetVariant = "celsius") {
874
+ const result = castTemperature(input, targetVariant);
875
+ if (result === null) {
876
+ return {
877
+ ok: false,
878
+ error: `Cannot cast "${String(input)}" to temperature:${targetVariant}`
879
+ };
880
+ }
881
+ return { ok: true, value: result };
882
+ }
883
+ function parseTemperatureString(input, targetVariant) {
884
+ const trimmed = input.trim().toLowerCase();
885
+ const match = trimmed.match(/^([+-]?[\d.,\s]+)\s*(.*)$/);
886
+ if (!match) {
887
+ return null;
888
+ }
889
+ const [, numStr, unitStr] = match;
890
+ const numValue = parseNumber4(numStr);
891
+ if (numValue === null) {
892
+ return null;
893
+ }
894
+ const cleanUnitStr = unitStr.trim();
895
+ if (cleanUnitStr) {
896
+ const detectedUnit = detectUnit4(cleanUnitStr);
897
+ if (detectedUnit && detectedUnit !== targetVariant) {
898
+ return convertTemperature(detectedUnit, targetVariant, numValue);
899
+ }
900
+ }
901
+ return numValue;
902
+ }
903
+ function parseNumber4(numStr) {
904
+ numStr = numStr.replace(/\s/g, "");
905
+ const lastComma = numStr.lastIndexOf(",");
906
+ const lastDot = numStr.lastIndexOf(".");
907
+ const hasComma = lastComma !== -1;
908
+ const hasDot = lastDot !== -1;
909
+ if (hasComma && hasDot) {
910
+ if (lastComma > lastDot) {
911
+ numStr = numStr.replace(/\./g, "").replace(",", ".");
912
+ } else {
913
+ numStr = numStr.replace(/,/g, "");
914
+ }
915
+ } else if (hasComma && !hasDot) {
916
+ const afterComma = numStr.split(",").slice(1);
917
+ const isThousandSep = afterComma.every((part) => part.length === 3);
918
+ if (isThousandSep) {
919
+ numStr = numStr.replace(/,/g, "");
920
+ } else {
921
+ numStr = numStr.replace(",", ".");
922
+ }
923
+ } else if (!hasComma && hasDot) {
924
+ const afterDot = numStr.split(".").slice(1);
925
+ if (afterDot.length > 1) {
926
+ numStr = numStr.replace(/\./g, "");
927
+ }
928
+ }
929
+ const value = parseFloat(numStr);
930
+ return isNaN(value) ? null : value;
931
+ }
932
+ function detectUnit4(unitStr) {
933
+ const normalized = unitStr.toLowerCase().trim();
934
+ if (normalized in TEMPERATURE_ALIASES) {
935
+ return TEMPERATURE_ALIASES[normalized];
936
+ }
937
+ for (const [variant, config] of Object.entries(TEMPERATURE_UNITS)) {
938
+ if (config.symbol.toLowerCase() === normalized || config.singular?.toLowerCase() === normalized || config.plural?.toLowerCase() === normalized || variant.toLowerCase() === normalized) {
939
+ return variant;
940
+ }
941
+ }
942
+ return null;
943
+ }
944
+
945
+ // src/roles/volume/constants.ts
946
+ var VOLUME_UNITS = {
947
+ // SI / Métrico
948
+ cubic_meter: {
949
+ factor: 1e3,
950
+ symbol: "m\xB3",
951
+ singular: "cubic meter",
952
+ plural: "cubic meters"
953
+ },
954
+ cubic_decimeter: {
955
+ factor: 1,
956
+ symbol: "dm\xB3",
957
+ singular: "cubic decimeter",
958
+ plural: "cubic decimeters"
959
+ },
960
+ cubic_centimeter: {
961
+ factor: 1e-3,
962
+ symbol: "cm\xB3",
963
+ singular: "cubic centimeter",
964
+ plural: "cubic centimeters"
965
+ },
966
+ cubic_millimeter: {
967
+ factor: 1e-6,
968
+ symbol: "mm\xB3",
969
+ singular: "cubic millimeter",
970
+ plural: "cubic millimeters"
971
+ },
972
+ hectoliter: {
973
+ factor: 100,
974
+ symbol: "hL",
975
+ singular: "hectoliter",
976
+ plural: "hectoliters"
977
+ },
978
+ decaliter: {
979
+ factor: 10,
980
+ symbol: "daL",
981
+ singular: "decaliter",
982
+ plural: "decaliters"
983
+ },
984
+ liter: {
985
+ factor: 1,
986
+ symbol: "L",
987
+ singular: "liter",
988
+ plural: "liters"
989
+ },
990
+ deciliter: {
991
+ factor: 0.1,
992
+ symbol: "dL",
993
+ singular: "deciliter",
994
+ plural: "deciliters"
995
+ },
996
+ centiliter: {
997
+ factor: 0.01,
998
+ symbol: "cL",
999
+ singular: "centiliter",
1000
+ plural: "centiliters"
1001
+ },
1002
+ milliliter: {
1003
+ factor: 1e-3,
1004
+ symbol: "mL",
1005
+ singular: "milliliter",
1006
+ plural: "milliliters"
1007
+ },
1008
+ microliter: {
1009
+ factor: 1e-6,
1010
+ symbol: "\u03BCL",
1011
+ singular: "microliter",
1012
+ plural: "microliters"
1013
+ },
1014
+ // US Customary (baseado em 1 gallon = 231 in³ = 3.785411784 L)
1015
+ gallon_us: {
1016
+ factor: 3.785411784,
1017
+ symbol: "gal",
1018
+ singular: "gallon",
1019
+ plural: "gallons"
1020
+ },
1021
+ quart_us: {
1022
+ factor: 0.946352946,
1023
+ // gallon/4
1024
+ symbol: "qt",
1025
+ singular: "quart",
1026
+ plural: "quarts"
1027
+ },
1028
+ pint_us: {
1029
+ factor: 0.473176473,
1030
+ // gallon/8
1031
+ symbol: "pt",
1032
+ singular: "pint",
1033
+ plural: "pints"
1034
+ },
1035
+ cup_us: {
1036
+ factor: 0.2365882365,
1037
+ // gallon/16
1038
+ symbol: "cup",
1039
+ singular: "cup",
1040
+ plural: "cups"
1041
+ },
1042
+ fluid_ounce_us: {
1043
+ factor: 0.0295735295625,
1044
+ // gallon/128
1045
+ symbol: "fl oz",
1046
+ singular: "fluid ounce",
1047
+ plural: "fluid ounces"
1048
+ },
1049
+ tablespoon_us: {
1050
+ factor: 0.01478676478125,
1051
+ // fl oz/2
1052
+ symbol: "tbsp",
1053
+ singular: "tablespoon",
1054
+ plural: "tablespoons"
1055
+ },
1056
+ teaspoon_us: {
1057
+ factor: 0.00492892159375,
1058
+ // tbsp/3
1059
+ symbol: "tsp",
1060
+ singular: "teaspoon",
1061
+ plural: "teaspoons"
1062
+ },
1063
+ // Imperial UK (1 gallon UK = 4.54609 L exato)
1064
+ gallon_uk: {
1065
+ factor: 4.54609,
1066
+ symbol: "gal (UK)",
1067
+ singular: "gallon (UK)",
1068
+ plural: "gallons (UK)"
1069
+ },
1070
+ quart_uk: {
1071
+ factor: 1.1365225,
1072
+ // gallon/4
1073
+ symbol: "qt (UK)",
1074
+ singular: "quart (UK)",
1075
+ plural: "quarts (UK)"
1076
+ },
1077
+ pint_uk: {
1078
+ factor: 0.56826125,
1079
+ // gallon/8
1080
+ symbol: "pt (UK)",
1081
+ singular: "pint (UK)",
1082
+ plural: "pints (UK)"
1083
+ },
1084
+ fluid_ounce_uk: {
1085
+ factor: 0.0284130625,
1086
+ // gallon/160
1087
+ symbol: "fl oz (UK)",
1088
+ singular: "fluid ounce (UK)",
1089
+ plural: "fluid ounces (UK)"
1090
+ },
1091
+ // Other
1092
+ barrel_oil: {
1093
+ factor: 158.987294928,
1094
+ // 42 US gallons (petroleum)
1095
+ symbol: "bbl",
1096
+ singular: "barrel",
1097
+ plural: "barrels"
1098
+ },
1099
+ cubic_inch: {
1100
+ factor: 0.016387064,
1101
+ // (0.0254)³ × 1000
1102
+ symbol: "in\xB3",
1103
+ singular: "cubic inch",
1104
+ plural: "cubic inches"
1105
+ },
1106
+ cubic_foot: {
1107
+ factor: 28.316846592,
1108
+ // (0.3048)³ × 1000
1109
+ symbol: "ft\xB3",
1110
+ singular: "cubic foot",
1111
+ plural: "cubic feet"
1112
+ },
1113
+ cubic_yard: {
1114
+ factor: 764.554857984,
1115
+ // (0.9144)³ × 1000
1116
+ symbol: "yd\xB3",
1117
+ singular: "cubic yard",
1118
+ plural: "cubic yards"
1119
+ }
1120
+ };
1121
+ var VOLUME_ALIASES = {
1122
+ // cubic_meter
1123
+ "m\xB3": "cubic_meter",
1124
+ m3: "cubic_meter",
1125
+ // cubic_decimeter
1126
+ "dm\xB3": "cubic_decimeter",
1127
+ dm3: "cubic_decimeter",
1128
+ // cubic_centimeter
1129
+ "cm\xB3": "cubic_centimeter",
1130
+ cm3: "cubic_centimeter",
1131
+ cc: "cubic_centimeter",
1132
+ // cubic_millimeter
1133
+ "mm\xB3": "cubic_millimeter",
1134
+ mm3: "cubic_millimeter",
1135
+ // hectoliter
1136
+ hL: "hectoliter",
1137
+ hl: "hectoliter",
1138
+ // decaliter
1139
+ daL: "decaliter",
1140
+ dal: "decaliter",
1141
+ // liter
1142
+ L: "liter",
1143
+ l: "liter",
1144
+ // deciliter
1145
+ dL: "deciliter",
1146
+ dl: "deciliter",
1147
+ // centiliter
1148
+ cL: "centiliter",
1149
+ cl: "centiliter",
1150
+ // milliliter
1151
+ mL: "milliliter",
1152
+ ml: "milliliter",
1153
+ // microliter
1154
+ "\u03BCL": "microliter",
1155
+ uL: "microliter",
1156
+ ul: "microliter",
1157
+ // gallon_us
1158
+ gal: "gallon_us",
1159
+ // quart_us
1160
+ qt: "quart_us",
1161
+ // pint_us
1162
+ pt: "pint_us",
1163
+ // cup_us
1164
+ cup: "cup_us",
1165
+ // fluid_ounce_us
1166
+ "fl oz": "fluid_ounce_us",
1167
+ floz: "fluid_ounce_us",
1168
+ // tablespoon_us
1169
+ tbsp: "tablespoon_us",
1170
+ // teaspoon_us
1171
+ tsp: "teaspoon_us",
1172
+ // gallon_uk
1173
+ "gal uk": "gallon_uk",
1174
+ // quart_uk
1175
+ "qt uk": "quart_uk",
1176
+ // pint_uk
1177
+ "pt uk": "pint_uk",
1178
+ // fluid_ounce_uk
1179
+ "fl oz uk": "fluid_ounce_uk",
1180
+ // barrel_oil
1181
+ bbl: "barrel_oil",
1182
+ // cubic_inch
1183
+ "in\xB3": "cubic_inch",
1184
+ in3: "cubic_inch",
1185
+ // cubic_foot
1186
+ "ft\xB3": "cubic_foot",
1187
+ ft3: "cubic_foot",
1188
+ // cubic_yard
1189
+ "yd\xB3": "cubic_yard",
1190
+ yd3: "cubic_yard"
1191
+ };
1192
+ Object.fromEntries(
1193
+ Object.entries(VOLUME_UNITS).map(([unit, config]) => [
1194
+ unit,
1195
+ config.factor ?? 1
1196
+ ])
1197
+ );
1198
+
1199
+ // src/roles/volume/convert.ts
1200
+ function toBaseVolume(variant, value) {
1201
+ const unit = VOLUME_UNITS[variant];
1202
+ if (!unit) {
1203
+ throw new Error(`Unknown volume variant: ${variant}`);
1204
+ }
1205
+ return value * (unit.factor ?? 1);
1206
+ }
1207
+ function fromBaseVolume(variant, baseValue) {
1208
+ const unit = VOLUME_UNITS[variant];
1209
+ if (!unit) {
1210
+ throw new Error(`Unknown volume variant: ${variant}`);
1211
+ }
1212
+ return baseValue / (unit.factor ?? 1);
1213
+ }
1214
+ function convertVolume(from, to, value) {
1215
+ if (from === to) {
1216
+ return value;
1217
+ }
1218
+ const baseValue = toBaseVolume(from, value);
1219
+ return fromBaseVolume(to, baseValue);
1220
+ }
1221
+
1222
+ // src/roles/volume/cast.ts
1223
+ function castVolume(input, targetVariant = "liter") {
1224
+ if (typeof input === "number") {
1225
+ return Number.isFinite(input) ? input : null;
1226
+ }
1227
+ if (typeof input === "string") {
1228
+ return parseVolumeString(input, targetVariant);
1229
+ }
1230
+ return null;
1231
+ }
1232
+ function tryCastVolume(input, targetVariant = "liter") {
1233
+ const result = castVolume(input, targetVariant);
1234
+ if (result === null) {
1235
+ return {
1236
+ ok: false,
1237
+ error: `Cannot cast "${String(input)}" to volume:${targetVariant}`
1238
+ };
1239
+ }
1240
+ return { ok: true, value: result };
1241
+ }
1242
+ function parseVolumeString(input, targetVariant) {
1243
+ const trimmed = input.trim().toLowerCase();
1244
+ const match = trimmed.match(/^([+-]?[\d.,\s]+)\s*(.*)$/);
1245
+ if (!match) {
1246
+ return null;
1247
+ }
1248
+ const [, numStr, unitStr] = match;
1249
+ const numValue = parseNumber5(numStr);
1250
+ if (numValue === null) {
1251
+ return null;
1252
+ }
1253
+ const cleanUnitStr = unitStr.trim();
1254
+ if (cleanUnitStr) {
1255
+ const detectedUnit = detectUnit5(cleanUnitStr);
1256
+ if (detectedUnit && detectedUnit !== targetVariant) {
1257
+ return convertVolume(detectedUnit, targetVariant, numValue);
1258
+ }
1259
+ }
1260
+ return numValue;
1261
+ }
1262
+ function parseNumber5(numStr) {
1263
+ numStr = numStr.replace(/\s/g, "");
1264
+ const lastComma = numStr.lastIndexOf(",");
1265
+ const lastDot = numStr.lastIndexOf(".");
1266
+ const hasComma = lastComma !== -1;
1267
+ const hasDot = lastDot !== -1;
1268
+ if (hasComma && hasDot) {
1269
+ if (lastComma > lastDot) {
1270
+ numStr = numStr.replace(/\./g, "").replace(",", ".");
1271
+ } else {
1272
+ numStr = numStr.replace(/,/g, "");
1273
+ }
1274
+ } else if (hasComma && !hasDot) {
1275
+ const afterComma = numStr.split(",").slice(1);
1276
+ const isThousandSep = afterComma.every((part) => part.length === 3);
1277
+ if (isThousandSep) {
1278
+ numStr = numStr.replace(/,/g, "");
1279
+ } else {
1280
+ numStr = numStr.replace(",", ".");
1281
+ }
1282
+ } else if (!hasComma && hasDot) {
1283
+ const afterDot = numStr.split(".").slice(1);
1284
+ if (afterDot.length > 1) {
1285
+ numStr = numStr.replace(/\./g, "");
1286
+ }
1287
+ }
1288
+ const value = parseFloat(numStr);
1289
+ return isNaN(value) ? null : value;
1290
+ }
1291
+ function detectUnit5(unitStr) {
1292
+ const normalized = unitStr.toLowerCase().trim();
1293
+ if (normalized in VOLUME_ALIASES) {
1294
+ return VOLUME_ALIASES[normalized];
1295
+ }
1296
+ for (const [variant, config] of Object.entries(VOLUME_UNITS)) {
1297
+ if (config.symbol.toLowerCase() === normalized || config.singular?.toLowerCase() === normalized || config.plural?.toLowerCase() === normalized || variant.toLowerCase() === normalized) {
1298
+ return variant;
1299
+ }
1300
+ }
1301
+ return null;
1302
+ }
1303
+
1304
+ // src/roles/speed/constants.ts
1305
+ var SPEED_UNITS = {
1306
+ // SI Units
1307
+ meter_per_second: {
1308
+ factor: 1,
1309
+ symbol: "m/s",
1310
+ singular: "meter per second",
1311
+ plural: "meters per second"
1312
+ },
1313
+ kilometer_per_hour: {
1314
+ factor: 1e3 / 3600,
1315
+ // 0.277777...
1316
+ symbol: "km/h",
1317
+ singular: "kilometer per hour",
1318
+ plural: "kilometers per hour"
1319
+ },
1320
+ // Imperial/US (exatos desde 1959)
1321
+ mile_per_hour: {
1322
+ factor: 0.44704,
1323
+ // 1609.344 / 3600 (exato)
1324
+ symbol: "mph",
1325
+ singular: "mile per hour",
1326
+ plural: "miles per hour"
1327
+ },
1328
+ foot_per_second: {
1329
+ factor: 0.3048,
1330
+ // exato
1331
+ symbol: "ft/s",
1332
+ singular: "foot per second",
1333
+ plural: "feet per second"
1334
+ },
1335
+ // Nautical (exato)
1336
+ knot: {
1337
+ factor: 1852 / 3600,
1338
+ // 0.514444...
1339
+ symbol: "kn",
1340
+ singular: "knot",
1341
+ plural: "knots"
1342
+ },
1343
+ // Scientific
1344
+ mach: {
1345
+ factor: 340.29,
1346
+ // velocidade do som ao nível do mar, 15°C (aproximado)
1347
+ symbol: "Ma",
1348
+ singular: "mach",
1349
+ plural: "mach"
1350
+ },
1351
+ speed_of_light: {
1352
+ factor: 299792458,
1353
+ // exato por definição SI
1354
+ symbol: "c",
1355
+ singular: "speed of light",
1356
+ plural: "speed of light"
1357
+ }
1358
+ };
1359
+ var SPEED_ALIASES = {
1360
+ // meter_per_second
1361
+ "m/s": "meter_per_second",
1362
+ mps: "meter_per_second",
1363
+ // kilometer_per_hour
1364
+ "km/h": "kilometer_per_hour",
1365
+ kmh: "kilometer_per_hour",
1366
+ kph: "kilometer_per_hour",
1367
+ // mile_per_hour
1368
+ mph: "mile_per_hour",
1369
+ "mi/h": "mile_per_hour",
1370
+ // foot_per_second
1371
+ "ft/s": "foot_per_second",
1372
+ fps: "foot_per_second",
1373
+ // knot
1374
+ kn: "knot",
1375
+ kt: "knot",
1376
+ kts: "knot",
1377
+ // mach
1378
+ ma: "mach",
1379
+ // speed_of_light
1380
+ c: "speed_of_light"
1381
+ };
1382
+ Object.fromEntries(
1383
+ Object.entries(SPEED_UNITS).map(([unit, config]) => [
1384
+ unit,
1385
+ config.factor ?? 1
1386
+ ])
1387
+ );
1388
+
1389
+ // src/roles/speed/convert.ts
1390
+ function toBaseSpeed(variant, value) {
1391
+ const unit = SPEED_UNITS[variant];
1392
+ if (!unit) {
1393
+ throw new Error(`Unknown speed variant: ${variant}`);
1394
+ }
1395
+ return value * (unit.factor ?? 1);
1396
+ }
1397
+ function fromBaseSpeed(variant, baseValue) {
1398
+ const unit = SPEED_UNITS[variant];
1399
+ if (!unit) {
1400
+ throw new Error(`Unknown speed variant: ${variant}`);
1401
+ }
1402
+ return baseValue / (unit.factor ?? 1);
1403
+ }
1404
+ function convertSpeed(from, to, value) {
1405
+ if (from === to) {
1406
+ return value;
1407
+ }
1408
+ const baseValue = toBaseSpeed(from, value);
1409
+ return fromBaseSpeed(to, baseValue);
1410
+ }
1411
+
1412
+ // src/roles/speed/cast.ts
1413
+ function castSpeed(input, targetVariant = "meter_per_second") {
1414
+ if (typeof input === "number") {
1415
+ return Number.isFinite(input) ? input : null;
1416
+ }
1417
+ if (typeof input === "string") {
1418
+ return parseSpeedString(input, targetVariant);
1419
+ }
1420
+ return null;
1421
+ }
1422
+ function tryCastSpeed(input, targetVariant = "meter_per_second") {
1423
+ const result = castSpeed(input, targetVariant);
1424
+ if (result === null) {
1425
+ return {
1426
+ ok: false,
1427
+ error: `Cannot cast "${String(input)}" to speed:${targetVariant}`
1428
+ };
1429
+ }
1430
+ return { ok: true, value: result };
1431
+ }
1432
+ function parseSpeedString(input, targetVariant) {
1433
+ const trimmed = input.trim().toLowerCase();
1434
+ const match = trimmed.match(/^([+-]?[\d.,\s]+)\s*(.*)$/);
1435
+ if (!match) {
1436
+ return null;
1437
+ }
1438
+ const [, numStr, unitStr] = match;
1439
+ const numValue = parseNumber6(numStr);
1440
+ if (numValue === null) {
1441
+ return null;
1442
+ }
1443
+ const cleanUnitStr = unitStr.trim();
1444
+ if (cleanUnitStr) {
1445
+ const detectedUnit = detectUnit6(cleanUnitStr);
1446
+ if (detectedUnit && detectedUnit !== targetVariant) {
1447
+ return convertSpeed(detectedUnit, targetVariant, numValue);
1448
+ }
1449
+ }
1450
+ return numValue;
1451
+ }
1452
+ function parseNumber6(numStr) {
1453
+ numStr = numStr.replace(/\s/g, "");
1454
+ const lastComma = numStr.lastIndexOf(",");
1455
+ const lastDot = numStr.lastIndexOf(".");
1456
+ const hasComma = lastComma !== -1;
1457
+ const hasDot = lastDot !== -1;
1458
+ if (hasComma && hasDot) {
1459
+ if (lastComma > lastDot) {
1460
+ numStr = numStr.replace(/\./g, "").replace(",", ".");
1461
+ } else {
1462
+ numStr = numStr.replace(/,/g, "");
1463
+ }
1464
+ } else if (hasComma && !hasDot) {
1465
+ const afterComma = numStr.split(",").slice(1);
1466
+ const isThousandSep = afterComma.every((part) => part.length === 3);
1467
+ if (isThousandSep) {
1468
+ numStr = numStr.replace(/,/g, "");
1469
+ } else {
1470
+ numStr = numStr.replace(",", ".");
1471
+ }
1472
+ } else if (!hasComma && hasDot) {
1473
+ const afterDot = numStr.split(".").slice(1);
1474
+ if (afterDot.length > 1) {
1475
+ numStr = numStr.replace(/\./g, "");
1476
+ }
1477
+ }
1478
+ const value = parseFloat(numStr);
1479
+ return isNaN(value) ? null : value;
1480
+ }
1481
+ function detectUnit6(unitStr) {
1482
+ const normalized = unitStr.toLowerCase().trim();
1483
+ if (normalized in SPEED_ALIASES) {
1484
+ return SPEED_ALIASES[normalized];
1485
+ }
1486
+ for (const [variant, config] of Object.entries(SPEED_UNITS)) {
1487
+ if (config.symbol.toLowerCase() === normalized || config.singular?.toLowerCase() === normalized || config.plural?.toLowerCase() === normalized || variant.toLowerCase() === normalized) {
1488
+ return variant;
1489
+ }
1490
+ }
1491
+ return null;
1492
+ }
1493
+
1494
+ // src/roles/energy/constants.ts
1495
+ var ENERGY_UNITS = {
1496
+ // SI Units (todos exatos)
1497
+ gigajoule: {
1498
+ factor: 1e9,
1499
+ symbol: "GJ",
1500
+ singular: "gigajoule",
1501
+ plural: "gigajoules"
1502
+ },
1503
+ megajoule: {
1504
+ factor: 1e6,
1505
+ symbol: "MJ",
1506
+ singular: "megajoule",
1507
+ plural: "megajoules"
1508
+ },
1509
+ kilojoule: {
1510
+ factor: 1e3,
1511
+ symbol: "kJ",
1512
+ singular: "kilojoule",
1513
+ plural: "kilojoules"
1514
+ },
1515
+ joule: {
1516
+ factor: 1,
1517
+ symbol: "J",
1518
+ singular: "joule",
1519
+ plural: "joules"
1520
+ },
1521
+ millijoule: {
1522
+ factor: 1e-3,
1523
+ symbol: "mJ",
1524
+ singular: "millijoule",
1525
+ plural: "millijoules"
1526
+ },
1527
+ // Calories (International Table - IT)
1528
+ // 1 cal (IT) = 4.1868 J (exato por definição)
1529
+ calorie: {
1530
+ factor: 4.1868,
1531
+ symbol: "cal",
1532
+ singular: "calorie",
1533
+ plural: "calories"
1534
+ },
1535
+ // 1 kcal = 1000 cal = 4186.8 J (= 1 "food Calorie")
1536
+ kilocalorie: {
1537
+ factor: 4186.8,
1538
+ symbol: "kcal",
1539
+ singular: "kilocalorie",
1540
+ plural: "kilocalories"
1541
+ },
1542
+ // Watt-hour (exatos)
1543
+ // 1 Wh = 1 W × 3600 s = 3600 J
1544
+ watt_hour: {
1545
+ factor: 3600,
1546
+ symbol: "Wh",
1547
+ singular: "watt-hour",
1548
+ plural: "watt-hours"
1549
+ },
1550
+ kilowatt_hour: {
1551
+ factor: 36e5,
1552
+ symbol: "kWh",
1553
+ singular: "kilowatt-hour",
1554
+ plural: "kilowatt-hours"
1555
+ },
1556
+ megawatt_hour: {
1557
+ factor: 36e8,
1558
+ symbol: "MWh",
1559
+ singular: "megawatt-hour",
1560
+ plural: "megawatt-hours"
1561
+ },
1562
+ gigawatt_hour: {
1563
+ factor: 36e11,
1564
+ symbol: "GWh",
1565
+ singular: "gigawatt-hour",
1566
+ plural: "gigawatt-hours"
1567
+ },
1568
+ // BTU (International Table)
1569
+ // 1 BTU (IT) = 1055.05585262 J (definição)
1570
+ btu: {
1571
+ factor: 1055.05585262,
1572
+ symbol: "BTU",
1573
+ singular: "BTU",
1574
+ plural: "BTUs"
1575
+ },
1576
+ // 1 therm = 100,000 BTU (IT)
1577
+ therm: {
1578
+ factor: 105505585262e-3,
1579
+ symbol: "thm",
1580
+ singular: "therm",
1581
+ plural: "therms"
1582
+ },
1583
+ // Scientific
1584
+ // 1 eV = 1.602176634e-19 J (SI 2019, exato por definição)
1585
+ electronvolt: {
1586
+ factor: 1602176634e-28,
1587
+ symbol: "eV",
1588
+ singular: "electronvolt",
1589
+ plural: "electronvolts"
1590
+ },
1591
+ kiloelectronvolt: {
1592
+ factor: 1602176634e-25,
1593
+ symbol: "keV",
1594
+ singular: "kiloelectronvolt",
1595
+ plural: "kiloelectronvolts"
1596
+ },
1597
+ megaelectronvolt: {
1598
+ factor: 1602176634e-22,
1599
+ symbol: "MeV",
1600
+ singular: "megaelectronvolt",
1601
+ plural: "megaelectronvolts"
1602
+ },
1603
+ // 1 erg = 1e-7 J (CGS, exato)
1604
+ erg: {
1605
+ factor: 1e-7,
1606
+ symbol: "erg",
1607
+ singular: "erg",
1608
+ plural: "ergs"
1609
+ },
1610
+ // Mechanical
1611
+ // 1 ft⋅lbf = 1.3558179483314004 J (derivado de foot e pound-force)
1612
+ foot_pound: {
1613
+ factor: 1.3558179483314003,
1614
+ symbol: "ft\u22C5lbf",
1615
+ singular: "foot-pound",
1616
+ plural: "foot-pounds"
1617
+ }
1618
+ };
1619
+ var ENERGY_ALIASES = {
1620
+ // gigajoule
1621
+ gj: "gigajoule",
1622
+ // megajoule
1623
+ mj: "megajoule",
1624
+ // kilojoule
1625
+ kj: "kilojoule",
1626
+ // joule
1627
+ j: "joule",
1628
+ // calorie
1629
+ cal: "calorie",
1630
+ // kilocalorie
1631
+ kcal: "kilocalorie",
1632
+ Cal: "kilocalorie",
1633
+ // watt_hour
1634
+ wh: "watt_hour",
1635
+ // kilowatt_hour
1636
+ kwh: "kilowatt_hour",
1637
+ // megawatt_hour
1638
+ mwh: "megawatt_hour",
1639
+ // gigawatt_hour
1640
+ gwh: "gigawatt_hour",
1641
+ // btu
1642
+ btu: "btu",
1643
+ // therm
1644
+ thm: "therm",
1645
+ // electronvolt
1646
+ ev: "electronvolt",
1647
+ // kiloelectronvolt
1648
+ kev: "kiloelectronvolt",
1649
+ // megaelectronvolt
1650
+ mev: "megaelectronvolt",
1651
+ // erg
1652
+ erg: "erg",
1653
+ // foot_pound
1654
+ "ft-lb": "foot_pound",
1655
+ "ft-lbf": "foot_pound"
1656
+ };
1657
+ Object.fromEntries(
1658
+ Object.entries(ENERGY_UNITS).map(([unit, config]) => [
1659
+ unit,
1660
+ config.factor ?? 1
1661
+ ])
1662
+ );
1663
+
1664
+ // src/roles/energy/convert.ts
1665
+ function toBaseEnergy(variant, value) {
1666
+ const unit = ENERGY_UNITS[variant];
1667
+ if (!unit) {
1668
+ throw new Error(`Unknown energy variant: ${variant}`);
1669
+ }
1670
+ return value * (unit.factor ?? 1);
1671
+ }
1672
+ function fromBaseEnergy(variant, baseValue) {
1673
+ const unit = ENERGY_UNITS[variant];
1674
+ if (!unit) {
1675
+ throw new Error(`Unknown energy variant: ${variant}`);
1676
+ }
1677
+ return baseValue / (unit.factor ?? 1);
1678
+ }
1679
+ function convertEnergy(from, to, value) {
1680
+ if (from === to) {
1681
+ return value;
1682
+ }
1683
+ const baseValue = toBaseEnergy(from, value);
1684
+ return fromBaseEnergy(to, baseValue);
1685
+ }
1686
+
1687
+ // src/roles/energy/cast.ts
1688
+ function castEnergy(input, targetVariant = "joule") {
1689
+ if (typeof input === "number") {
1690
+ return Number.isFinite(input) ? input : null;
1691
+ }
1692
+ if (typeof input === "string") {
1693
+ return parseEnergyString(input, targetVariant);
1694
+ }
1695
+ return null;
1696
+ }
1697
+ function tryCastEnergy(input, targetVariant = "joule") {
1698
+ const result = castEnergy(input, targetVariant);
1699
+ if (result === null) {
1700
+ return {
1701
+ ok: false,
1702
+ error: `Cannot cast "${String(input)}" to energy:${targetVariant}`
1703
+ };
1704
+ }
1705
+ return { ok: true, value: result };
1706
+ }
1707
+ function parseEnergyString(input, targetVariant) {
1708
+ const trimmed = input.trim().toLowerCase();
1709
+ const match = trimmed.match(/^([+-]?[\d.,\s]+)\s*(.*)$/);
1710
+ if (!match) {
1711
+ return null;
1712
+ }
1713
+ const [, numStr, unitStr] = match;
1714
+ const numValue = parseNumber7(numStr);
1715
+ if (numValue === null) {
1716
+ return null;
1717
+ }
1718
+ const cleanUnitStr = unitStr.trim();
1719
+ if (cleanUnitStr) {
1720
+ const detectedUnit = detectUnit7(cleanUnitStr);
1721
+ if (detectedUnit && detectedUnit !== targetVariant) {
1722
+ return convertEnergy(detectedUnit, targetVariant, numValue);
1723
+ }
1724
+ }
1725
+ return numValue;
1726
+ }
1727
+ function parseNumber7(numStr) {
1728
+ numStr = numStr.replace(/\s/g, "");
1729
+ const lastComma = numStr.lastIndexOf(",");
1730
+ const lastDot = numStr.lastIndexOf(".");
1731
+ const hasComma = lastComma !== -1;
1732
+ const hasDot = lastDot !== -1;
1733
+ if (hasComma && hasDot) {
1734
+ if (lastComma > lastDot) {
1735
+ numStr = numStr.replace(/\./g, "").replace(",", ".");
1736
+ } else {
1737
+ numStr = numStr.replace(/,/g, "");
1738
+ }
1739
+ } else if (hasComma && !hasDot) {
1740
+ const afterComma = numStr.split(",").slice(1);
1741
+ const isThousandSep = afterComma.every((part) => part.length === 3);
1742
+ if (isThousandSep) {
1743
+ numStr = numStr.replace(/,/g, "");
1744
+ } else {
1745
+ numStr = numStr.replace(",", ".");
1746
+ }
1747
+ } else if (!hasComma && hasDot) {
1748
+ const afterDot = numStr.split(".").slice(1);
1749
+ if (afterDot.length > 1) {
1750
+ numStr = numStr.replace(/\./g, "");
1751
+ }
1752
+ }
1753
+ const value = parseFloat(numStr);
1754
+ return isNaN(value) ? null : value;
1755
+ }
1756
+ function detectUnit7(unitStr) {
1757
+ const normalized = unitStr.toLowerCase().trim();
1758
+ if (normalized in ENERGY_ALIASES) {
1759
+ return ENERGY_ALIASES[normalized];
1760
+ }
1761
+ for (const [variant, config] of Object.entries(ENERGY_UNITS)) {
1762
+ if (config.symbol.toLowerCase() === normalized || config.singular?.toLowerCase() === normalized || config.plural?.toLowerCase() === normalized || variant.toLowerCase() === normalized) {
1763
+ return variant;
1764
+ }
1765
+ }
1766
+ return null;
1767
+ }
1768
+
1769
+ // src/roles/power/constants.ts
1770
+ var POWER_UNITS = {
1771
+ // SI Units (todos exatos)
1772
+ gigawatt: {
1773
+ factor: 1e9,
1774
+ symbol: "GW",
1775
+ singular: "gigawatt",
1776
+ plural: "gigawatts"
1777
+ },
1778
+ megawatt: {
1779
+ factor: 1e6,
1780
+ symbol: "MW",
1781
+ singular: "megawatt",
1782
+ plural: "megawatts"
1783
+ },
1784
+ kilowatt: {
1785
+ factor: 1e3,
1786
+ symbol: "kW",
1787
+ singular: "kilowatt",
1788
+ plural: "kilowatts"
1789
+ },
1790
+ watt: {
1791
+ factor: 1,
1792
+ symbol: "W",
1793
+ singular: "watt",
1794
+ plural: "watts"
1795
+ },
1796
+ milliwatt: {
1797
+ factor: 1e-3,
1798
+ symbol: "mW",
1799
+ singular: "milliwatt",
1800
+ plural: "milliwatts"
1801
+ },
1802
+ microwatt: {
1803
+ factor: 1e-6,
1804
+ symbol: "\u03BCW",
1805
+ singular: "microwatt",
1806
+ plural: "microwatts"
1807
+ },
1808
+ // Horsepower variants
1809
+ // Mechanical (imperial): 550 ft⋅lbf/s
1810
+ // = 550 × 0.3048 m × 4.4482216152605 N / s = 745.69987158227 W
1811
+ horsepower_mechanical: {
1812
+ factor: 745.69987158227,
1813
+ symbol: "hp",
1814
+ singular: "horsepower",
1815
+ plural: "horsepower"
1816
+ },
1817
+ // Metric (PS, CV, pk): 75 kgf⋅m/s = 75 × 9.80665 W = 735.49875 W (exato)
1818
+ horsepower_metric: {
1819
+ factor: 735.49875,
1820
+ symbol: "PS",
1821
+ singular: "metric horsepower",
1822
+ plural: "metric horsepower"
1823
+ },
1824
+ // Electrical: 746 W (exato por definição)
1825
+ horsepower_electric: {
1826
+ factor: 746,
1827
+ symbol: "hp(E)",
1828
+ singular: "electric horsepower",
1829
+ plural: "electric horsepower"
1830
+ },
1831
+ // Boiler: 33,475 BTU/h = 9809.5 W
1832
+ horsepower_boiler: {
1833
+ factor: 9809.5,
1834
+ symbol: "hp(S)",
1835
+ singular: "boiler horsepower",
1836
+ plural: "boiler horsepower"
1837
+ },
1838
+ // BTU-based
1839
+ // 1 BTU/h = 1055.05585262 J / 3600 s = 0.29307107017222 W
1840
+ btu_per_hour: {
1841
+ factor: 0.29307107017222,
1842
+ symbol: "BTU/h",
1843
+ singular: "BTU per hour",
1844
+ plural: "BTUs per hour"
1845
+ },
1846
+ // 1 BTU/s = 1055.05585262 W
1847
+ btu_per_second: {
1848
+ factor: 1055.05585262,
1849
+ symbol: "BTU/s",
1850
+ singular: "BTU per second",
1851
+ plural: "BTUs per second"
1852
+ },
1853
+ // Other
1854
+ // 1 ton of refrigeration = 12000 BTU/h = 3516.8528420667 W
1855
+ ton_of_refrigeration: {
1856
+ factor: 3516.8528420667,
1857
+ symbol: "TR",
1858
+ singular: "ton of refrigeration",
1859
+ plural: "tons of refrigeration"
1860
+ },
1861
+ // 1 ft⋅lbf/s = 1.3558179483314004 W
1862
+ foot_pound_per_second: {
1863
+ factor: 1.3558179483314003,
1864
+ symbol: "ft\u22C5lbf/s",
1865
+ singular: "foot-pound per second",
1866
+ plural: "foot-pounds per second"
1867
+ },
1868
+ // 1 cal/s = 4.1868 W
1869
+ calorie_per_second: {
1870
+ factor: 4.1868,
1871
+ symbol: "cal/s",
1872
+ singular: "calorie per second",
1873
+ plural: "calories per second"
1874
+ },
1875
+ // 1 kcal/h = 4186.8 / 3600 = 1.163 W
1876
+ kilocalorie_per_hour: {
1877
+ factor: 1.163,
1878
+ symbol: "kcal/h",
1879
+ singular: "kilocalorie per hour",
1880
+ plural: "kilocalories per hour"
1881
+ }
1882
+ };
1883
+ var POWER_ALIASES = {
1884
+ // gigawatt
1885
+ gw: "gigawatt",
1886
+ // megawatt
1887
+ mw: "megawatt",
1888
+ // kilowatt
1889
+ kw: "kilowatt",
1890
+ // watt
1891
+ w: "watt",
1892
+ // microwatt
1893
+ "\u03BCw": "microwatt",
1894
+ uw: "microwatt",
1895
+ // horsepower_mechanical
1896
+ hp: "horsepower_mechanical",
1897
+ // horsepower_metric
1898
+ ps: "horsepower_metric",
1899
+ cv: "horsepower_metric",
1900
+ // horsepower_electric
1901
+ "hp(e)": "horsepower_electric",
1902
+ // horsepower_boiler
1903
+ "hp(s)": "horsepower_boiler",
1904
+ bhp: "horsepower_boiler",
1905
+ // btu_per_hour
1906
+ "btu/h": "btu_per_hour",
1907
+ "btu/hr": "btu_per_hour",
1908
+ // btu_per_second
1909
+ "btu/s": "btu_per_second",
1910
+ // ton_of_refrigeration
1911
+ tr: "ton_of_refrigeration",
1912
+ rt: "ton_of_refrigeration",
1913
+ // foot_pound_per_second
1914
+ "ft-lb/s": "foot_pound_per_second",
1915
+ "ft-lbf/s": "foot_pound_per_second",
1916
+ // calorie_per_second
1917
+ "cal/s": "calorie_per_second",
1918
+ // kilocalorie_per_hour
1919
+ "kcal/h": "kilocalorie_per_hour",
1920
+ "kcal/hr": "kilocalorie_per_hour"
1921
+ };
1922
+ Object.fromEntries(
1923
+ Object.entries(POWER_UNITS).map(([unit, config]) => [
1924
+ unit,
1925
+ config.factor ?? 1
1926
+ ])
1927
+ );
1928
+
1929
+ // src/roles/power/convert.ts
1930
+ var POWER_BASE = "watt";
1931
+ function toBasePower(variant, value) {
1932
+ const config = POWER_UNITS[variant];
1933
+ if (!config) {
1934
+ throw new Error(`Unknown power unit: ${variant}`);
1935
+ }
1936
+ return value * (config.factor ?? 1);
1937
+ }
1938
+ function fromBasePower(variant, baseValue) {
1939
+ const config = POWER_UNITS[variant];
1940
+ if (!config) {
1941
+ throw new Error(`Unknown power unit: ${variant}`);
1942
+ }
1943
+ return baseValue / (config.factor ?? 1);
1944
+ }
1945
+ function convertPower(from, to, value) {
1946
+ if (from === to) {
1947
+ return value;
1948
+ }
1949
+ const baseValue = toBasePower(from, value);
1950
+ return fromBasePower(to, baseValue);
1951
+ }
1952
+
1953
+ // src/roles/power/cast.ts
1954
+ function castPower(input, targetVariant = POWER_BASE) {
1955
+ if (typeof input === "number") {
1956
+ return Number.isFinite(input) ? input : null;
1957
+ }
1958
+ if (typeof input === "string") {
1959
+ const parsed = parsePowerString(input);
1960
+ if (parsed === null) {
1961
+ return null;
1962
+ }
1963
+ if (parsed.unit && parsed.unit !== targetVariant) {
1964
+ return convertPower(parsed.unit, targetVariant, parsed.value);
1965
+ }
1966
+ return parsed.value;
1967
+ }
1968
+ return null;
1969
+ }
1970
+ function tryCastPower(input, targetVariant = POWER_BASE) {
1971
+ const result = castPower(input, targetVariant);
1972
+ if (result === null) {
1973
+ return { ok: false, error: `Cannot cast "${String(input)}" to power` };
1974
+ }
1975
+ return { ok: true, value: result };
1976
+ }
1977
+ function parsePowerString(input) {
1978
+ const trimmed = input.trim();
1979
+ if (!trimmed) {
1980
+ return null;
1981
+ }
1982
+ const match = trimmed.match(/^([+-]?[\d.,\s]+)\s*(.*)$/);
1983
+ if (!match) {
1984
+ return null;
1985
+ }
1986
+ const numStr = match[1].replace(/\s/g, "").replace(",", ".");
1987
+ const value = parseFloat(numStr);
1988
+ if (isNaN(value) || !Number.isFinite(value)) {
1989
+ return null;
1990
+ }
1991
+ const unitStr = match[2]?.trim().toLowerCase() || "";
1992
+ let unit = null;
1993
+ if (unitStr) {
1994
+ if (unitStr in POWER_ALIASES) {
1995
+ unit = POWER_ALIASES[unitStr];
1996
+ } else if (unitStr in POWER_UNITS) {
1997
+ unit = unitStr;
1998
+ } else {
1999
+ for (const [u, config] of Object.entries(POWER_UNITS)) {
2000
+ if (config.symbol.toLowerCase() === unitStr) {
2001
+ unit = u;
2002
+ break;
2003
+ }
2004
+ }
2005
+ }
2006
+ }
2007
+ return { value, unit };
2008
+ }
2009
+
2010
+ // src/roles/pressure/constants.ts
2011
+ var PRESSURE_UNITS = {
2012
+ // SI Units (todos exatos)
2013
+ megapascal: {
2014
+ factor: 1e6,
2015
+ symbol: "MPa",
2016
+ singular: "megapascal",
2017
+ plural: "megapascals"
2018
+ },
2019
+ kilopascal: {
2020
+ factor: 1e3,
2021
+ symbol: "kPa",
2022
+ singular: "kilopascal",
2023
+ plural: "kilopascals"
2024
+ },
2025
+ hectopascal: {
2026
+ factor: 100,
2027
+ symbol: "hPa",
2028
+ singular: "hectopascal",
2029
+ plural: "hectopascals"
2030
+ },
2031
+ pascal: {
2032
+ factor: 1,
2033
+ symbol: "Pa",
2034
+ singular: "pascal",
2035
+ plural: "pascals"
2036
+ },
2037
+ // Bar (exatos por definição)
2038
+ bar: {
2039
+ factor: 1e5,
2040
+ symbol: "bar",
2041
+ singular: "bar",
2042
+ plural: "bar"
2043
+ },
2044
+ millibar: {
2045
+ factor: 100,
2046
+ // = 1 hPa
2047
+ symbol: "mbar",
2048
+ singular: "millibar",
2049
+ plural: "millibar"
2050
+ },
2051
+ // Atmosphere (exato, CGPM 1954)
2052
+ atmosphere: {
2053
+ factor: 101325,
2054
+ symbol: "atm",
2055
+ singular: "atmosphere",
2056
+ plural: "atmospheres"
2057
+ },
2058
+ // Mercury column
2059
+ torr: {
2060
+ factor: 101325 / 760,
2061
+ // ≈ 133.322368421
2062
+ symbol: "Torr",
2063
+ singular: "torr",
2064
+ plural: "torr"
2065
+ },
2066
+ mmhg: {
2067
+ factor: 133.322387415,
2068
+ // Convenção NIST
2069
+ symbol: "mmHg",
2070
+ singular: "millimeter of mercury",
2071
+ plural: "millimeters of mercury"
2072
+ },
2073
+ inhg: {
2074
+ factor: 3386.389,
2075
+ // Polegadas de mercúrio
2076
+ symbol: "inHg",
2077
+ singular: "inch of mercury",
2078
+ plural: "inches of mercury"
2079
+ },
2080
+ // Imperial (derivados de lb/in²)
2081
+ psi: {
2082
+ factor: 6894.757293168,
2083
+ // 1 lbf/in²
2084
+ symbol: "psi",
2085
+ singular: "pound per square inch",
2086
+ plural: "pounds per square inch"
2087
+ },
2088
+ ksi: {
2089
+ factor: 6894757293168e-6,
2090
+ // 1000 psi
2091
+ symbol: "ksi",
2092
+ singular: "kilopound per square inch",
2093
+ plural: "kilopounds per square inch"
2094
+ },
2095
+ // Water column
2096
+ cmh2o: {
2097
+ factor: 98.0665,
2098
+ // cm de água a 4°C
2099
+ symbol: "cmH\u2082O",
2100
+ singular: "centimeter of water",
2101
+ plural: "centimeters of water"
2102
+ },
2103
+ inh2o: {
2104
+ factor: 249.08891,
2105
+ // polegadas de água
2106
+ symbol: "inH\u2082O",
2107
+ singular: "inch of water",
2108
+ plural: "inches of water"
2109
+ }
2110
+ };
2111
+ var PRESSURE_ALIASES = {
2112
+ // megapascal
2113
+ mpa: "megapascal",
2114
+ // kilopascal
2115
+ kpa: "kilopascal",
2116
+ // hectopascal
2117
+ hpa: "hectopascal",
2118
+ // pascal
2119
+ pa: "pascal",
2120
+ // bar
2121
+ bar: "bar",
2122
+ // millibar
2123
+ mbar: "millibar",
2124
+ mb: "millibar",
2125
+ // atmosphere
2126
+ atm: "atmosphere",
2127
+ // torr
2128
+ torr: "torr",
2129
+ // mmhg
2130
+ mmhg: "mmhg",
2131
+ // inhg
2132
+ inhg: "inhg",
2133
+ // psi
2134
+ psi: "psi",
2135
+ // ksi
2136
+ ksi: "ksi",
2137
+ // cmh2o
2138
+ cmh2o: "cmh2o",
2139
+ // inh2o
2140
+ inh2o: "inh2o"
2141
+ };
2142
+ Object.fromEntries(
2143
+ Object.entries(PRESSURE_UNITS).map(([unit, config]) => [
2144
+ unit,
2145
+ config.factor ?? 1
2146
+ ])
2147
+ );
2148
+
2149
+ // src/roles/pressure/convert.ts
2150
+ var PRESSURE_BASE = "pascal";
2151
+ function toBasePressure(variant, value) {
2152
+ const config = PRESSURE_UNITS[variant];
2153
+ if (!config) {
2154
+ throw new Error(`Unknown pressure unit: ${variant}`);
2155
+ }
2156
+ return value * (config.factor ?? 1);
2157
+ }
2158
+ function fromBasePressure(variant, baseValue) {
2159
+ const config = PRESSURE_UNITS[variant];
2160
+ if (!config) {
2161
+ throw new Error(`Unknown pressure unit: ${variant}`);
2162
+ }
2163
+ return baseValue / (config.factor ?? 1);
2164
+ }
2165
+ function convertPressure(from, to, value) {
2166
+ if (from === to) {
2167
+ return value;
2168
+ }
2169
+ const baseValue = toBasePressure(from, value);
2170
+ return fromBasePressure(to, baseValue);
2171
+ }
2172
+
2173
+ // src/roles/pressure/cast.ts
2174
+ function castPressure(input, targetVariant = PRESSURE_BASE) {
2175
+ if (typeof input === "number") {
2176
+ return Number.isFinite(input) ? input : null;
2177
+ }
2178
+ if (typeof input === "string") {
2179
+ const parsed = parsePressureString(input);
2180
+ if (parsed === null) {
2181
+ return null;
2182
+ }
2183
+ if (parsed.unit && parsed.unit !== targetVariant) {
2184
+ return convertPressure(parsed.unit, targetVariant, parsed.value);
2185
+ }
2186
+ return parsed.value;
2187
+ }
2188
+ return null;
2189
+ }
2190
+ function tryCastPressure(input, targetVariant = PRESSURE_BASE) {
2191
+ const result = castPressure(input, targetVariant);
2192
+ if (result === null) {
2193
+ return { ok: false, error: `Cannot cast "${String(input)}" to pressure` };
2194
+ }
2195
+ return { ok: true, value: result };
2196
+ }
2197
+ function parsePressureString(input) {
2198
+ const trimmed = input.trim();
2199
+ if (!trimmed) {
2200
+ return null;
2201
+ }
2202
+ const match = trimmed.match(/^([+-]?[\d.,\s]+)\s*(.*)$/);
2203
+ if (!match) {
2204
+ return null;
2205
+ }
2206
+ const numStr = match[1].replace(/\s/g, "").replace(",", ".");
2207
+ const value = parseFloat(numStr);
2208
+ if (isNaN(value) || !Number.isFinite(value)) {
2209
+ return null;
2210
+ }
2211
+ const unitStr = match[2]?.trim().toLowerCase() || "";
2212
+ let unit = null;
2213
+ if (unitStr) {
2214
+ if (unitStr in PRESSURE_ALIASES) {
2215
+ unit = PRESSURE_ALIASES[unitStr];
2216
+ } else if (unitStr in PRESSURE_UNITS) {
2217
+ unit = unitStr;
2218
+ } else {
2219
+ for (const [u, config] of Object.entries(PRESSURE_UNITS)) {
2220
+ if (config.symbol.toLowerCase() === unitStr) {
2221
+ unit = u;
2222
+ break;
2223
+ }
2224
+ }
2225
+ }
2226
+ }
2227
+ return { value, unit };
2228
+ }
2229
+
2230
+ // src/roles/frequency/constants.ts
2231
+ var FREQUENCY_UNITS = {
2232
+ // SI Units (todos exatos)
2233
+ terahertz: {
2234
+ factor: 1e12,
2235
+ symbol: "THz",
2236
+ singular: "terahertz",
2237
+ plural: "terahertz"
2238
+ },
2239
+ gigahertz: {
2240
+ factor: 1e9,
2241
+ symbol: "GHz",
2242
+ singular: "gigahertz",
2243
+ plural: "gigahertz"
2244
+ },
2245
+ megahertz: {
2246
+ factor: 1e6,
2247
+ symbol: "MHz",
2248
+ singular: "megahertz",
2249
+ plural: "megahertz"
2250
+ },
2251
+ kilohertz: {
2252
+ factor: 1e3,
2253
+ symbol: "kHz",
2254
+ singular: "kilohertz",
2255
+ plural: "kilohertz"
2256
+ },
2257
+ hertz: {
2258
+ factor: 1,
2259
+ symbol: "Hz",
2260
+ singular: "hertz",
2261
+ plural: "hertz"
2262
+ },
2263
+ millihertz: {
2264
+ factor: 1e-3,
2265
+ symbol: "mHz",
2266
+ singular: "millihertz",
2267
+ plural: "millihertz"
2268
+ },
2269
+ microhertz: {
2270
+ factor: 1e-6,
2271
+ symbol: "\u03BCHz",
2272
+ singular: "microhertz",
2273
+ plural: "microhertz"
2274
+ },
2275
+ // Other units
2276
+ rpm: {
2277
+ factor: 1 / 60,
2278
+ // 1 RPM = 1/60 Hz
2279
+ symbol: "rpm",
2280
+ singular: "revolution per minute",
2281
+ plural: "revolutions per minute"
2282
+ },
2283
+ bpm: {
2284
+ factor: 1 / 60,
2285
+ // 1 BPM = 1/60 Hz
2286
+ symbol: "bpm",
2287
+ singular: "beat per minute",
2288
+ plural: "beats per minute"
2289
+ },
2290
+ radians_per_second: {
2291
+ factor: 1 / (2 * Math.PI),
2292
+ // 1 rad/s = 1/(2π) Hz ≈ 0.159154943
2293
+ symbol: "rad/s",
2294
+ singular: "radian per second",
2295
+ plural: "radians per second"
2296
+ },
2297
+ cycles_per_minute: {
2298
+ factor: 1 / 60,
2299
+ // same as RPM
2300
+ symbol: "cpm",
2301
+ singular: "cycle per minute",
2302
+ plural: "cycles per minute"
2303
+ }
2304
+ };
2305
+ var FREQUENCY_ALIASES = {
2306
+ // terahertz
2307
+ thz: "terahertz",
2308
+ // gigahertz
2309
+ ghz: "gigahertz",
2310
+ // megahertz
2311
+ mhz: "megahertz",
2312
+ // kilohertz
2313
+ khz: "kilohertz",
2314
+ // hertz
2315
+ hz: "hertz",
2316
+ cps: "hertz",
2317
+ // microhertz
2318
+ "\u03BChz": "microhertz",
2319
+ uhz: "microhertz",
2320
+ // rpm
2321
+ rpm: "rpm",
2322
+ // bpm
2323
+ bpm: "bpm",
2324
+ // radians_per_second
2325
+ "rad/s": "radians_per_second",
2326
+ // cycles_per_minute
2327
+ cpm: "cycles_per_minute"
2328
+ };
2329
+ Object.fromEntries(
2330
+ Object.entries(FREQUENCY_UNITS).map(([unit, config]) => [
2331
+ unit,
2332
+ config.factor ?? 1
2333
+ ])
2334
+ );
2335
+
2336
+ // src/roles/frequency/convert.ts
2337
+ var FREQUENCY_BASE = "hertz";
2338
+ function toBaseFrequency(variant, value) {
2339
+ const config = FREQUENCY_UNITS[variant];
2340
+ if (!config) {
2341
+ throw new Error(`Unknown frequency unit: ${variant}`);
2342
+ }
2343
+ return value * (config.factor ?? 1);
2344
+ }
2345
+ function fromBaseFrequency(variant, baseValue) {
2346
+ const config = FREQUENCY_UNITS[variant];
2347
+ if (!config) {
2348
+ throw new Error(`Unknown frequency unit: ${variant}`);
2349
+ }
2350
+ return baseValue / (config.factor ?? 1);
2351
+ }
2352
+ function convertFrequency(from, to, value) {
2353
+ if (from === to) {
2354
+ return value;
2355
+ }
2356
+ const baseValue = toBaseFrequency(from, value);
2357
+ return fromBaseFrequency(to, baseValue);
2358
+ }
2359
+
2360
+ // src/roles/frequency/cast.ts
2361
+ function castFrequency(input, targetVariant = FREQUENCY_BASE) {
2362
+ if (typeof input === "number") {
2363
+ return Number.isFinite(input) ? input : null;
2364
+ }
2365
+ if (typeof input === "string") {
2366
+ const parsed = parseFrequencyString(input);
2367
+ if (parsed === null) {
2368
+ return null;
2369
+ }
2370
+ if (parsed.unit && parsed.unit !== targetVariant) {
2371
+ return convertFrequency(parsed.unit, targetVariant, parsed.value);
2372
+ }
2373
+ return parsed.value;
2374
+ }
2375
+ return null;
2376
+ }
2377
+ function tryCastFrequency(input, targetVariant = FREQUENCY_BASE) {
2378
+ const result = castFrequency(input, targetVariant);
2379
+ if (result === null) {
2380
+ return { ok: false, error: `Cannot cast "${String(input)}" to frequency` };
2381
+ }
2382
+ return { ok: true, value: result };
2383
+ }
2384
+ function parseFrequencyString(input) {
2385
+ const trimmed = input.trim();
2386
+ if (!trimmed) {
2387
+ return null;
2388
+ }
2389
+ const match = trimmed.match(/^([+-]?[\d.,\s]+)\s*(.*)$/);
2390
+ if (!match) {
2391
+ return null;
2392
+ }
2393
+ const numStr = match[1].replace(/\s/g, "").replace(",", ".");
2394
+ const value = parseFloat(numStr);
2395
+ if (isNaN(value) || !Number.isFinite(value)) {
2396
+ return null;
2397
+ }
2398
+ const unitStr = match[2]?.trim().toLowerCase() || "";
2399
+ let unit = null;
2400
+ if (unitStr) {
2401
+ if (unitStr in FREQUENCY_ALIASES) {
2402
+ unit = FREQUENCY_ALIASES[unitStr];
2403
+ } else if (unitStr in FREQUENCY_UNITS) {
2404
+ unit = unitStr;
2405
+ } else {
2406
+ for (const [u, config] of Object.entries(FREQUENCY_UNITS)) {
2407
+ if (config.symbol.toLowerCase() === unitStr) {
2408
+ unit = u;
2409
+ break;
2410
+ }
2411
+ }
2412
+ }
2413
+ }
2414
+ return { value, unit };
2415
+ }
2416
+
2417
+ // src/roles/angle/constants.ts
2418
+ var DEGREES_PER_RADIAN = 180 / Math.PI;
2419
+ var DEGREES_PER_MILLIRADIAN = 180 / (1e3 * Math.PI);
2420
+ var ANGLE_UNITS = {
2421
+ // Volta completa
2422
+ turn: {
2423
+ factor: 360,
2424
+ symbol: "tr",
2425
+ singular: "turn",
2426
+ plural: "turns"
2427
+ },
2428
+ // Base
2429
+ degree: {
2430
+ factor: 1,
2431
+ symbol: "\xB0",
2432
+ singular: "degree",
2433
+ plural: "degrees",
2434
+ noSpace: true
2435
+ // 45° não 45 °
2436
+ },
2437
+ // Subdivisões do grau
2438
+ arcminute: {
2439
+ factor: 1 / 60,
2440
+ // 0.016666...
2441
+ symbol: "\u2032",
2442
+ singular: "arcminute",
2443
+ plural: "arcminutes",
2444
+ noSpace: true
2445
+ // 30′ não 30 ′
2446
+ },
2447
+ arcsecond: {
2448
+ factor: 1 / 3600,
2449
+ // 0.000277...
2450
+ symbol: "\u2033",
2451
+ singular: "arcsecond",
2452
+ plural: "arcseconds",
2453
+ noSpace: true
2454
+ // 45″ não 45 ″
2455
+ },
2456
+ milliarcsecond: {
2457
+ factor: 1 / 36e5,
2458
+ // 2.777...e-7
2459
+ symbol: "mas",
2460
+ singular: "milliarcsecond",
2461
+ plural: "milliarcseconds"
2462
+ },
2463
+ // Radianos (unidade SI)
2464
+ radian: {
2465
+ factor: DEGREES_PER_RADIAN,
2466
+ // 180/π ≈ 57.2958
2467
+ symbol: "rad",
2468
+ singular: "radian",
2469
+ plural: "radians"
2470
+ },
2471
+ milliradian: {
2472
+ factor: DEGREES_PER_MILLIRADIAN,
2473
+ // 180/(1000π) ≈ 0.0573
2474
+ symbol: "mrad",
2475
+ singular: "milliradian",
2476
+ plural: "milliradians"
2477
+ },
2478
+ // Gradiano (gon)
2479
+ gradian: {
2480
+ factor: 0.9,
2481
+ // 360/400
2482
+ symbol: "gon",
2483
+ singular: "gradian",
2484
+ plural: "gradians"
2485
+ }
2486
+ };
2487
+ var ANGLE_ALIASES = {
2488
+ // turn
2489
+ tr: "turn",
2490
+ rev: "turn",
2491
+ // degree
2492
+ "\xB0": "degree",
2493
+ deg: "degree",
2494
+ // arcminute
2495
+ "\u2032": "arcminute",
2496
+ "'": "arcminute",
2497
+ arcmin: "arcminute",
2498
+ // arcsecond
2499
+ "\u2033": "arcsecond",
2500
+ '"': "arcsecond",
2501
+ arcsec: "arcsecond",
2502
+ // milliarcsecond
2503
+ mas: "milliarcsecond",
2504
+ // radian
2505
+ rad: "radian",
2506
+ // milliradian
2507
+ mrad: "milliradian",
2508
+ // gradian
2509
+ gon: "gradian",
2510
+ grad: "gradian"
2511
+ };
2512
+ Object.fromEntries(
2513
+ Object.entries(ANGLE_UNITS).map(([unit, config]) => [
2514
+ unit,
2515
+ config.factor ?? 1
2516
+ ])
2517
+ );
2518
+
2519
+ // src/roles/angle/convert.ts
2520
+ var ANGLE_BASE = "degree";
2521
+ function toBaseAngle(variant, value) {
2522
+ const config = ANGLE_UNITS[variant];
2523
+ if (!config) {
2524
+ throw new Error(`Unknown angle unit: ${variant}`);
2525
+ }
2526
+ return value * (config.factor ?? 1);
2527
+ }
2528
+ function fromBaseAngle(variant, baseValue) {
2529
+ const config = ANGLE_UNITS[variant];
2530
+ if (!config) {
2531
+ throw new Error(`Unknown angle unit: ${variant}`);
2532
+ }
2533
+ return baseValue / (config.factor ?? 1);
2534
+ }
2535
+ function convertAngle(from, to, value) {
2536
+ if (from === to) {
2537
+ return value;
2538
+ }
2539
+ const baseValue = toBaseAngle(from, value);
2540
+ return fromBaseAngle(to, baseValue);
2541
+ }
2542
+
2543
+ // src/roles/angle/cast.ts
2544
+ function castAngle(input, targetVariant = ANGLE_BASE) {
2545
+ if (typeof input === "number") {
2546
+ return Number.isFinite(input) ? input : null;
2547
+ }
2548
+ if (typeof input === "string") {
2549
+ const parsed = parseAngleString(input);
2550
+ if (parsed === null) {
2551
+ return null;
2552
+ }
2553
+ if (parsed.unit && parsed.unit !== targetVariant) {
2554
+ return convertAngle(parsed.unit, targetVariant, parsed.value);
2555
+ }
2556
+ return parsed.value;
2557
+ }
2558
+ return null;
2559
+ }
2560
+ function tryCastAngle(input, targetVariant = ANGLE_BASE) {
2561
+ const result = castAngle(input, targetVariant);
2562
+ if (result === null) {
2563
+ return { ok: false, error: `Cannot cast "${String(input)}" to angle` };
2564
+ }
2565
+ return { ok: true, value: result };
2566
+ }
2567
+ function parseAngleString(input) {
2568
+ const trimmed = input.trim();
2569
+ if (!trimmed) {
2570
+ return null;
2571
+ }
2572
+ const degreeMatch = trimmed.match(/^([+-]?[\d.,]+)°$/);
2573
+ if (degreeMatch) {
2574
+ const numStr2 = degreeMatch[1].replace(",", ".");
2575
+ const value2 = parseFloat(numStr2);
2576
+ if (isNaN(value2) || !Number.isFinite(value2)) {
2577
+ return null;
2578
+ }
2579
+ return { value: value2, unit: "degree" };
2580
+ }
2581
+ const match = trimmed.match(/^([+-]?[\d.,\s]+)\s*(.*)$/);
2582
+ if (!match) {
2583
+ return null;
2584
+ }
2585
+ const numStr = match[1].replace(/\s/g, "").replace(",", ".");
2586
+ const value = parseFloat(numStr);
2587
+ if (isNaN(value) || !Number.isFinite(value)) {
2588
+ return null;
2589
+ }
2590
+ const unitStr = match[2]?.trim().toLowerCase() || "";
2591
+ let unit = null;
2592
+ if (unitStr) {
2593
+ if (unitStr in ANGLE_ALIASES) {
2594
+ unit = ANGLE_ALIASES[unitStr];
2595
+ } else if (unitStr in ANGLE_UNITS) {
2596
+ unit = unitStr;
2597
+ } else {
2598
+ for (const [u, config] of Object.entries(ANGLE_UNITS)) {
2599
+ if (config.symbol.toLowerCase() === unitStr || config.symbol === unitStr) {
2600
+ unit = u;
2601
+ break;
2602
+ }
2603
+ }
2604
+ }
2605
+ }
2606
+ return { value, unit };
2607
+ }
2608
+
2609
+ // src/roles/time/constants.ts
2610
+ var TIME_UNITS = {
2611
+ // SI prefixes (exatos)
2612
+ nanosecond: {
2613
+ factor: 1e-9,
2614
+ symbol: "ns",
2615
+ singular: "nanosecond",
2616
+ plural: "nanoseconds"
2617
+ },
2618
+ microsecond: {
2619
+ factor: 1e-6,
2620
+ symbol: "\u03BCs",
2621
+ singular: "microsecond",
2622
+ plural: "microseconds"
2623
+ },
2624
+ millisecond: {
2625
+ factor: 1e-3,
2626
+ symbol: "ms",
2627
+ singular: "millisecond",
2628
+ plural: "milliseconds"
2629
+ },
2630
+ second: {
2631
+ factor: 1,
2632
+ symbol: "s",
2633
+ singular: "second",
2634
+ plural: "seconds"
2635
+ },
2636
+ // Common (exatos)
2637
+ minute: {
2638
+ factor: 60,
2639
+ symbol: "min",
2640
+ singular: "minute",
2641
+ plural: "minutes"
2642
+ },
2643
+ hour: {
2644
+ factor: 3600,
2645
+ symbol: "h",
2646
+ singular: "hour",
2647
+ plural: "hours"
2648
+ },
2649
+ day: {
2650
+ factor: 86400,
2651
+ symbol: "d",
2652
+ singular: "day",
2653
+ plural: "days"
2654
+ },
2655
+ week: {
2656
+ factor: 604800,
2657
+ symbol: "wk",
2658
+ singular: "week",
2659
+ plural: "weeks"
2660
+ },
2661
+ // Calendar (aproximados - baseados no ano Gregoriano)
2662
+ month: {
2663
+ factor: 2629746,
2664
+ // 31556952 / 12
2665
+ symbol: "mo",
2666
+ singular: "month",
2667
+ plural: "months"
2668
+ },
2669
+ year: {
2670
+ factor: 31556952,
2671
+ // 365.2425 * 86400
2672
+ symbol: "yr",
2673
+ singular: "year",
2674
+ plural: "years"
2675
+ },
2676
+ decade: {
2677
+ factor: 315569520,
2678
+ // 10 * year
2679
+ symbol: "dec",
2680
+ singular: "decade",
2681
+ plural: "decades"
2682
+ },
2683
+ century: {
2684
+ factor: 3155695200,
2685
+ // 100 * year
2686
+ symbol: "c",
2687
+ singular: "century",
2688
+ plural: "centuries"
2689
+ },
2690
+ millennium: {
2691
+ factor: 31556952e3,
2692
+ // 1000 * year
2693
+ symbol: "ky",
2694
+ singular: "millennium",
2695
+ plural: "millennia"
2696
+ }
2697
+ };
2698
+ var TIME_ALIASES = {
2699
+ // nanosecond
2700
+ ns: "nanosecond",
2701
+ // microsecond
2702
+ "\u03BCs": "microsecond",
2703
+ us: "microsecond",
2704
+ // millisecond
2705
+ ms: "millisecond",
2706
+ // second
2707
+ s: "second",
2708
+ sec: "second",
2709
+ // minute
2710
+ min: "minute",
2711
+ // hour
2712
+ h: "hour",
2713
+ hr: "hour",
2714
+ hrs: "hour",
2715
+ // day
2716
+ d: "day",
2717
+ // week
2718
+ wk: "week",
2719
+ // month
2720
+ mo: "month",
2721
+ // year
2722
+ yr: "year",
2723
+ y: "year",
2724
+ // decade
2725
+ dec: "decade",
2726
+ // century
2727
+ c: "century",
2728
+ // millennium
2729
+ ky: "millennium"
2730
+ };
2731
+ Object.fromEntries(
2732
+ Object.entries(TIME_UNITS).map(([unit, config]) => [
2733
+ unit,
2734
+ config.factor ?? 1
2735
+ ])
2736
+ );
2737
+
2738
+ // src/roles/time/convert.ts
2739
+ var TIME_BASE = "second";
2740
+ function toBaseTime(variant, value) {
2741
+ const config = TIME_UNITS[variant];
2742
+ if (!config) {
2743
+ throw new Error(`Unknown time unit: ${variant}`);
2744
+ }
2745
+ return value * (config.factor ?? 1);
2746
+ }
2747
+ function fromBaseTime(variant, baseValue) {
2748
+ const config = TIME_UNITS[variant];
2749
+ if (!config) {
2750
+ throw new Error(`Unknown time unit: ${variant}`);
2751
+ }
2752
+ return baseValue / (config.factor ?? 1);
2753
+ }
2754
+ function convertTime(from, to, value) {
2755
+ if (from === to) {
2756
+ return value;
2757
+ }
2758
+ const baseValue = toBaseTime(from, value);
2759
+ return fromBaseTime(to, baseValue);
2760
+ }
2761
+
2762
+ // src/roles/time/cast.ts
2763
+ function castTime(input, targetVariant = TIME_BASE) {
2764
+ if (typeof input === "number") {
2765
+ return Number.isFinite(input) ? input : null;
2766
+ }
2767
+ if (typeof input === "string") {
2768
+ const parsed = parseTimeString(input);
2769
+ if (parsed === null) {
2770
+ return null;
2771
+ }
2772
+ if (parsed.unit && parsed.unit !== targetVariant) {
2773
+ return convertTime(parsed.unit, targetVariant, parsed.value);
2774
+ }
2775
+ return parsed.value;
2776
+ }
2777
+ return null;
2778
+ }
2779
+ function tryCastTime(input, targetVariant = TIME_BASE) {
2780
+ const result = castTime(input, targetVariant);
2781
+ if (result === null) {
2782
+ return { ok: false, error: `Cannot cast "${String(input)}" to time` };
2783
+ }
2784
+ return { ok: true, value: result };
2785
+ }
2786
+ function parseTimeString(input) {
2787
+ const trimmed = input.trim();
2788
+ if (!trimmed) {
2789
+ return null;
2790
+ }
2791
+ const match = trimmed.match(/^([+-]?[\d.,\s]+)\s*(.*)$/);
2792
+ if (!match) {
2793
+ return null;
2794
+ }
2795
+ const numStr = match[1].replace(/\s/g, "").replace(",", ".");
2796
+ const value = parseFloat(numStr);
2797
+ if (isNaN(value) || !Number.isFinite(value)) {
2798
+ return null;
2799
+ }
2800
+ const unitStr = match[2]?.trim().toLowerCase() || "";
2801
+ let unit = null;
2802
+ if (unitStr) {
2803
+ if (unitStr in TIME_ALIASES) {
2804
+ unit = TIME_ALIASES[unitStr];
2805
+ } else if (unitStr in TIME_UNITS) {
2806
+ unit = unitStr;
2807
+ } else {
2808
+ for (const [u, config] of Object.entries(TIME_UNITS)) {
2809
+ if (config.symbol.toLowerCase() === unitStr) {
2810
+ unit = u;
2811
+ break;
2812
+ }
2813
+ }
2814
+ }
2815
+ }
2816
+ return { value, unit };
2817
+ }
2818
+
2819
+ // src/roles/digital/constants.ts
2820
+ var DIGITAL_UNITS = {
2821
+ // Fundamental
2822
+ bit: {
2823
+ factor: 0.125,
2824
+ // 1/8 byte
2825
+ symbol: "b",
2826
+ singular: "bit",
2827
+ plural: "bits"
2828
+ },
2829
+ byte: {
2830
+ factor: 1,
2831
+ symbol: "B",
2832
+ singular: "byte",
2833
+ plural: "bytes"
2834
+ },
2835
+ // IEC Binary (base 1024) - RAM, cache, file systems
2836
+ kibibyte: {
2837
+ factor: 1024,
2838
+ // 2^10
2839
+ symbol: "KiB",
2840
+ singular: "kibibyte",
2841
+ plural: "kibibytes"
2842
+ },
2843
+ mebibyte: {
2844
+ factor: 1048576,
2845
+ // 2^20
2846
+ symbol: "MiB",
2847
+ singular: "mebibyte",
2848
+ plural: "mebibytes"
2849
+ },
2850
+ gibibyte: {
2851
+ factor: 1073741824,
2852
+ // 2^30
2853
+ symbol: "GiB",
2854
+ singular: "gibibyte",
2855
+ plural: "gibibytes"
2856
+ },
2857
+ tebibyte: {
2858
+ factor: 1099511627776,
2859
+ // 2^40
2860
+ symbol: "TiB",
2861
+ singular: "tebibyte",
2862
+ plural: "tebibytes"
2863
+ },
2864
+ pebibyte: {
2865
+ factor: 1125899906842624,
2866
+ // 2^50
2867
+ symbol: "PiB",
2868
+ singular: "pebibyte",
2869
+ plural: "pebibytes"
2870
+ },
2871
+ exbibyte: {
2872
+ factor: 1152921504606847e3,
2873
+ // 2^60
2874
+ symbol: "EiB",
2875
+ singular: "exbibyte",
2876
+ plural: "exbibytes"
2877
+ },
2878
+ // SI Decimal (base 1000) - HD marketing, network speeds
2879
+ kilobyte: {
2880
+ factor: 1e3,
2881
+ // 10^3
2882
+ symbol: "kB",
2883
+ singular: "kilobyte",
2884
+ plural: "kilobytes"
2885
+ },
2886
+ megabyte: {
2887
+ factor: 1e6,
2888
+ // 10^6
2889
+ symbol: "MB",
2890
+ singular: "megabyte",
2891
+ plural: "megabytes"
2892
+ },
2893
+ gigabyte: {
2894
+ factor: 1e9,
2895
+ // 10^9
2896
+ symbol: "GB",
2897
+ singular: "gigabyte",
2898
+ plural: "gigabytes"
2899
+ },
2900
+ terabyte: {
2901
+ factor: 1e12,
2902
+ // 10^12
2903
+ symbol: "TB",
2904
+ singular: "terabyte",
2905
+ plural: "terabytes"
2906
+ },
2907
+ petabyte: {
2908
+ factor: 1e15,
2909
+ // 10^15
2910
+ symbol: "PB",
2911
+ singular: "petabyte",
2912
+ plural: "petabytes"
2913
+ },
2914
+ exabyte: {
2915
+ factor: 1e18,
2916
+ // 10^18
2917
+ symbol: "EB",
2918
+ singular: "exabyte",
2919
+ plural: "exabytes"
2920
+ }
2921
+ };
2922
+ var DIGITAL_ALIASES = {
2923
+ // bit
2924
+ b: "bit",
2925
+ // byte
2926
+ B: "byte",
2927
+ // kibibyte (IEC)
2928
+ KiB: "kibibyte",
2929
+ // mebibyte (IEC)
2930
+ MiB: "mebibyte",
2931
+ // gibibyte (IEC)
2932
+ GiB: "gibibyte",
2933
+ // tebibyte (IEC)
2934
+ TiB: "tebibyte",
2935
+ // pebibyte (IEC)
2936
+ PiB: "pebibyte",
2937
+ // exbibyte (IEC)
2938
+ EiB: "exbibyte",
2939
+ // kilobyte (SI)
2940
+ kB: "kilobyte",
2941
+ kb: "kilobyte",
2942
+ // megabyte (SI)
2943
+ MB: "megabyte",
2944
+ // gigabyte (SI)
2945
+ GB: "gigabyte",
2946
+ // terabyte (SI)
2947
+ TB: "terabyte",
2948
+ // petabyte (SI)
2949
+ PB: "petabyte",
2950
+ // exabyte (SI)
2951
+ EB: "exabyte"
2952
+ };
2953
+ Object.fromEntries(
2954
+ Object.entries(DIGITAL_UNITS).map(([unit, config]) => [
2955
+ unit,
2956
+ config.factor ?? 1
2957
+ ])
2958
+ );
2959
+
2960
+ // src/roles/digital/convert.ts
2961
+ var DIGITAL_BASE = "byte";
2962
+ function toBaseDigital(variant, value) {
2963
+ const config = DIGITAL_UNITS[variant];
2964
+ if (!config) {
2965
+ throw new Error(`Unknown digital unit: ${variant}`);
2966
+ }
2967
+ return value * (config.factor ?? 1);
2968
+ }
2969
+ function fromBaseDigital(variant, baseValue) {
2970
+ const config = DIGITAL_UNITS[variant];
2971
+ if (!config) {
2972
+ throw new Error(`Unknown digital unit: ${variant}`);
2973
+ }
2974
+ return baseValue / (config.factor ?? 1);
2975
+ }
2976
+ function convertDigital(from, to, value) {
2977
+ if (from === to) {
2978
+ return value;
2979
+ }
2980
+ const baseValue = toBaseDigital(from, value);
2981
+ return fromBaseDigital(to, baseValue);
2982
+ }
2983
+
2984
+ // src/roles/digital/cast.ts
2985
+ function castDigital(input, targetVariant = DIGITAL_BASE) {
2986
+ if (typeof input === "number") {
2987
+ return Number.isFinite(input) ? input : null;
2988
+ }
2989
+ if (typeof input === "string") {
2990
+ const parsed = parseDigitalString(input);
2991
+ if (parsed === null) {
2992
+ return null;
2993
+ }
2994
+ if (parsed.unit && parsed.unit !== targetVariant) {
2995
+ return convertDigital(parsed.unit, targetVariant, parsed.value);
2996
+ }
2997
+ return parsed.value;
2998
+ }
2999
+ return null;
3000
+ }
3001
+ function tryCastDigital(input, targetVariant = DIGITAL_BASE) {
3002
+ const result = castDigital(input, targetVariant);
3003
+ if (result === null) {
3004
+ return { ok: false, error: `Cannot cast "${String(input)}" to digital storage` };
3005
+ }
3006
+ return { ok: true, value: result };
3007
+ }
3008
+ function parseDigitalString(input) {
3009
+ const trimmed = input.trim();
3010
+ if (!trimmed) {
3011
+ return null;
3012
+ }
3013
+ const match = trimmed.match(/^([+-]?[\d.,\s]+)\s*(.*)$/);
3014
+ if (!match) {
3015
+ return null;
3016
+ }
3017
+ const numStr = match[1].replace(/\s/g, "").replace(",", ".");
3018
+ const value = parseFloat(numStr);
3019
+ if (isNaN(value) || !Number.isFinite(value)) {
3020
+ return null;
3021
+ }
3022
+ const unitStr = match[2]?.trim() || "";
3023
+ const lowerUnitStr = unitStr.toLowerCase();
3024
+ let unit = null;
3025
+ if (unitStr) {
3026
+ if (unitStr in DIGITAL_ALIASES) {
3027
+ unit = DIGITAL_ALIASES[unitStr];
3028
+ } else if (lowerUnitStr in DIGITAL_ALIASES) {
3029
+ unit = DIGITAL_ALIASES[lowerUnitStr];
3030
+ } else if (lowerUnitStr in DIGITAL_UNITS) {
3031
+ unit = lowerUnitStr;
3032
+ } else {
3033
+ for (const [u, config] of Object.entries(DIGITAL_UNITS)) {
3034
+ if (config.symbol === unitStr || config.symbol.toLowerCase() === lowerUnitStr) {
3035
+ unit = u;
3036
+ break;
3037
+ }
3038
+ }
3039
+ }
3040
+ }
3041
+ return { value, unit };
3042
+ }
3043
+
3044
+ // src/roles/color/types.ts
3045
+ var NAMED_COLORS = {
3046
+ // Basic colors
3047
+ black: { r: 0, g: 0, b: 0, a: 1 },
3048
+ white: { r: 255, g: 255, b: 255, a: 1 },
3049
+ red: { r: 255, g: 0, b: 0, a: 1 },
3050
+ green: { r: 0, g: 128, b: 0, a: 1 },
3051
+ blue: { r: 0, g: 0, b: 255, a: 1 },
3052
+ yellow: { r: 255, g: 255, b: 0, a: 1 },
3053
+ cyan: { r: 0, g: 255, b: 255, a: 1 },
3054
+ magenta: { r: 255, g: 0, b: 255, a: 1 },
3055
+ // Extended colors
3056
+ orange: { r: 255, g: 165, b: 0, a: 1 },
3057
+ purple: { r: 128, g: 0, b: 128, a: 1 },
3058
+ pink: { r: 255, g: 192, b: 203, a: 1 },
3059
+ brown: { r: 165, g: 42, b: 42, a: 1 },
3060
+ gray: { r: 128, g: 128, b: 128, a: 1 },
3061
+ grey: { r: 128, g: 128, b: 128, a: 1 },
3062
+ // Web colors
3063
+ lime: { r: 0, g: 255, b: 0, a: 1 },
3064
+ aqua: { r: 0, g: 255, b: 255, a: 1 },
3065
+ fuchsia: { r: 255, g: 0, b: 255, a: 1 },
3066
+ silver: { r: 192, g: 192, b: 192, a: 1 },
3067
+ maroon: { r: 128, g: 0, b: 0, a: 1 },
3068
+ olive: { r: 128, g: 128, b: 0, a: 1 },
3069
+ navy: { r: 0, g: 0, b: 128, a: 1 },
3070
+ teal: { r: 0, g: 128, b: 128, a: 1 },
3071
+ // Transparent
3072
+ transparent: { r: 0, g: 0, b: 0, a: 0 }
3073
+ };
3074
+ function clamp(value, min, max) {
3075
+ return Math.min(Math.max(value, min), max);
3076
+ }
3077
+ function normalizeRgbChannel(value) {
3078
+ return clamp(Math.round(value), 0, 255);
3079
+ }
3080
+ function normalizeAlpha(value) {
3081
+ if (value === void 0) return 1;
3082
+ return clamp(value, 0, 1);
3083
+ }
3084
+ function rgbToHsl(r, g, b) {
3085
+ r /= 255;
3086
+ g /= 255;
3087
+ b /= 255;
3088
+ const max = Math.max(r, g, b);
3089
+ const min = Math.min(r, g, b);
3090
+ const l = (max + min) / 2;
3091
+ if (max === min) {
3092
+ return { h: 0, s: 0, l: l * 100 };
3093
+ }
3094
+ const d = max - min;
3095
+ const s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
3096
+ let h;
3097
+ switch (max) {
3098
+ case r:
3099
+ h = ((g - b) / d + (g < b ? 6 : 0)) / 6;
3100
+ break;
3101
+ case g:
3102
+ h = ((b - r) / d + 2) / 6;
3103
+ break;
3104
+ default:
3105
+ h = ((r - g) / d + 4) / 6;
3106
+ break;
3107
+ }
3108
+ return {
3109
+ h: Math.round(h * 360),
3110
+ s: Math.round(s * 100),
3111
+ l: Math.round(l * 100)
3112
+ };
3113
+ }
3114
+ function hslToRgb(h, s, l) {
3115
+ h /= 360;
3116
+ s /= 100;
3117
+ l /= 100;
3118
+ if (s === 0) {
3119
+ const gray = Math.round(l * 255);
3120
+ return { r: gray, g: gray, b: gray };
3121
+ }
3122
+ const hue2rgb = (p2, q2, t) => {
3123
+ if (t < 0) t += 1;
3124
+ if (t > 1) t -= 1;
3125
+ if (t < 1 / 6) return p2 + (q2 - p2) * 6 * t;
3126
+ if (t < 1 / 2) return q2;
3127
+ if (t < 2 / 3) return p2 + (q2 - p2) * (2 / 3 - t) * 6;
3128
+ return p2;
3129
+ };
3130
+ const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
3131
+ const p = 2 * l - q;
3132
+ return {
3133
+ r: Math.round(hue2rgb(p, q, h + 1 / 3) * 255),
3134
+ g: Math.round(hue2rgb(p, q, h) * 255),
3135
+ b: Math.round(hue2rgb(p, q, h - 1 / 3) * 255)
3136
+ };
3137
+ }
3138
+
3139
+ // src/roles/color/convert.ts
3140
+ function hexToRgba(hex) {
3141
+ let h = hex.trim();
3142
+ if (h.startsWith("#")) h = h.slice(1);
3143
+ let r, g, b, a;
3144
+ if (h.length === 3) {
3145
+ r = parseInt(h[0] + h[0], 16);
3146
+ g = parseInt(h[1] + h[1], 16);
3147
+ b = parseInt(h[2] + h[2], 16);
3148
+ a = 1;
3149
+ } else if (h.length === 4) {
3150
+ r = parseInt(h[0] + h[0], 16);
3151
+ g = parseInt(h[1] + h[1], 16);
3152
+ b = parseInt(h[2] + h[2], 16);
3153
+ a = parseInt(h[3] + h[3], 16) / 255;
3154
+ } else if (h.length === 6) {
3155
+ r = parseInt(h.slice(0, 2), 16);
3156
+ g = parseInt(h.slice(2, 4), 16);
3157
+ b = parseInt(h.slice(4, 6), 16);
3158
+ a = 1;
3159
+ } else if (h.length === 8) {
3160
+ r = parseInt(h.slice(0, 2), 16);
3161
+ g = parseInt(h.slice(2, 4), 16);
3162
+ b = parseInt(h.slice(4, 6), 16);
3163
+ a = parseInt(h.slice(6, 8), 16) / 255;
3164
+ } else {
3165
+ throw new Error(`Invalid hex color: ${hex}`);
3166
+ }
3167
+ return { r, g, b, a };
3168
+ }
3169
+ function rgbObjectToRgba(rgb) {
3170
+ return {
3171
+ r: normalizeRgbChannel(rgb.r),
3172
+ g: normalizeRgbChannel(rgb.g),
3173
+ b: normalizeRgbChannel(rgb.b),
3174
+ a: normalizeAlpha(rgb.a)
3175
+ };
3176
+ }
3177
+ function rgbStringToRgba(rgb) {
3178
+ const match = rgb.match(/rgba?\s*\(\s*([\d.]+)\s*,\s*([\d.]+)\s*,\s*([\d.]+)\s*(?:,\s*([\d.]+)\s*)?\)/i);
3179
+ if (!match) {
3180
+ throw new Error(`Invalid RGB string: ${rgb}`);
3181
+ }
3182
+ return {
3183
+ r: normalizeRgbChannel(parseFloat(match[1])),
3184
+ g: normalizeRgbChannel(parseFloat(match[2])),
3185
+ b: normalizeRgbChannel(parseFloat(match[3])),
3186
+ a: match[4] !== void 0 ? normalizeAlpha(parseFloat(match[4])) : 1
3187
+ };
3188
+ }
3189
+ function hslObjectToRgba(hsl) {
3190
+ const { r, g, b } = hslToRgb(hsl.h, hsl.s, hsl.l);
3191
+ return {
3192
+ r,
3193
+ g,
3194
+ b,
3195
+ a: normalizeAlpha(hsl.a)
3196
+ };
3197
+ }
3198
+ function hslStringToRgba(hsl) {
3199
+ const match = hsl.match(/hsla?\s*\(\s*([\d.]+)\s*,\s*([\d.]+)%?\s*,\s*([\d.]+)%?\s*(?:,\s*([\d.]+)\s*)?\)/i);
3200
+ if (!match) {
3201
+ throw new Error(`Invalid HSL string: ${hsl}`);
3202
+ }
3203
+ const h = parseFloat(match[1]);
3204
+ const s = parseFloat(match[2]);
3205
+ const l = parseFloat(match[3]);
3206
+ const a = match[4] !== void 0 ? parseFloat(match[4]) : 1;
3207
+ const { r, g, b } = hslToRgb(h, s, l);
3208
+ return { r, g, b, a: normalizeAlpha(a) };
3209
+ }
3210
+ function rgbaToHex(rgba, includeAlpha = false) {
3211
+ const r = rgba.r.toString(16).padStart(2, "0");
3212
+ const g = rgba.g.toString(16).padStart(2, "0");
3213
+ const b = rgba.b.toString(16).padStart(2, "0");
3214
+ if (includeAlpha || rgba.a < 1) {
3215
+ const a = Math.round(rgba.a * 255).toString(16).padStart(2, "0");
3216
+ return `#${r}${g}${b}${a}`;
3217
+ }
3218
+ return `#${r}${g}${b}`;
3219
+ }
3220
+ function rgbaToRgbObject(rgba) {
3221
+ if (rgba.a < 1) {
3222
+ return { r: rgba.r, g: rgba.g, b: rgba.b, a: rgba.a };
3223
+ }
3224
+ return { r: rgba.r, g: rgba.g, b: rgba.b };
3225
+ }
3226
+ function rgbaToRgbString(rgba) {
3227
+ if (rgba.a < 1) {
3228
+ return `rgba(${rgba.r}, ${rgba.g}, ${rgba.b}, ${rgba.a})`;
3229
+ }
3230
+ return `rgb(${rgba.r}, ${rgba.g}, ${rgba.b})`;
3231
+ }
3232
+ function rgbaToHslObject(rgba) {
3233
+ const { h, s, l } = rgbToHsl(rgba.r, rgba.g, rgba.b);
3234
+ if (rgba.a < 1) {
3235
+ return { h, s, l, a: rgba.a };
3236
+ }
3237
+ return { h, s, l };
3238
+ }
3239
+ function rgbaToHslString(rgba) {
3240
+ const { h, s, l } = rgbToHsl(rgba.r, rgba.g, rgba.b);
3241
+ if (rgba.a < 1) {
3242
+ return `hsla(${h}, ${s}%, ${l}%, ${rgba.a})`;
3243
+ }
3244
+ return `hsl(${h}, ${s}%, ${l}%)`;
3245
+ }
3246
+ function fromBaseColor(variant, rgba) {
3247
+ switch (variant) {
3248
+ case "hex":
3249
+ return rgbaToHex(rgba);
3250
+ case "rgb_object":
3251
+ return rgbaToRgbObject(rgba);
3252
+ case "rgb_string":
3253
+ return rgbaToRgbString(rgba);
3254
+ case "hsl_object":
3255
+ return rgbaToHslObject(rgba);
3256
+ case "hsl_string":
3257
+ return rgbaToHslString(rgba);
3258
+ default:
3259
+ throw new Error(`Unknown color variant: ${variant}`);
3260
+ }
3261
+ }
3262
+
3263
+ // src/roles/color/cast.ts
3264
+ function detectObjectFormat(obj) {
3265
+ if (typeof obj.r === "number" && typeof obj.g === "number" && typeof obj.b === "number") {
3266
+ return "rgb_object";
3267
+ }
3268
+ if (typeof obj.h === "number" && typeof obj.s === "number" && typeof obj.l === "number") {
3269
+ return "hsl_object";
3270
+ }
3271
+ return null;
3272
+ }
3273
+ function parseToRgba(input) {
3274
+ if (typeof input === "object" && input !== null && "r" in input && "g" in input && "b" in input && "a" in input) {
3275
+ const rgba = input;
3276
+ if (typeof rgba.r === "number" && typeof rgba.g === "number" && typeof rgba.b === "number" && typeof rgba.a === "number") {
3277
+ return rgba;
3278
+ }
3279
+ }
3280
+ if (typeof input === "string") {
3281
+ const trimmed = input.trim();
3282
+ const namedColor = NAMED_COLORS[trimmed.toLowerCase()];
3283
+ if (namedColor) {
3284
+ return namedColor;
3285
+ }
3286
+ if (/^#?[0-9a-f]{3,8}$/i.test(trimmed)) {
3287
+ try {
3288
+ return hexToRgba(trimmed);
3289
+ } catch {
3290
+ return null;
3291
+ }
3292
+ }
3293
+ if (/^rgba?\s*\(/i.test(trimmed)) {
3294
+ try {
3295
+ return rgbStringToRgba(trimmed);
3296
+ } catch {
3297
+ return null;
3298
+ }
3299
+ }
3300
+ if (/^hsla?\s*\(/i.test(trimmed)) {
3301
+ try {
3302
+ return hslStringToRgba(trimmed);
3303
+ } catch {
3304
+ return null;
3305
+ }
3306
+ }
3307
+ return null;
3308
+ }
3309
+ if (typeof input === "object" && input !== null) {
3310
+ const format = detectObjectFormat(input);
3311
+ if (format === "rgb_object") {
3312
+ try {
3313
+ return rgbObjectToRgba(input);
3314
+ } catch {
3315
+ return null;
3316
+ }
3317
+ }
3318
+ if (format === "hsl_object") {
3319
+ try {
3320
+ return hslObjectToRgba(input);
3321
+ } catch {
3322
+ return null;
3323
+ }
3324
+ }
3325
+ }
3326
+ return null;
3327
+ }
3328
+ function castColor(variant, input) {
3329
+ const rgba = parseToRgba(input);
3330
+ if (!rgba) {
3331
+ return null;
3332
+ }
3333
+ try {
3334
+ return fromBaseColor(variant, rgba);
3335
+ } catch {
3336
+ return null;
3337
+ }
3338
+ }
3339
+ function tryCastColor(variant, input) {
3340
+ const result = castColor(variant, input);
3341
+ if (result === null) {
3342
+ return {
3343
+ ok: false,
3344
+ error: `Cannot cast "${String(input)}" to color variant "${variant}"`
3345
+ };
3346
+ }
3347
+ return { ok: true, value: result };
3348
+ }
3349
+
3350
+ // src/roles/date/cast.ts
3351
+ function castIso(input) {
3352
+ if (typeof input === "string") {
3353
+ const trimmed = input.trim();
3354
+ const date = new Date(trimmed);
3355
+ if (!isNaN(date.getTime())) {
3356
+ return date.toISOString();
3357
+ }
3358
+ return null;
3359
+ }
3360
+ if (typeof input === "number") {
3361
+ if (!Number.isFinite(input)) return null;
3362
+ const date = new Date(input);
3363
+ if (!isNaN(date.getTime())) {
3364
+ return date.toISOString();
3365
+ }
3366
+ return null;
3367
+ }
3368
+ if (input instanceof Date) {
3369
+ if (isNaN(input.getTime())) return null;
3370
+ return input.toISOString();
3371
+ }
3372
+ return null;
3373
+ }
3374
+ function castTimestamp(input) {
3375
+ if (typeof input === "number") {
3376
+ if (!Number.isFinite(input)) return null;
3377
+ return input;
3378
+ }
3379
+ if (typeof input === "string") {
3380
+ const trimmed = input.trim();
3381
+ if (/^-?\d+$/.test(trimmed)) {
3382
+ const num = parseInt(trimmed, 10);
3383
+ if (Number.isFinite(num)) return num;
3384
+ }
3385
+ const date = new Date(trimmed);
3386
+ if (!isNaN(date.getTime())) {
3387
+ return date.getTime();
3388
+ }
3389
+ return null;
3390
+ }
3391
+ if (input instanceof Date) {
3392
+ const ts = input.getTime();
3393
+ return isNaN(ts) ? null : ts;
3394
+ }
3395
+ return null;
3396
+ }
3397
+ function castEpoch(input) {
3398
+ if (typeof input === "number") {
3399
+ if (!Number.isFinite(input)) return null;
3400
+ if (Math.abs(input) > 1e11) {
3401
+ return Math.floor(input / 1e3);
3402
+ }
3403
+ return Math.floor(input);
3404
+ }
3405
+ if (typeof input === "string") {
3406
+ const trimmed = input.trim();
3407
+ if (/^-?\d+$/.test(trimmed)) {
3408
+ const num = parseInt(trimmed, 10);
3409
+ if (Number.isFinite(num)) {
3410
+ if (Math.abs(num) > 1e11) {
3411
+ return Math.floor(num / 1e3);
3412
+ }
3413
+ return num;
3414
+ }
3415
+ }
3416
+ const date = new Date(trimmed);
3417
+ if (!isNaN(date.getTime())) {
3418
+ return Math.floor(date.getTime() / 1e3);
3419
+ }
3420
+ return null;
3421
+ }
3422
+ if (input instanceof Date) {
3423
+ const ts = input.getTime();
3424
+ return isNaN(ts) ? null : Math.floor(ts / 1e3);
3425
+ }
3426
+ return null;
3427
+ }
3428
+ function castDate(variant, input) {
3429
+ switch (variant) {
3430
+ case "iso":
3431
+ return castIso(input);
3432
+ case "timestamp":
3433
+ return castTimestamp(input);
3434
+ case "epoch":
3435
+ return castEpoch(input);
3436
+ default:
3437
+ return null;
3438
+ }
3439
+ }
3440
+ function tryCastDate(variant, input) {
3441
+ const result = castDate(variant, input);
3442
+ if (result === null) {
3443
+ return {
3444
+ ok: false,
3445
+ error: `Cannot cast "${String(input)}" to date variant "${variant}"`
3446
+ };
3447
+ }
3448
+ return { ok: true, value: result };
3449
+ }
3450
+
3451
+ // src/roles/currency/cast.ts
3452
+ function castCurrency(input) {
3453
+ if (typeof input === "number") {
3454
+ return Number.isFinite(input) ? input : null;
3455
+ }
3456
+ if (typeof input === "string") {
3457
+ return parseCurrencyString(input);
3458
+ }
3459
+ return null;
3460
+ }
3461
+ function tryCastCurrency(input) {
3462
+ const result = castCurrency(input);
3463
+ if (result === null) {
3464
+ return {
3465
+ ok: false,
3466
+ error: `Cannot cast "${String(input)}" to currency value`
3467
+ };
3468
+ }
3469
+ return { ok: true, value: result };
3470
+ }
3471
+ var CURRENCY_SYMBOL_REGEX = /^[\s]*([A-Z]{1,3}\$?|[R$€£¥₹₩₽₺CHF]+)[\s]*/i;
3472
+ var CURRENCY_SYMBOL_SUFFIX_REGEX = /[\s]*([€₽])[\s]*$/;
3473
+ function parseCurrencyString(input) {
3474
+ let str = input.trim();
3475
+ if (!str) {
3476
+ return null;
3477
+ }
3478
+ let isNegative = false;
3479
+ if (str.startsWith("-")) {
3480
+ isNegative = true;
3481
+ str = str.slice(1).trim();
3482
+ } else if (str.startsWith("(") && str.endsWith(")")) {
3483
+ isNegative = true;
3484
+ str = str.slice(1, -1).trim();
3485
+ }
3486
+ str = str.replace(CURRENCY_SYMBOL_REGEX, "");
3487
+ str = str.replace(CURRENCY_SYMBOL_SUFFIX_REGEX, "");
3488
+ str = str.trim();
3489
+ if (!str) {
3490
+ return null;
3491
+ }
3492
+ const numValue = parseNumber8(str);
3493
+ if (numValue === null) {
3494
+ return null;
3495
+ }
3496
+ return isNegative ? -numValue : numValue;
3497
+ }
3498
+ function parseNumber8(numStr) {
3499
+ numStr = numStr.replace(/\s/g, "");
3500
+ if (!/^[+-]?[\d.,]+$/.test(numStr)) {
3501
+ return null;
3502
+ }
3503
+ const lastComma = numStr.lastIndexOf(",");
3504
+ const lastDot = numStr.lastIndexOf(".");
3505
+ const hasComma = lastComma !== -1;
3506
+ const hasDot = lastDot !== -1;
3507
+ if (hasComma && hasDot) {
3508
+ if (lastComma > lastDot) {
3509
+ numStr = numStr.replace(/\./g, "").replace(",", ".");
3510
+ } else {
3511
+ numStr = numStr.replace(/,/g, "");
3512
+ }
3513
+ } else if (hasComma && !hasDot) {
3514
+ const parts = numStr.split(",");
3515
+ const afterComma = parts.slice(1);
3516
+ if (afterComma.length > 1 || afterComma.length === 1 && afterComma[0].length === 3 && parts[0].length <= 3) {
3517
+ numStr = numStr.replace(/,/g, "");
3518
+ } else {
3519
+ numStr = numStr.replace(",", ".");
3520
+ }
3521
+ } else if (!hasComma && hasDot) {
3522
+ const parts = numStr.split(".");
3523
+ const afterDot = parts.slice(1);
3524
+ if (afterDot.length > 1) {
3525
+ numStr = numStr.replace(/\./g, "");
3526
+ } else if (afterDot.length === 1 && afterDot[0].length === 3) {
3527
+ numStr = numStr.replace(/\./g, "");
3528
+ }
3529
+ }
3530
+ const value = parseFloat(numStr);
3531
+ return isNaN(value) ? null : value;
3532
+ }
3533
+
3534
+ export { castAngle, castArea, castColor, castCurrency, castDate, castDigital, castEnergy, castFrequency, castLength, castMass, castPower, castPressure, castSpeed, castTemperature, castTime, castVolume, tryCastAngle, tryCastArea, tryCastColor, tryCastCurrency, tryCastDate, tryCastDigital, tryCastEnergy, tryCastFrequency, tryCastLength, tryCastMass, tryCastPower, tryCastPressure, tryCastSpeed, tryCastTemperature, tryCastTime, tryCastVolume };
3535
+ //# sourceMappingURL=cast.mjs.map
3536
+ //# sourceMappingURL=cast.mjs.map