-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-select.html
More file actions
2780 lines (2501 loc) · 111 KB
/
Copy pathsimple-select.html
File metadata and controls
2780 lines (2501 loc) · 111 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>simple-select — Power Automate Select Builder</title>
<style>
/* ── Design tokens ─────────────────────────────────────────────────────── */
:root {
--bg: #060606;
--bg-surface: #17181c;
--bg-elevated: #24272f;
--text-primary: #e8eaed;
--text-secondary: #b8bcc4;
--text-muted: #848894;
--ink-purple: #ac87eb;
--ink-blue: #4fa0ff;
--ink-pink: #d1617f;
--ink-green: #a6e3a1;
--ink-yellow: #f9e2af;
--ink-red: #f28b82;
--border-subtle: rgba(255, 255, 255, 0.10);
--border-muted: rgba(255, 255, 255, 0.20);
--border-strong: rgba(255, 255, 255, 0.30);
--gradient-cta: linear-gradient(93deg, #4fa0ff 0%, #ac87eb 50%, #d1617f 100%);
--code-bg: rgba(172, 135, 235, 0.10);
--font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
--font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
--radius-sm: 0.375rem;
--radius-md: 0.5rem;
--radius-lg: 1rem;
--radius-pill: 9999px;
--ease: cubic-bezier(0.4, 0, 0.2, 1);
--ease-out: cubic-bezier(0.2, 0.8, 0.2, 1);
--dur: 150ms;
--header-h: 52px;
}
/* ── Reset ─────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; border: 0; }
button { cursor: pointer; font: inherit; }
input, textarea, select { font: inherit; color: inherit; }
/* ── Base ──────────────────────────────────────────────────────────────── */
html, body { height: 100%; overflow: hidden; }
body {
font-family: var(--font-sans);
font-size: 0.9375rem;
line-height: 1.5;
color: var(--text-primary);
background-color: var(--bg);
/* Ambient glow — pure CSS, no external assets */
background-image:
radial-gradient(ellipse 90% 55% at 50% -5%,
rgba(79, 160, 255, 0.12) 0%,
rgba(172, 135, 235, 0.09) 40%,
transparent 70%);
background-attachment: fixed;
}
/* ── App header ────────────────────────────────────────────────────────── */
.app-header {
position: fixed;
inset: 0 0 auto 0;
height: var(--header-h);
z-index: 100;
display: flex;
align-items: center;
gap: 0.875rem;
padding: 0 1.5rem;
background: transparent;
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
border-bottom: 1px solid var(--border-subtle);
}
.app-header h1 {
font-family: var(--font-mono);
font-size: 1rem;
font-weight: 500;
color: var(--text-primary);
letter-spacing: -0.02em;
}
.app-header .sep {
width: 1px;
height: 1.1em;
background: var(--border-subtle);
}
.app-header .tagline {
font-family: var(--font-mono);
font-size: 0.75rem;
color: var(--text-muted);
}
/* ── Three-panel layout ────────────────────────────────────────────────── */
.panels {
position: fixed;
inset: var(--header-h) 0 0 0;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
overflow: hidden;
}
/* ── Panel shell ───────────────────────────────────────────────────────── */
.panel {
display: flex;
flex-direction: column;
height: 100%;
overflow: hidden;
border-right: 1px solid var(--border-subtle);
}
.panel:last-child { border-right: none; }
.panel-header {
flex-shrink: 0;
display: flex;
align-items: center;
gap: 0.625rem;
padding: 0 1.25rem;
height: 44px;
background: var(--bg-surface);
border-bottom: 1px solid var(--border-subtle);
font-family: var(--font-mono);
font-size: 0.8125rem;
font-weight: 500;
color: var(--text-secondary);
text-transform: uppercase;
letter-spacing: 0.09em;
}
.panel-header .title { flex: 1; }
/* Scrollable body */
.panel-body {
flex: 1;
overflow-y: auto;
padding: 1rem 1.25rem;
display: flex;
flex-direction: column;
gap: 0.75rem;
min-height: 0;
}
.panel-body::-webkit-scrollbar { width: 5px; }
.panel-body::-webkit-scrollbar-track { background: transparent; }
.panel-body::-webkit-scrollbar-thumb { background: var(--border-subtle); border-radius: 3px; }
/* Chips shelf (pinned below panel-header in the middle panel) */
.chips-shelf {
flex-shrink: 0;
padding: 0.625rem 1.25rem;
border-bottom: 1px solid var(--border-subtle);
background: rgba(172, 135, 235, 0.03);
}
.chips-shelf.hidden { display: none; }
/* Fixed footer */
.panel-footer {
flex-shrink: 0;
display: flex;
align-items: center;
gap: 0.5rem;
padding: 0.75rem 1.25rem;
background: var(--bg-surface);
border-top: 1px solid var(--border-subtle);
}
/* ── Badge ─────────────────────────────────────────────────────────────── */
.badge {
display: inline-flex;
align-items: center;
padding: 0.1rem 0.5rem;
border-radius: var(--radius-pill);
font-family: var(--font-mono);
font-size: 0.6875rem;
font-weight: 400;
line-height: 1.5;
}
.badge-ok {
background: rgba(166, 227, 161, 0.10);
color: var(--ink-green);
border: 1px solid rgba(166, 227, 161, 0.22);
}
.badge-err {
background: rgba(242, 139, 130, 0.10);
color: var(--ink-red);
border: 1px solid rgba(242, 139, 130, 0.22);
}
.badge-neutral {
background: rgba(255,255,255,0.05);
color: var(--text-secondary);
border: 1px solid var(--border-subtle);
}
.hidden { display: none !important; }
/* ── Buttons ───────────────────────────────────────────────────────────── */
.btn {
display: inline-flex;
align-items: center;
gap: 0.375rem;
padding: 0.4rem 0.875rem;
border-radius: var(--radius-pill);
font-size: 0.8125rem;
font-weight: 500;
line-height: 1.4;
transition: background var(--dur) var(--ease),
border-color var(--dur) var(--ease),
color var(--dur) var(--ease),
opacity var(--dur) var(--ease),
transform 100ms var(--ease-out);
outline: none;
user-select: none;
white-space: nowrap;
}
.btn-ghost {
background: transparent;
border: 1px solid var(--border-muted);
color: rgba(255,255,255,0.65);
}
.btn-ghost:hover {
border-color: var(--border-strong);
color: var(--text-primary);
background: rgba(255,255,255,0.05);
}
.btn-ghost:active { transform: scale(0.97); }
.btn-primary {
background: var(--gradient-cta);
color: #060606;
border: 1px solid transparent;
font-weight: 600;
}
.btn-primary:hover { opacity: 0.88; transform: translateY(-1px); }
.btn-primary:active { transform: translateY(0); opacity: 1; }
.btn-icon {
background: transparent;
border: 1px solid transparent;
color: var(--text-muted);
padding: 0.3rem 0.5rem;
border-radius: var(--radius-sm);
font-size: 0.875rem;
line-height: 1;
transition: color var(--dur) var(--ease), background var(--dur) var(--ease);
}
.btn-icon:hover { color: var(--ink-red); background: rgba(242,139,130,0.10); }
.btn-copy-ok {
border-color: var(--ink-green) !important;
color: var(--ink-green) !important;
}
/* ── Inputs & textarea ─────────────────────────────────────────────────── */
textarea, input[type="text"], select {
background: var(--bg-elevated);
border: 1px solid var(--border-subtle);
border-radius: var(--radius-md);
color: var(--text-primary);
outline: none;
transition: border-color var(--dur) var(--ease);
}
textarea:focus, input[type="text"]:focus, select:focus {
border-color: var(--ink-purple);
}
textarea.has-error, select.has-error { border-color: var(--ink-red); }
textarea {
width: 100%;
flex: 1;
min-height: 160px;
font-family: var(--font-mono);
font-size: 0.8rem;
line-height: 1.65;
padding: 0.75rem;
resize: none;
}
textarea::placeholder { color: var(--text-muted); }
/* ── Error message ─────────────────────────────────────────────────────── */
.error-msg {
font-size: 0.8125rem;
color: var(--ink-red);
padding: 0.5rem 0.75rem;
background: rgba(242,139,130,0.07);
border: 1px solid rgba(242,139,130,0.22);
border-radius: var(--radius-sm);
line-height: 1.45;
}
/* ── Array picker ──────────────────────────────────────────────────────── */
.picker-group {
display: flex;
flex-direction: column;
gap: 0.4rem;
}
.field-label {
font-size: 0.75rem;
font-weight: 500;
color: var(--text-muted);
text-transform: uppercase;
letter-spacing: 0.07em;
}
select {
padding: 0.375rem 0.625rem;
font-size: 0.875rem;
}
/* ── Field chips ───────────────────────────────────────────────────────── */
.chips-wrap {
display: flex;
flex-wrap: wrap;
gap: 0.375rem;
margin-top: 0.35rem;
}
.chip {
display: inline-flex;
align-items: center;
padding: 0.2rem 0.6rem;
background: var(--code-bg);
border: 1px solid rgba(172, 135, 235, 0.28);
border-radius: var(--radius-pill);
font-family: var(--font-mono);
font-size: 0.75rem;
color: var(--ink-purple);
user-select: none;
transition: background var(--dur) var(--ease),
border-color var(--dur) var(--ease),
color var(--dur) var(--ease);
}
.chip.clickable {
cursor: pointer;
}
.chip.clickable:hover {
background: rgba(172,135,235,0.18);
border-color: rgba(172,135,235,0.55);
color: var(--text-primary);
}
.chip.clickable:active { transform: scale(0.95); }
.chip.added {
background: rgba(166, 227, 161, 0.10);
border-color: rgba(166, 227, 161, 0.35);
color: var(--ink-green);
}
.chip.added:hover {
background: rgba(166, 227, 161, 0.18);
border-color: rgba(166, 227, 161, 0.55);
color: var(--ink-green);
}
/* ── Mapping rows ──────────────────────────────────────────────────────── */
.rows-list {
display: flex;
flex-direction: column;
gap: 0.4rem;
}
.map-row {
display: flex;
align-items: stretch;
gap: 0;
background: var(--bg-elevated);
border: 1px solid var(--border-subtle);
border-radius: var(--radius-md);
overflow: hidden;
transition: border-color var(--dur) var(--ease);
}
.map-row:focus-within { border-color: var(--border-muted); }
.map-row input[type="text"],
.map-row textarea {
background: transparent;
border: none;
border-radius: 0;
padding: 0.45rem 0.6rem;
font-family: var(--font-mono);
font-size: 0.8rem;
outline: none;
transition: none;
}
.map-row input[type="text"]:focus,
.map-row textarea:focus { border-color: transparent; }
.row-key {
flex: 0 0 36%;
color: var(--ink-blue);
border-right: 1px solid var(--border-subtle) !important;
}
.row-key::placeholder { color: var(--text-muted); }
.row-colon {
flex-shrink: 0;
padding: 0.45rem 0.35rem 0;
font-family: var(--font-mono);
font-size: 0.8rem;
color: var(--text-muted);
pointer-events: none;
align-self: flex-start;
}
.row-value {
flex: 1;
color: var(--text-primary);
min-width: 0;
resize: none;
overflow: hidden;
white-space: pre-wrap;
word-break: break-all;
line-height: 1.4;
min-height: 30px;
box-sizing: border-box;
}
.row-value::placeholder { color: var(--text-muted); }
.row-del {
flex-shrink: 0;
background: transparent;
border: none;
border-left: 1px solid var(--border-subtle);
color: var(--text-muted);
padding: 0 0.55rem;
font-size: 0.875rem;
line-height: 1;
height: 100%;
min-height: 32px;
display: flex;
align-items: center;
cursor: pointer;
transition: color var(--dur) var(--ease), background var(--dur) var(--ease);
}
.row-del:hover { color: var(--ink-red); background: rgba(242,139,130,0.10); }
/* ── Empty state ───────────────────────────────────────────────────────── */
.empty-state {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 0.4rem;
padding: 2.5rem 1rem;
color: var(--text-muted);
font-size: 0.8375rem;
text-align: center;
line-height: 1.5;
}
.empty-state .icon { font-size: 1.6rem; opacity: 0.4; margin-bottom: 0.25rem; }
/* ── Output pre ────────────────────────────────────────────────────────── */
#output-display, #preview-display {
flex: 1;
background: var(--bg-elevated);
border: 1px solid var(--border-subtle);
border-radius: var(--radius-md);
padding: 0.875rem 1rem;
font-family: var(--font-mono);
font-size: 0.8rem;
line-height: 1.65;
white-space: pre-wrap;
word-break: break-all;
overflow: auto;
color: var(--text-primary);
min-height: 0;
}
#output-display.placeholder,
#preview-display.placeholder { color: var(--text-muted); font-style: italic; }
#output-display::-webkit-scrollbar,
#preview-display::-webkit-scrollbar { width: 5px; height: 5px; }
#output-display::-webkit-scrollbar-track,
#preview-display::-webkit-scrollbar-track { background: transparent; }
#output-display::-webkit-scrollbar-thumb,
#preview-display::-webkit-scrollbar-thumb { background: var(--border-subtle); border-radius: 3px; }
/* ── Right column (two stacked panels) ─────────────────────────────────── */
.col-right {
display: flex;
flex-direction: column;
height: 100%;
overflow: hidden;
}
.col-right .panel {
flex: 1;
height: auto;
min-height: 0;
border-right: none;
}
.col-right .panel:first-child {
border-bottom: 1px solid var(--border-subtle);
}
/* ── Info note ─────────────────────────────────────────────────────────── */
.note {
font-size: 0.8rem;
color: var(--text-muted);
line-height: 1.5;
}
.note strong { color: var(--ink-purple); font-weight: 500; }
/* ── Detected summary ─────────────────────────────────────────────────── */
.detect-summary {
font-size: 0.8125rem;
color: var(--text-secondary);
}
/* ── Signature tooltip ─────────────────────────────────────────────────── */
.sig-tooltip {
position: fixed;
z-index: 200;
background: #0e0f14;
border: 1px solid rgba(172, 135, 235, 0.35);
border-radius: var(--radius-sm);
padding: 0.3rem 0.65rem;
font-family: var(--font-mono);
font-size: 0.775rem;
line-height: 1.5;
color: var(--text-secondary);
pointer-events: none;
white-space: normal;
overflow: hidden;
box-shadow: 0 4px 20px rgba(0,0,0,0.6), 0 0 0 1px rgba(172,135,235,0.08);
max-width: min(480px, calc(100vw - 16px));
}
.sig-fn { color: var(--ink-purple); }
.sig-paren { color: var(--text-muted); }
.sig-param { color: var(--text-secondary); }
.sig-param-active { color: var(--ink-yellow); font-weight: 600; }
.sig-comma { color: var(--text-muted); }
.sig-desc { display: block; color: var(--text-muted); font-size: 0.71rem; margin-top: 0.15rem; }
</style>
</head>
<body>
<!-- ── Header ──────────────────────────────────────────────────────────── -->
<header class="app-header">
<h1>simple-select</h1>
<span class="sep"></span>
<span class="tagline">Power Automate · Select Action Builder</span>
</header>
<!-- ── Panels ──────────────────────────────────────────────────────────── -->
<main class="panels">
<!-- ── LEFT: JSON Input ──────────────────────────────────────────────── -->
<section class="panel" id="panel-left">
<div class="panel-header">
<span class="title">1. Paste your Array of JSON Objects [ {...} ]</span>
<span id="left-badge" class="badge badge-neutral hidden"></span>
</div>
<div class="panel-body">
<textarea
id="json-input"
spellcheck="false"
autocomplete="off"
placeholder="Paste a JSON array - The pasted string should begin with [ and end with ]"
></textarea>
<div id="json-error" class="error-msg hidden"></div>
<!-- Array picker (shown when object has multiple arrays) -->
<div id="array-picker-wrap" class="picker-group hidden">
<span class="field-label">Select array to map from</span>
<select id="array-picker"></select>
</div>
<!-- Detected info -->
<div id="detect-info" class="hidden">
<p id="detect-summary" class="detect-summary"></p>
<p class="note" style="margin-top:0.3rem">
Click <strong>field chips</strong> in the Map panel to add rows, or use <strong>+ Add custom row</strong> for custom expressions.
</p>
</div>
<p class="note">
Runs locally in your browser with no network calls or saved data.
</p>
</div>
</section>
<!-- ── MIDDLE: Mapping Builder ────────────────────────────────────────── -->
<section class="panel" id="panel-middle">
<div class="panel-header">
<span class="title">2. Build out the Select Action pairs</span>
<span id="row-badge" class="badge badge-neutral hidden"></span>
<button id="add-row-btn" class="btn btn-ghost">+ Add custom row</button>
</div>
<!-- Detected field chips (pinned, not in scroll area) -->
<div id="chips-shelf" class="chips-shelf hidden">
<span class="field-label" style="display:block; margin-bottom:0.4rem;">Detected fields — click to add</span>
<div id="field-chips" class="chips-wrap"></div>
</div>
<!-- Pivot-flattened nested array chips -->
<div id="pivot-shelf" class="chips-shelf hidden">
<span class="field-label" style="display:block; margin-bottom:0.4rem;">Pivot expressions — click to add</span>
<div id="pivot-chips" class="chips-wrap"></div>
</div>
<div class="panel-body">
<!-- Mapping rows -->
<div id="rows-list" class="rows-list"></div>
<!-- Empty state -->
<div id="rows-empty" class="empty-state">
<span class="icon">⇄</span>
<span>No mapping rows yet.</span>
<span>Paste JSON on the left, then click detected fields to add rows — or use <em>+ Add custom row</em>.</span>
</div>
</div>
</section>
<!-- ── RIGHT: two stacked panels ────────────────────────────────────── -->
<div class="col-right">
<!-- TOP: Map JSON output -->
<section class="panel" id="panel-output">
<div class="panel-header">
<span class="title">3. Copy & Paste into your Select Action (switch to text mode)</span>
<button id="copy-btn" class="btn btn-ghost">Copy</button>
</div>
<div class="panel-body">
<pre id="output-display" class="placeholder">// Add mapping rows on the left to build the output.</pre>
</div>
</section>
<!-- BOTTOM: Select action preview -->
<section class="panel" id="panel-preview">
<div class="panel-header">
<span class="title">PREVIEW</span>
<span id="preview-badge" class="badge badge-neutral hidden"></span>
</div>
<div class="panel-body">
<pre id="preview-display" class="placeholder">// Paste JSON and add rows to preview the Select action output.</pre>
</div>
</section>
</div>
</main>
<script>
'use strict';
// =============================================================================
// Section 1: Decimal precision helper
// =============================================================================
function isDecimal(v) {
return v !== null && typeof v === 'object' && v._decimal === true;
}
function decimal(str) {
return { _decimal: true, str: String(str) };
}
function addDecimals(a, b) {
const aStr = a.str;
const bStr = b.str;
const aDot = aStr.indexOf('.');
const bDot = bStr.indexOf('.');
const aDP = aDot === -1 ? 0 : aStr.length - aDot - 1;
const bDP = bDot === -1 ? 0 : bStr.length - bDot - 1;
const maxDP = Math.max(aDP, bDP);
function toScaled(s, dp, targetDP) {
let intPart, fracPart;
const dot = s.indexOf('.');
if (dot === -1) {
intPart = s;
fracPart = '';
} else {
intPart = s.slice(0, dot);
fracPart = s.slice(dot + 1);
}
fracPart = fracPart.padEnd(targetDP, '0');
return BigInt(intPart + fracPart);
}
const aScaled = toScaled(aStr, aDP, maxDP);
const bScaled = toScaled(bStr, bDP, maxDP);
const sum = aScaled + bScaled;
const sumStr = sum.toString();
if (maxDP === 0) return { _decimal: true, str: sumStr };
const padded = sumStr.padStart(maxDP + 1, '0');
const intPart = padded.slice(0, padded.length - maxDP);
const fracPart = padded.slice(padded.length - maxDP);
return { _decimal: true, str: intPart + '.' + fracPart };
}
function decimalString(v) {
if (isDecimal(v)) return v.str;
return String(v);
}
function addValues(a, b) {
if (isDecimal(a) || isDecimal(b)) {
const da = isDecimal(a) ? a : decimal(String(a));
const db = isDecimal(b) ? b : decimal(String(b));
return addDecimals(da, db);
}
return a + b;
}
// =============================================================================
// Section 2: Tokenizer
// =============================================================================
const TT = { STRING: 'STRING', NUMBER: 'NUMBER', BOOL: 'BOOL', NULL: 'NULL',
IDENT: 'IDENT', LPAREN: 'LPAREN', RPAREN: 'RPAREN', COMMA: 'COMMA' };
function tokenize(src) {
const tokens = [];
let i = 0;
function peek() { return src[i]; }
function next() { return src[i++]; }
while (i < src.length) {
const ch = peek();
// whitespace
if (/\s/.test(ch)) { i++; continue; }
// string literal
if (ch === "'" || ch === '"') {
const q = next();
let s = '';
while (i < src.length && peek() !== q) {
if (peek() === '\\') { next(); }
s += next();
}
next(); // closing quote
tokens.push({ type: TT.STRING, value: s });
continue;
}
// number (including leading dot like .3)
// negative number only if preceded by LPAREN, COMMA, or start
const prevTT = tokens.length ? tokens[tokens.length - 1].type : null;
const canBeNeg = prevTT === null || prevTT === TT.LPAREN || prevTT === TT.COMMA;
if (/\d/.test(ch) || ch === '.' || (ch === '-' && canBeNeg && i + 1 < src.length && /[\d.]/.test(src[i + 1]))) {
let numStr = '';
if (ch === '-') numStr += next();
while (i < src.length && /\d/.test(peek())) numStr += next();
let isInt = true;
if (i < src.length && peek() === '.') {
isInt = false;
numStr += next(); // the dot
while (i < src.length && /\d/.test(peek())) numStr += next();
}
tokens.push({ type: TT.NUMBER, value: parseFloat(numStr), isInt });
continue;
}
if (ch === '(') { tokens.push({ type: TT.LPAREN }); i++; continue; }
if (ch === ')') { tokens.push({ type: TT.RPAREN }); i++; continue; }
if (ch === ',') { tokens.push({ type: TT.COMMA }); i++; continue; }
// identifier or keyword
if (/[a-zA-Z_$]/.test(ch)) {
let id = '';
while (i < src.length && /[a-zA-Z0-9_$]/.test(peek())) id += next();
if (id === 'true') tokens.push({ type: TT.BOOL, value: true });
else if (id === 'false') tokens.push({ type: TT.BOOL, value: false });
else if (id === 'null') tokens.push({ type: TT.NULL, value: null });
else tokens.push({ type: TT.IDENT, value: id });
continue;
}
throw new Error(`Unexpected character: ${ch}`);
}
return tokens;
}
// =============================================================================
// Section 3: Parser
// =============================================================================
function parse(tokens) {
let pos = 0;
function peek() { return tokens[pos]; }
function consume(type) {
const t = tokens[pos];
if (type && t.type !== type) throw new Error(`Expected ${type}, got ${t ? t.type : 'EOF'}`);
pos++;
return t;
}
function parseExpr() {
const t = peek();
if (!t) throw new Error('Unexpected end of input');
if (t.type === TT.IDENT) {
// look ahead for LPAREN → function call
if (tokens[pos + 1] && tokens[pos + 1].type === TT.LPAREN) {
const name = consume(TT.IDENT).value;
consume(TT.LPAREN);
const args = [];
if (peek() && peek().type !== TT.RPAREN) {
args.push(parseExpr());
while (peek() && peek().type === TT.COMMA) {
consume(TT.COMMA);
args.push(parseExpr());
}
}
consume(TT.RPAREN);
return { type: 'Call', name, args };
}
// bare identifier (shouldn't normally appear but handle it)
return { type: 'Literal', value: consume(TT.IDENT).value };
}
if (t.type === TT.STRING) return { type: 'Literal', value: consume(TT.STRING).value };
if (t.type === TT.NUMBER) {
const tok = consume(TT.NUMBER);
return { type: 'Literal', value: tok.value, isInt: tok.isInt };
}
if (t.type === TT.BOOL) return { type: 'Literal', value: consume(TT.BOOL).value };
if (t.type === TT.NULL) return { type: 'Literal', value: consume(TT.NULL).value };
throw new Error(`Unexpected token type: ${t.type}`);
}
const ast = parseExpr();
return ast;
}
// =============================================================================
// Section 4: DateTime helpers
// =============================================================================
function parseInputDate(str) {
if (!str) throw new Error('Empty date string');
// MM/DD/YYYY [HH:mm:ss]
const slashMatch = str.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})(?:\s+(\d{1,2}):(\d{2}):(\d{2}))?/);
if (slashMatch) {
const [, mo, da, yr, hh = '0', mm = '0', ss = '0'] = slashMatch;
const date = new Date(Date.UTC(+yr, +mo - 1, +da, +hh, +mm, +ss));
return { date, isUTC: false };
}
// ISO-like with Z or offset → UTC
const hasUTC = /Z$|[+-]\d{2}:?\d{2}$/.test(str.trim());
const date = new Date(str);
if (isNaN(date)) throw new Error(`Cannot parse date: ${str}`);
return { date, isUTC: hasUTC };
}
function pad(n, w) { return String(n).padStart(w, '0'); }
function toIso7(date, isUTC) {
const y = pad(date.getUTCFullYear(), 4);
const mo = pad(date.getUTCMonth() + 1, 2);
const da = pad(date.getUTCDate(), 2);
const hh = pad(date.getUTCHours(), 2);
const mm = pad(date.getUTCMinutes(), 2);
const ss = pad(date.getUTCSeconds(), 2);
const ms = pad(date.getUTCMilliseconds(), 3);
const frac = ms + '0000';
const suffix = isUTC ? 'Z' : '';
return `${y}-${mo}-${da}T${hh}:${mm}:${ss}.${frac}${suffix}`;
}
const LOCALE_CACHE = {};
function getLocaleNames(locale, timeZone) {
const key = `${locale}|${timeZone}`;
if (LOCALE_CACHE[key]) return LOCALE_CACHE[key];
const months = Array.from({length: 12}, (_, i) => {
return new Intl.DateTimeFormat(locale, { month: 'long', timeZone }).format(new Date(Date.UTC(2000, i, 1)));
});
const days = Array.from({length: 7}, (_, i) => {
// 2000-01-02 is a Sunday
return new Intl.DateTimeFormat(locale, { weekday: 'long', timeZone }).format(new Date(Date.UTC(2000, 0, 2 + i)));
});
LOCALE_CACHE[key] = { months, days };
return LOCALE_CACHE[key];
}
const FORMAT_TOKENS = ['fffffff','yyyy','MMMM','dddd','MM','dd','HH','mm','ss','K','M','d','yy','h'];
function formatWithPattern(date, pattern, locale, isUTC) {
if (!pattern || pattern === 'o') return toIso7(date, isUTC);
// Expand 'D' pattern
if (pattern === 'D') pattern = 'dddd, MMMM d, yyyy';
const tz = 'UTC';
const names = getLocaleNames(locale || 'en-US', tz);
const y = date.getUTCFullYear();
const mo = date.getUTCMonth(); // 0-based
const da = date.getUTCDate();
const hh = date.getUTCHours();
const mi = date.getUTCMinutes();
const ss = date.getUTCSeconds();
const ms = date.getUTCMilliseconds();
const dow = date.getUTCDay(); // 0=Sunday
const replacements = {
'fffffff': pad(ms, 3) + '0000',
'yyyy': pad(y, 4),
'yy': String(y).slice(-2),
'MMMM': names.months[mo],
'MM': pad(mo + 1, 2),
'M': String(mo + 1),
'dddd': names.days[dow],
'dd': pad(da, 2),
'd': String(da),
'HH': pad(hh, 2),
'h': String(hh % 12 || 12),
'mm': pad(mi, 2),
'ss': pad(ss, 2),
'K': isUTC ? 'Z' : '',
};
let result = '';
let i = 0;
while (i < pattern.length) {
let matched = false;
for (const tok of FORMAT_TOKENS) {
if (pattern.startsWith(tok, i)) {
result += replacements[tok] !== undefined ? replacements[tok] : tok;
i += tok.length;
matched = true;
break;
}
}
if (!matched) {
result += pattern[i++];
}
}
return result;
}
function parseDateWithFormat(str, format) {
// Token extraction from format
const tokens = ['yyyyMMdd', 'ddMMyyyy', 'yyyy-MM-dd', 'MM/dd/yyyy', 'yyyy', 'MM', 'dd', 'HH', 'mm', 'ss'];
// Greedy parse: match format tokens against str
let year = 0, month = 1, day = 1, hour = 0, min = 0, sec = 0;
let fi = 0, si = 0;
const fmtTokens = [
{ tok: 'yyyy', len: 4, set: v => { year = v; } },
{ tok: 'MM', len: 2, set: v => { month = v; } },
{ tok: 'dd', len: 2, set: v => { day = v; } },
{ tok: 'HH', len: 2, set: v => { hour = v; } },
{ tok: 'mm', len: 2, set: v => { min = v; } },
{ tok: 'ss', len: 2, set: v => { sec = v; } },
];
while (fi < format.length) {
let matched = false;
for (const { tok, len, set } of fmtTokens) {
if (format.startsWith(tok, fi)) {
const val = parseInt(str.slice(si, si + len), 10);
set(val);
fi += tok.length;
si += len;
matched = true;
break;
}
}
if (!matched) {
// literal char — skip both
fi++;
si++;
}
}
return new Date(Date.UTC(year, month - 1, day, hour, min, sec));
}
function applyTimeUnit(date, n, unit) {
const ms = date.getTime();
switch (unit) {
case 'Second': return new Date(ms + n * 1000);
case 'Minute': return new Date(ms + n * 60000);
case 'Hour': return new Date(ms + n * 3600000);
case 'Day': return new Date(ms + n * 86400000);
case 'Week': return new Date(ms + n * 7 * 86400000);
case 'Month': {
const d = new Date(date);
d.setUTCMonth(d.getUTCMonth() + n);
return d;
}
case 'Year': {
const d = new Date(date);
d.setUTCFullYear(d.getUTCFullYear() + n);
return d;
}