@barqjs/aria 0.2.0-next.0 → 0.2.0-next.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/README.md +24 -13
- package/package.json +127 -37
- package/src/autocomplete.tsx +581 -0
- package/src/breadcrumbs.tsx +21 -20
- package/src/button.tsx +151 -25
- package/src/calendar.tsx +1019 -92
- package/src/checkbox.tsx +214 -52
- package/src/collections.ts +88 -5
- package/src/color.ts +1580 -23
- package/src/colorpicker.tsx +698 -153
- package/src/colorswatchpicker.tsx +240 -0
- package/src/combobox.tsx +635 -113
- package/src/command.tsx +78 -59
- package/src/date.ts +31 -9
- package/src/datefield.tsx +1451 -336
- package/src/datepicker.tsx +465 -114
- package/src/dialog.tsx +302 -115
- package/src/disclosure.tsx +81 -29
- package/src/dnd/clipboard.ts +125 -0
- package/src/dnd/collection.ts +1265 -0
- package/src/dnd/data.ts +409 -0
- package/src/dnd/drag-drop.ts +801 -0
- package/src/dnd/index.ts +133 -0
- package/src/dnd/manager.ts +639 -0
- package/src/dnd/strings.ts +1279 -0
- package/src/dnd/targets.ts +726 -0
- package/src/dnd/types.ts +212 -0
- package/src/dom.ts +3 -6
- package/src/drag-and-drop.tsx +669 -0
- package/src/dropzone.tsx +208 -0
- package/src/file-trigger.tsx +103 -0
- package/src/form.tsx +18 -18
- package/src/gridlist.tsx +912 -121
- package/src/group.tsx +146 -0
- package/src/index.ts +402 -0
- package/src/interactions/focus-events.ts +13 -5
- package/src/interactions/keyboard.ts +195 -2
- package/src/interactions/modality.ts +18 -6
- package/src/interactions/open-link.ts +46 -13
- package/src/interactions/press.ts +35 -18
- package/src/label.ts +18 -6
- package/src/link.tsx +146 -50
- package/src/listbox.tsx +615 -89
- package/src/live.ts +4 -0
- package/src/menu.tsx +182 -89
- package/src/menubar.tsx +21 -17
- package/src/navigation-tree.tsx +357 -0
- package/src/numberfield.tsx +164 -60
- package/src/overlay-arrow.tsx +157 -0
- package/src/overlays.ts +364 -62
- package/src/platform.ts +2 -1
- package/src/preview-trigger.tsx +582 -0
- package/src/progress-bar-context.ts +15 -0
- package/src/questionnaire.tsx +109 -104
- package/src/radio.tsx +101 -27
- package/src/select.tsx +225 -145
- package/src/selection-indicator.tsx +74 -0
- package/src/selection.ts +73 -16
- package/src/shared-element-transition.tsx +316 -0
- package/src/sheet.tsx +1461 -0
- package/src/slider.tsx +139 -44
- package/src/switch.tsx +47 -21
- package/src/table.tsx +0 -0
- package/src/tabs.tsx +198 -89
- package/src/tag.tsx +217 -32
- package/src/textarea.tsx +186 -0
- package/src/textfield.tsx +103 -37
- package/src/toast-components.tsx +589 -0
- package/src/toggle-button-group.tsx +175 -0
- package/src/toggle-input.tsx +38 -0
- package/src/toggle.ts +68 -5
- package/src/tokenfield.tsx +1673 -0
- package/src/toolbar.tsx +15 -10
- package/src/tooltip.tsx +46 -29
- package/src/tree.tsx +2075 -0
- package/src/utils.ts +30 -1
- package/src/validation.ts +79 -22
- package/src/virtualizer.tsx +186 -23
package/src/command.tsx
CHANGED
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
*/
|
|
40
40
|
|
|
41
41
|
import {
|
|
42
|
+
cells,
|
|
42
43
|
type Accessor,
|
|
43
44
|
type Child,
|
|
44
45
|
createContext,
|
|
@@ -46,7 +47,6 @@ import {
|
|
|
46
47
|
createMemo,
|
|
47
48
|
createSignal,
|
|
48
49
|
getContext,
|
|
49
|
-
type Incoming,
|
|
50
50
|
onCleanup,
|
|
51
51
|
onSettled,
|
|
52
52
|
provide,
|
|
@@ -572,9 +572,13 @@ export function command(options: CommandOptions, state: CommandState): CommandRe
|
|
|
572
572
|
const baseId = id();
|
|
573
573
|
const label = (): string | undefined => access(options.label) ?? access(options["aria-label"]);
|
|
574
574
|
const itemId = (key: Key): string => `${baseId()}-item-${String(key)}`;
|
|
575
|
+
// While the caret is being moved, the screen reader has to read the text and
|
|
576
|
+
// not the option: the highlight stays, and the input stops naming it until
|
|
577
|
+
// the next move into the list.
|
|
578
|
+
const [isListActive, setListActive] = createSignal(true, { ownedWrite: true });
|
|
575
579
|
const activeId = (): string | undefined => {
|
|
576
580
|
const key = state.highlightedKey();
|
|
577
|
-
return key === null ? undefined : itemId(key);
|
|
581
|
+
return key === null || !isListActive() ? undefined : itemId(key);
|
|
578
582
|
};
|
|
579
583
|
|
|
580
584
|
/** Scroll to an item, and to its group's heading when it is the group's first. */
|
|
@@ -595,6 +599,7 @@ export function command(options: CommandOptions, state: CommandState): CommandRe
|
|
|
595
599
|
|
|
596
600
|
const step = (event: KeyboardEvent, by: 1 | -1): void => {
|
|
597
601
|
event.preventDefault();
|
|
602
|
+
setListActive(true);
|
|
598
603
|
if (event.metaKey) reveal(state.moveToEdge(by === 1 ? "last" : "first"));
|
|
599
604
|
else if (event.altKey) reveal(state.moveToGroup(by));
|
|
600
605
|
else reveal(state.move(by));
|
|
@@ -603,7 +608,12 @@ export function command(options: CommandOptions, state: CommandState): CommandRe
|
|
|
603
608
|
const onKeyDown = (event: KeyboardEvent): void => {
|
|
604
609
|
if (event.defaultPrevented || isComposing(event)) return;
|
|
605
610
|
|
|
611
|
+
const nothingHighlighted = untrack(state.highlightedKey) === null;
|
|
606
612
|
switch (event.key) {
|
|
613
|
+
case "ArrowLeft":
|
|
614
|
+
case "ArrowRight":
|
|
615
|
+
setListActive(false);
|
|
616
|
+
break;
|
|
607
617
|
case "n":
|
|
608
618
|
case "j":
|
|
609
619
|
if (state.vimBindings() && event.ctrlKey) step(event, 1);
|
|
@@ -619,12 +629,12 @@ export function command(options: CommandOptions, state: CommandState): CommandRe
|
|
|
619
629
|
step(event, -1);
|
|
620
630
|
break;
|
|
621
631
|
case "Home":
|
|
622
|
-
event.preventDefault();
|
|
623
|
-
reveal(state.moveToEdge("first"));
|
|
624
|
-
break;
|
|
625
632
|
case "End":
|
|
633
|
+
// Shift extends the text selection, unless the list is what is moving.
|
|
634
|
+
if (event.shiftKey && nothingHighlighted) break;
|
|
626
635
|
event.preventDefault();
|
|
627
|
-
|
|
636
|
+
setListActive(true);
|
|
637
|
+
reveal(state.moveToEdge(event.key === "Home" ? "first" : "last"));
|
|
628
638
|
break;
|
|
629
639
|
case "Enter":
|
|
630
640
|
event.preventDefault();
|
|
@@ -638,10 +648,21 @@ export function command(options: CommandOptions, state: CommandState): CommandRe
|
|
|
638
648
|
// A new search starts again from the top, where its best match is.
|
|
639
649
|
createEffect(state.search, (now, previous) => {
|
|
640
650
|
if (previous === undefined || previous === now) return;
|
|
651
|
+
setListActive(true);
|
|
641
652
|
const list = untrack(() => access(options.listRef)) as HTMLElement | null;
|
|
642
653
|
if (list !== null) list.scrollTop = 0;
|
|
643
654
|
});
|
|
644
655
|
|
|
656
|
+
createEffect(state.highlightedKey, (now, previous) => {
|
|
657
|
+
if (previous !== undefined && previous !== now) setListActive(true);
|
|
658
|
+
});
|
|
659
|
+
|
|
660
|
+
const onPointerDown = (event: PointerEvent): void => {
|
|
661
|
+
// A touch opens the on-screen keyboard rather than placing a caret.
|
|
662
|
+
if (event.button !== 0 || event.pointerType === "touch") return;
|
|
663
|
+
setListActive(false);
|
|
664
|
+
};
|
|
665
|
+
|
|
645
666
|
return {
|
|
646
667
|
label,
|
|
647
668
|
itemId,
|
|
@@ -659,6 +680,8 @@ export function command(options: CommandOptions, state: CommandState): CommandRe
|
|
|
659
680
|
"aria-label": () =>
|
|
660
681
|
access(options.label) === undefined ? access(options["aria-label"]) : undefined,
|
|
661
682
|
"aria-activedescendant": activeId,
|
|
683
|
+
enterkeyhint: "go",
|
|
684
|
+
onPointerDown,
|
|
662
685
|
},
|
|
663
686
|
listProps: {
|
|
664
687
|
"cmdk-list": "",
|
|
@@ -897,10 +920,10 @@ export function useCommand(): CommandContextValue {
|
|
|
897
920
|
return value;
|
|
898
921
|
}
|
|
899
922
|
|
|
900
|
-
function CommandProvider(props:
|
|
923
|
+
function CommandProvider(props: { value: CommandContextValue; children?: Child }) {
|
|
901
924
|
return provide(
|
|
902
925
|
CommandContext,
|
|
903
|
-
() => props.value
|
|
926
|
+
() => props.value,
|
|
904
927
|
() => props.children as unknown,
|
|
905
928
|
);
|
|
906
929
|
}
|
|
@@ -926,9 +949,9 @@ export interface CommandComponentProps extends StyleProps {
|
|
|
926
949
|
onValueChange?: (value: string) => void;
|
|
927
950
|
}
|
|
928
951
|
|
|
929
|
-
export function Command(props:
|
|
952
|
+
export function Command(props: CommandComponentProps) {
|
|
930
953
|
const listRef = makeRef<HTMLDivElement>();
|
|
931
|
-
const options = fromProps(props);
|
|
954
|
+
const options = fromProps(cells(props));
|
|
932
955
|
const state = commandState(options);
|
|
933
956
|
const { commandProps, labelProps, inputProps, listProps, label, itemId } = command(
|
|
934
957
|
{ label: options.label, "aria-label": options["aria-label"], listRef },
|
|
@@ -939,13 +962,13 @@ export function Command(props: Incoming<CommandComponentProps>) {
|
|
|
939
962
|
const elementProps = mergeProps(
|
|
940
963
|
commandProps,
|
|
941
964
|
filterDOMProps(options, { global: true }),
|
|
942
|
-
styleProps(props),
|
|
943
|
-
{ "data-testid": () => props["data-testid"]
|
|
965
|
+
styleProps(cells(props)),
|
|
966
|
+
{ "data-testid": () => props["data-testid"] },
|
|
944
967
|
);
|
|
945
968
|
|
|
946
969
|
return (
|
|
947
|
-
<div {...elementProps} ref={mergeRefs(props.ref
|
|
948
|
-
<Show when={props.label
|
|
970
|
+
<div {...elementProps} ref={mergeRefs(props.ref)}>
|
|
971
|
+
<Show when={props.label !== undefined}>
|
|
949
972
|
<label {...labelProps}>{label()}</label>
|
|
950
973
|
</Show>
|
|
951
974
|
<CommandProvider value={value}>{props.children}</CommandProvider>
|
|
@@ -964,24 +987,24 @@ export interface CommandInputComponentProps extends StyleProps {
|
|
|
964
987
|
onValueChange?: (search: string) => void;
|
|
965
988
|
}
|
|
966
989
|
|
|
967
|
-
export function CommandInput(props:
|
|
990
|
+
export function CommandInput(props: CommandInputComponentProps) {
|
|
968
991
|
const held = useCommand();
|
|
969
|
-
const options = fromProps(props);
|
|
992
|
+
const options = fromProps(cells(props));
|
|
970
993
|
const { inputProps } = commandInput(options, held.state, held.inputProps);
|
|
971
994
|
|
|
972
995
|
const elementProps = mergeProps(
|
|
973
996
|
inputProps,
|
|
974
997
|
filterDOMProps(options, { global: true, propNames: INPUT_PROPS }),
|
|
975
|
-
styleProps(props),
|
|
998
|
+
styleProps(cells(props)),
|
|
976
999
|
{
|
|
977
1000
|
"aria-label": () =>
|
|
978
|
-
props["aria-label"]
|
|
1001
|
+
props["aria-label"] ??
|
|
979
1002
|
access(held.inputProps["aria-label"] as MaybeAccessor<string | undefined>),
|
|
980
|
-
"data-testid": () => props["data-testid"]
|
|
1003
|
+
"data-testid": () => props["data-testid"],
|
|
981
1004
|
},
|
|
982
1005
|
);
|
|
983
1006
|
|
|
984
|
-
return <input {...elementProps} ref={mergeRefs(props.ref
|
|
1007
|
+
return <input {...elementProps} ref={mergeRefs(props.ref)} />;
|
|
985
1008
|
}
|
|
986
1009
|
|
|
987
1010
|
const INPUT_PROPS = new Set(["placeholder", "disabled", "autoFocus", "onKeyDown", "onKeyUp"]);
|
|
@@ -992,22 +1015,22 @@ export interface CommandListComponentProps extends StyleProps {
|
|
|
992
1015
|
ref?: RefTarget<HTMLDivElement>;
|
|
993
1016
|
}
|
|
994
1017
|
|
|
995
|
-
export function CommandList(props:
|
|
1018
|
+
export function CommandList(props: CommandListComponentProps) {
|
|
996
1019
|
const held = useCommand();
|
|
997
1020
|
const elementProps = mergeProps(
|
|
998
1021
|
held.listProps,
|
|
999
|
-
filterDOMProps(fromProps(props), { global: true }),
|
|
1000
|
-
styleProps(props),
|
|
1022
|
+
filterDOMProps(fromProps(cells(props)), { global: true }),
|
|
1023
|
+
styleProps(cells(props)),
|
|
1001
1024
|
{
|
|
1002
1025
|
"aria-label": () =>
|
|
1003
|
-
props["aria-label"]
|
|
1026
|
+
props["aria-label"] ??
|
|
1004
1027
|
access(held.listProps["aria-label"] as MaybeAccessor<string | undefined>),
|
|
1005
|
-
"data-testid": () => props["data-testid"]
|
|
1028
|
+
"data-testid": () => props["data-testid"],
|
|
1006
1029
|
},
|
|
1007
1030
|
);
|
|
1008
1031
|
|
|
1009
1032
|
return (
|
|
1010
|
-
<div {...elementProps} ref={mergeRefs(held.listRef.set, props.ref
|
|
1033
|
+
<div {...elementProps} ref={mergeRefs(held.listRef.set, props.ref)}>
|
|
1011
1034
|
{/* Presentational, or it would stand between the listbox and its options. */}
|
|
1012
1035
|
<div cmdk-list-sizer="" role="presentation" style={COLUMN}>
|
|
1013
1036
|
{props.children}
|
|
@@ -1021,15 +1044,15 @@ export interface CommandEmptyComponentProps extends StyleProps {
|
|
|
1021
1044
|
}
|
|
1022
1045
|
|
|
1023
1046
|
/** Rendered while the search matches nothing, and not otherwise. */
|
|
1024
|
-
export function CommandEmpty(props:
|
|
1047
|
+
export function CommandEmpty(props: CommandEmptyComponentProps) {
|
|
1025
1048
|
const held = useCommand();
|
|
1026
1049
|
return (
|
|
1027
1050
|
<Show when={held.state.isEmpty()}>
|
|
1028
1051
|
<div
|
|
1029
1052
|
cmdk-empty=""
|
|
1030
1053
|
role="presentation"
|
|
1031
|
-
{...filterDOMProps(fromProps(props), { global: true })}
|
|
1032
|
-
{...styleProps(props)}
|
|
1054
|
+
{...filterDOMProps(fromProps(cells(props)), { global: true })}
|
|
1055
|
+
{...styleProps(cells(props))}
|
|
1033
1056
|
>
|
|
1034
1057
|
{props.children}
|
|
1035
1058
|
</div>
|
|
@@ -1049,39 +1072,35 @@ export interface CommandGroupComponentProps extends StyleProps {
|
|
|
1049
1072
|
ref?: RefTarget<HTMLDivElement>;
|
|
1050
1073
|
}
|
|
1051
1074
|
|
|
1052
|
-
export function CommandGroup(props:
|
|
1075
|
+
export function CommandGroup(props: CommandGroupComponentProps) {
|
|
1053
1076
|
const held = useCommand();
|
|
1054
1077
|
const group = commandGroup(
|
|
1055
1078
|
{
|
|
1056
|
-
heading: () => props.heading
|
|
1057
|
-
"aria-label": () => props["aria-label"]
|
|
1058
|
-
forceMount: () => props.forceMount
|
|
1079
|
+
heading: () => props.heading,
|
|
1080
|
+
"aria-label": () => props["aria-label"],
|
|
1081
|
+
forceMount: () => props.forceMount,
|
|
1059
1082
|
},
|
|
1060
1083
|
held.state,
|
|
1061
1084
|
);
|
|
1062
1085
|
|
|
1063
1086
|
const elementProps = mergeProps(
|
|
1064
1087
|
group.groupProps,
|
|
1065
|
-
filterDOMProps(fromProps(props), { global: true }),
|
|
1066
|
-
styleProps(props),
|
|
1088
|
+
filterDOMProps(fromProps(cells(props)), { global: true }),
|
|
1089
|
+
styleProps(cells(props)),
|
|
1067
1090
|
{
|
|
1068
1091
|
// The order comes last, so a caller's own `style` does not drop it.
|
|
1069
1092
|
style: () => ({
|
|
1070
|
-
...props.style
|
|
1093
|
+
...props.style,
|
|
1071
1094
|
...access(group.groupProps.style as MaybeAccessor<Record<string, number> | undefined>),
|
|
1072
1095
|
}),
|
|
1073
|
-
"data-testid": () => props["data-testid"]
|
|
1096
|
+
"data-testid": () => props["data-testid"],
|
|
1074
1097
|
},
|
|
1075
1098
|
);
|
|
1076
1099
|
|
|
1077
1100
|
return (
|
|
1078
|
-
<div {...elementProps} ref={mergeRefs(group.ref, props.ref
|
|
1079
|
-
<Show when={props.heading
|
|
1080
|
-
<div
|
|
1081
|
-
{...group.headingProps}
|
|
1082
|
-
class={props.headingClass?.()}
|
|
1083
|
-
data-slot={props.headingSlot?.()}
|
|
1084
|
-
>
|
|
1101
|
+
<div {...elementProps} ref={mergeRefs(group.ref, props.ref)}>
|
|
1102
|
+
<Show when={props.heading !== undefined}>
|
|
1103
|
+
<div {...group.headingProps} class={props.headingClass} data-slot={props.headingSlot}>
|
|
1085
1104
|
{props.heading}
|
|
1086
1105
|
</div>
|
|
1087
1106
|
</Show>
|
|
@@ -1101,9 +1120,9 @@ export interface CommandItemComponentProps extends StyleProps {
|
|
|
1101
1120
|
onSelect?: (value: string) => void;
|
|
1102
1121
|
}
|
|
1103
1122
|
|
|
1104
|
-
export function CommandItem(props:
|
|
1123
|
+
export function CommandItem(props: CommandItemComponentProps) {
|
|
1105
1124
|
const held = useCommand();
|
|
1106
|
-
const options = fromProps(props);
|
|
1125
|
+
const options = fromProps(cells(props));
|
|
1107
1126
|
const item = commandItem(
|
|
1108
1127
|
{
|
|
1109
1128
|
value: options.value,
|
|
@@ -1119,20 +1138,20 @@ export function CommandItem(props: Incoming<CommandItemComponentProps>) {
|
|
|
1119
1138
|
const elementProps = mergeProps(
|
|
1120
1139
|
item.itemProps,
|
|
1121
1140
|
filterDOMProps(options, { global: true, labelable: true }),
|
|
1122
|
-
styleProps(props),
|
|
1141
|
+
styleProps(cells(props)),
|
|
1123
1142
|
{
|
|
1124
1143
|
// The order comes last, so a caller's own `style` does not drop it.
|
|
1125
1144
|
style: () => ({
|
|
1126
|
-
...props.style
|
|
1145
|
+
...props.style,
|
|
1127
1146
|
...access(item.itemProps.style as MaybeAccessor<Record<string, number> | undefined>),
|
|
1128
1147
|
}),
|
|
1129
1148
|
"data-focused": item.isHighlighted,
|
|
1130
|
-
"data-testid": () => props["data-testid"]
|
|
1149
|
+
"data-testid": () => props["data-testid"],
|
|
1131
1150
|
},
|
|
1132
1151
|
);
|
|
1133
1152
|
|
|
1134
1153
|
return (
|
|
1135
|
-
<div {...elementProps} ref={mergeRefs(item.ref, props.ref
|
|
1154
|
+
<div {...elementProps} ref={mergeRefs(item.ref, props.ref)}>
|
|
1136
1155
|
{props.children}
|
|
1137
1156
|
</div>
|
|
1138
1157
|
);
|
|
@@ -1143,15 +1162,15 @@ export interface CommandSeparatorComponentProps extends StyleProps {
|
|
|
1143
1162
|
alwaysRender?: boolean;
|
|
1144
1163
|
}
|
|
1145
1164
|
|
|
1146
|
-
export function CommandSeparator(props:
|
|
1165
|
+
export function CommandSeparator(props: CommandSeparatorComponentProps) {
|
|
1147
1166
|
const held = useCommand();
|
|
1148
1167
|
return (
|
|
1149
|
-
<Show when={props.alwaysRender
|
|
1168
|
+
<Show when={props.alwaysRender === true || held.state.search() === ""}>
|
|
1150
1169
|
<div
|
|
1151
1170
|
cmdk-separator=""
|
|
1152
1171
|
role="separator"
|
|
1153
|
-
{...filterDOMProps(fromProps(props), { global: true })}
|
|
1154
|
-
{...styleProps(props)}
|
|
1172
|
+
{...filterDOMProps(fromProps(cells(props)), { global: true })}
|
|
1173
|
+
{...styleProps(cells(props))}
|
|
1155
1174
|
/>
|
|
1156
1175
|
</Show>
|
|
1157
1176
|
);
|
|
@@ -1166,17 +1185,17 @@ export interface CommandLoadingComponentProps extends StyleProps {
|
|
|
1166
1185
|
}
|
|
1167
1186
|
|
|
1168
1187
|
/** Shown, by the caller's own condition, while the items are being fetched. */
|
|
1169
|
-
export function CommandLoading(props:
|
|
1188
|
+
export function CommandLoading(props: CommandLoadingComponentProps) {
|
|
1170
1189
|
return (
|
|
1171
1190
|
<div
|
|
1172
1191
|
cmdk-loading=""
|
|
1173
1192
|
role="progressbar"
|
|
1174
|
-
aria-valuenow={props.progress
|
|
1193
|
+
aria-valuenow={props.progress}
|
|
1175
1194
|
aria-valuemin={0}
|
|
1176
1195
|
aria-valuemax={100}
|
|
1177
|
-
aria-label={props.label
|
|
1178
|
-
{...filterDOMProps(fromProps(props), { global: true })}
|
|
1179
|
-
{...styleProps(props)}
|
|
1196
|
+
aria-label={props.label ?? "Loading..."}
|
|
1197
|
+
{...filterDOMProps(fromProps(cells(props)), { global: true })}
|
|
1198
|
+
{...styleProps(cells(props))}
|
|
1180
1199
|
>
|
|
1181
1200
|
<div aria-hidden="true">{props.children}</div>
|
|
1182
1201
|
</div>
|
package/src/date.ts
CHANGED
|
@@ -83,6 +83,10 @@ function fromEpochDay(epochDay: number): [number, number, number] {
|
|
|
83
83
|
return [month <= 2 ? year + 1 : year, month, day];
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
+
function within(value: number, min: number, max: number): number {
|
|
87
|
+
return Math.min(Math.max(value, min), max);
|
|
88
|
+
}
|
|
89
|
+
|
|
86
90
|
function pad(value: number, width: number): string {
|
|
87
91
|
return String(Math.abs(value)).padStart(width, "0");
|
|
88
92
|
}
|
|
@@ -216,10 +220,10 @@ export class Time {
|
|
|
216
220
|
readonly millisecond: number;
|
|
217
221
|
|
|
218
222
|
constructor(hour = 0, minute = 0, second = 0, millisecond = 0) {
|
|
219
|
-
this.hour = hour;
|
|
220
|
-
this.minute = minute;
|
|
221
|
-
this.second = second;
|
|
222
|
-
this.millisecond = millisecond;
|
|
223
|
+
this.hour = within(hour, 0, 23);
|
|
224
|
+
this.minute = within(minute, 0, 59);
|
|
225
|
+
this.second = within(second, 0, 59);
|
|
226
|
+
this.millisecond = within(millisecond, 0, 999);
|
|
223
227
|
}
|
|
224
228
|
|
|
225
229
|
set(fields: Partial<Record<TimeUnit, number>>): Time {
|
|
@@ -376,20 +380,38 @@ export type DateValue = CalendarDate | CalendarDateTime;
|
|
|
376
380
|
const DATE_PATTERN = /^(-?\d{4,})-(\d{2})-(\d{2})$/;
|
|
377
381
|
const TIME_PATTERN = /^(\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,3}))?)?$/;
|
|
378
382
|
|
|
379
|
-
/**
|
|
383
|
+
/** A field of a parsed string, refused when it names something that does not exist. */
|
|
384
|
+
function checked(text: string, min: number, max: number, value: string, kind: string): number {
|
|
385
|
+
const number = Number(text);
|
|
386
|
+
if (number < min || number > max) {
|
|
387
|
+
throw new RangeError(`Not an ISO 8601 ${kind}: ${JSON.stringify(value)}`);
|
|
388
|
+
}
|
|
389
|
+
return number;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* An ISO date, or a throw naming what was given.
|
|
394
|
+
*
|
|
395
|
+
* The 31st of February is refused rather than read as the 3rd of March: a
|
|
396
|
+
* string that names a day that does not exist is a mistake somewhere, and
|
|
397
|
+
* rolling it over hides the mistake inside a plausible date.
|
|
398
|
+
*/
|
|
380
399
|
export function parseDate(value: string): CalendarDate {
|
|
381
400
|
const match = DATE_PATTERN.exec(value);
|
|
382
401
|
if (match === null) throw new RangeError(`Not an ISO 8601 date: ${JSON.stringify(value)}`);
|
|
383
|
-
|
|
402
|
+
const year = Number(match[1]);
|
|
403
|
+
const month = checked(match[2] as string, 1, 12, value, "date");
|
|
404
|
+
const day = checked(match[3] as string, 1, getDaysInMonth(year, month), value, "date");
|
|
405
|
+
return new CalendarDate(year, month, day);
|
|
384
406
|
}
|
|
385
407
|
|
|
386
408
|
export function parseTime(value: string): Time {
|
|
387
409
|
const match = TIME_PATTERN.exec(value);
|
|
388
410
|
if (match === null) throw new RangeError(`Not an ISO 8601 time: ${JSON.stringify(value)}`);
|
|
389
411
|
return new Time(
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
412
|
+
checked(match[1] as string, 0, 23, value, "time"),
|
|
413
|
+
checked(match[2] as string, 0, 59, value, "time"),
|
|
414
|
+
checked(match[3] ?? "0", 0, 59, value, "time"),
|
|
393
415
|
Number((match[4] ?? "0").padEnd(3, "0")),
|
|
394
416
|
);
|
|
395
417
|
}
|