@@ -317,7 +317,6 @@ const TEMPORAL_UNITS = [
317
317
] ;
318
318
const SINGULAR_FOR = new MapCtor ( TEMPORAL_UNITS ) ;
319
319
// Iterable destructuring is acceptable in this first-run code.
320
- const PLURAL_FOR = new MapCtor ( Call ( ArrayPrototypeMap , TEMPORAL_UNITS , [ ( [ p , s ] ) => [ s , p ] ] ) ) ;
321
320
const UNITS_DESCENDING = Call ( ArrayPrototypeMap , TEMPORAL_UNITS , [ ( [ , s ] ) => s ] ) ;
322
321
const NS_PER_TIME_UNIT = new MapCtor (
323
322
Call ( ArrayPrototypeFlatMap , TEMPORAL_UNITS , [ ( [ , s , , l ] ) => ( l ? [ [ s , l ] ] : [ ] ) ] )
@@ -918,38 +917,35 @@ export function ToSecondsStringPrecisionRecord(smallestUnit, precision) {
918
917
919
918
export const REQUIRED = SymbolCtor ( '~required~' ) ;
920
919
921
- export function GetTemporalUnitValuedOption ( options , key , unitGroup , requiredOrDefault , extraValues = [ ] ) {
922
- const allowedSingular = [ ] ;
920
+ export function GetTemporalUnitValuedOption ( options , key , requiredOrUndefined = undefined ) {
921
+ const allowedStrings = [ ] ;
923
922
for ( let index = 0 ; index < TEMPORAL_UNITS . length ; index ++ ) {
924
923
const unitInfo = TEMPORAL_UNITS [ index ] ;
924
+ const plural = unitInfo [ 0 ] ;
925
925
const singular = unitInfo [ 1 ] ;
926
- const category = unitInfo [ 2 ] ;
927
- if ( unitGroup === 'datetime' || unitGroup === category ) {
928
- Call ( ArrayPrototypePush , allowedSingular , [ singular ] ) ;
929
- }
930
- }
931
- Call ( ArrayPrototypePush , allowedSingular , extraValues ) ;
932
- let defaultVal = requiredOrDefault ;
933
- if ( defaultVal === REQUIRED ) {
934
- defaultVal = undefined ;
935
- } else if ( defaultVal !== undefined ) {
936
- Call ( ArrayPrototypePush , allowedSingular , [ defaultVal ] ) ;
937
- }
938
- const allowedValues = [ ] ;
939
- Call ( ArrayPrototypePush , allowedValues , allowedSingular ) ;
940
- for ( let index = 0 ; index < allowedSingular . length ; index ++ ) {
941
- const singular = allowedSingular [ index ] ;
942
- const plural = Call ( MapPrototypeGet , PLURAL_FOR , [ singular ] ) ;
943
- if ( plural !== undefined ) Call ( ArrayPrototypePush , allowedValues , [ plural ] ) ;
944
- }
945
- let retval = GetOption ( options , key , allowedValues , defaultVal ) ;
946
- if ( retval === undefined && requiredOrDefault === REQUIRED ) {
947
- throw new RangeErrorCtor ( `${ key } is required` ) ;
926
+ Call ( ArrayPrototypePush , allowedStrings , [ singular , plural ] ) ;
948
927
}
928
+ Call ( ArrayPrototypePush , allowedStrings , [ 'auto' ] ) ;
929
+ let retval = GetOption ( options , key , allowedStrings , requiredOrUndefined ) ;
930
+ if ( retval === undefined ) return undefined ;
949
931
if ( Call ( MapPrototypeHas , SINGULAR_FOR , [ retval ] ) ) retval = Call ( MapPrototypeGet , SINGULAR_FOR , [ retval ] ) ;
950
932
return retval ;
951
933
}
952
934
935
+ export function ValidateTemporalUnitValue ( value , unitGroup , extraValues = [ ] ) {
936
+ if ( value === undefined ) return ;
937
+ if ( Call ( ArrayPrototypeIncludes , extraValues , [ value ] ) ) return ;
938
+ for ( let index = 0 ; index < TEMPORAL_UNITS . length ; index ++ ) {
939
+ const unitInfo = TEMPORAL_UNITS [ index ] ;
940
+ const singular = unitInfo [ 0 ] ;
941
+ const plural = unitInfo [ 1 ] ;
942
+ const category = unitInfo [ 2 ] ;
943
+ if ( value !== singular && value !== plural ) continue ;
944
+ if ( unitGroup === 'datetime' || unitGroup === category ) return ;
945
+ }
946
+ throw new RangeErrorCtor ( `${ value } not allowed as a ${ unitGroup } unit` ) ;
947
+ }
948
+
953
949
export function GetTemporalRelativeToOption ( options ) {
954
950
// returns: {
955
951
// plainRelativeTo: Temporal.PlainDate | undefined
@@ -3640,7 +3636,9 @@ export function GetDifferenceSettings(op, options, group, disallowed, fallbackSm
3640
3636
[ ]
3641
3637
] ) ;
3642
3638
3643
- let largestUnit = GetTemporalUnitValuedOption ( options , 'largestUnit' , group , 'auto' ) ;
3639
+ let largestUnit = GetTemporalUnitValuedOption ( options , 'largestUnit' ) ;
3640
+ ValidateTemporalUnitValue ( largestUnit , group , [ 'auto' ] ) ;
3641
+ if ( ! largestUnit ) largestUnit = 'auto' ;
3644
3642
if ( Call ( ArrayPrototypeIncludes , disallowed , [ largestUnit ] ) ) {
3645
3643
throw new RangeErrorCtor (
3646
3644
`largestUnit must be one of ${ Call ( ArrayPrototypeJoin , ALLOWED_UNITS , [ ', ' ] ) } , not ${ largestUnit } `
@@ -3652,7 +3650,9 @@ export function GetDifferenceSettings(op, options, group, disallowed, fallbackSm
3652
3650
let roundingMode = GetRoundingModeOption ( options , 'trunc' ) ;
3653
3651
if ( op === 'since' ) roundingMode = NegateRoundingMode ( roundingMode ) ;
3654
3652
3655
- const smallestUnit = GetTemporalUnitValuedOption ( options , 'smallestUnit' , group , fallbackSmallest ) ;
3653
+ let smallestUnit = GetTemporalUnitValuedOption ( options , 'smallestUnit' ) ;
3654
+ ValidateTemporalUnitValue ( smallestUnit , group ) ;
3655
+ if ( ! smallestUnit ) smallestUnit = fallbackSmallest ;
3656
3656
if ( Call ( ArrayPrototypeIncludes , disallowed , [ smallestUnit ] ) ) {
3657
3657
throw new RangeErrorCtor (
3658
3658
`smallestUnit must be one of ${ Call ( ArrayPrototypeJoin , ALLOWED_UNITS , [ ', ' ] ) } , not ${ smallestUnit } `
0 commit comments