@@ -1745,14 +1745,18 @@ export const ES = ObjectAssign({}, ES2020, {
1745
1745
if ( mergeFields === undefined ) return { ...fields , ...additionalFields } ;
1746
1746
return ES . Call ( mergeFields , calendar , [ fields , additionalFields ] ) ;
1747
1747
} ,
1748
- CalendarDateAdd : ( calendar , date , duration , options ) => {
1749
- const dateAdd = ES . GetMethod ( calendar , 'dateAdd' ) ;
1748
+ CalendarDateAdd : ( calendar , date , duration , options , dateAdd ) => {
1749
+ if ( dateAdd === undefined ) {
1750
+ dateAdd = ES . GetMethod ( calendar , 'dateAdd' ) ;
1751
+ }
1750
1752
const result = ES . Call ( dateAdd , calendar , [ date , duration , options ] ) ;
1751
1753
if ( ! ES . IsTemporalDate ( result ) ) throw new TypeError ( 'invalid result' ) ;
1752
1754
return result ;
1753
1755
} ,
1754
- CalendarDateUntil : ( calendar , date , otherDate , options ) => {
1755
- const dateUntil = ES . GetMethod ( calendar , 'dateUntil' ) ;
1756
+ CalendarDateUntil : ( calendar , date , otherDate , options , dateUntil ) => {
1757
+ if ( dateUntil === undefined ) {
1758
+ dateUntil = ES . GetMethod ( calendar , 'dateUntil' ) ;
1759
+ }
1756
1760
const result = ES . Call ( dateUntil , calendar , [ date , otherDate , options ] ) ;
1757
1761
if ( ! ES . IsTemporalDuration ( result ) ) throw new TypeError ( 'invalid result' ) ;
1758
1762
return result ;
@@ -2858,15 +2862,24 @@ export const ES = ObjectAssign({}, ES2020, {
2858
2862
// no-op
2859
2863
break ;
2860
2864
case 'months' :
2861
- if ( ! calendar ) throw new RangeError ( 'a starting point is required for months balancing' ) ;
2862
- // balance years down to months
2863
- while ( MathAbs ( years ) > 0 ) {
2864
- const newRelativeTo = ES . CalendarDateAdd ( calendar , relativeTo , oneYear , { } ) ;
2865
- const oneYearMonths = ES . CalendarDateUntil ( calendar , relativeTo , newRelativeTo , { largestUnit : 'months' } )
2866
- . months ;
2867
- relativeTo = newRelativeTo ;
2868
- months += oneYearMonths ;
2869
- years -= sign ;
2865
+ {
2866
+ if ( ! calendar ) throw new RangeError ( 'a starting point is required for months balancing' ) ;
2867
+ // balance years down to months
2868
+ const dateAdd = ES . GetMethod ( calendar , 'dateAdd' ) ;
2869
+ const dateUntil = ES . GetMethod ( calendar , 'dateUntil' ) ;
2870
+ while ( MathAbs ( years ) > 0 ) {
2871
+ const newRelativeTo = ES . CalendarDateAdd ( calendar , relativeTo , oneYear , { } , dateAdd ) ;
2872
+ const oneYearMonths = ES . CalendarDateUntil (
2873
+ calendar ,
2874
+ relativeTo ,
2875
+ newRelativeTo ,
2876
+ { largestUnit : 'months' } ,
2877
+ dateUntil
2878
+ ) . months ;
2879
+ relativeTo = newRelativeTo ;
2880
+ months += oneYearMonths ;
2881
+ years -= sign ;
2882
+ }
2870
2883
}
2871
2884
break ;
2872
2885
case 'weeks' :
@@ -2958,14 +2971,28 @@ export const ES = ObjectAssign({}, ES2020, {
2958
2971
}
2959
2972
2960
2973
// balance months up to years
2961
- newRelativeTo = ES . CalendarDateAdd ( calendar , relativeTo , oneYear , { } ) ;
2962
- let oneYearMonths = ES . CalendarDateUntil ( calendar , relativeTo , newRelativeTo , { largestUnit : 'months' } ) . months ;
2974
+ const dateAdd = ES . GetMethod ( calendar , 'dateAdd' ) ;
2975
+ newRelativeTo = ES . CalendarDateAdd ( calendar , relativeTo , oneYear , { } , dateAdd ) ;
2976
+ const dateUntil = ES . GetMethod ( calendar , 'dateUntil' ) ;
2977
+ let oneYearMonths = ES . CalendarDateUntil (
2978
+ calendar ,
2979
+ relativeTo ,
2980
+ newRelativeTo ,
2981
+ { largestUnit : 'months' } ,
2982
+ dateUntil
2983
+ ) . months ;
2963
2984
while ( MathAbs ( months ) >= MathAbs ( oneYearMonths ) ) {
2964
2985
months -= oneYearMonths ;
2965
2986
years += sign ;
2966
2987
relativeTo = newRelativeTo ;
2967
- newRelativeTo = ES . CalendarDateAdd ( calendar , relativeTo , oneYear , { } ) ;
2968
- oneYearMonths = ES . CalendarDateUntil ( calendar , relativeTo , newRelativeTo , { largestUnit : 'months' } ) . months ;
2988
+ newRelativeTo = ES . CalendarDateAdd ( calendar , relativeTo , oneYear , { } , dateAdd ) ;
2989
+ oneYearMonths = ES . CalendarDateUntil (
2990
+ calendar ,
2991
+ relativeTo ,
2992
+ newRelativeTo ,
2993
+ { largestUnit : 'months' } ,
2994
+ dateUntil
2995
+ ) . months ;
2969
2996
}
2970
2997
break ;
2971
2998
}
@@ -3527,9 +3554,9 @@ export const ES = ObjectAssign({}, ES2020, {
3527
3554
) ;
3528
3555
const dateDuration1 = new TemporalDuration ( y1 , mon1 , w1 , d1 , 0 , 0 , 0 , 0 , 0 , 0 ) ;
3529
3556
const dateDuration2 = new TemporalDuration ( y2 , mon2 , w2 , d2 , 0 , 0 , 0 , 0 , 0 , 0 ) ;
3530
- const dateAdd = calendar . dateAdd ;
3531
- const intermediate = ES . Call ( dateAdd , calendar , [ datePart , dateDuration1 , { } ] ) ;
3532
- const end = ES . Call ( dateAdd , calendar , [ intermediate , dateDuration2 , { } ] ) ;
3557
+ const dateAdd = ES . GetMethod ( calendar , ' dateAdd' ) ;
3558
+ const intermediate = ES . CalendarDateAdd ( calendar , datePart , dateDuration1 , { } , dateAdd ) ;
3559
+ const end = ES . CalendarDateAdd ( calendar , intermediate , dateDuration2 , { } , dateAdd ) ;
3533
3560
3534
3561
const dateLargestUnit = ES . LargerOfTwoTemporalDurationUnits ( 'days' , largestUnit ) ;
3535
3562
( { years, months, weeks, days } = ES . CalendarDateUntil ( calendar , datePart , end , {
@@ -4075,18 +4102,20 @@ export const ES = ObjectAssign({}, ES2020, {
4075
4102
4076
4103
// convert months and weeks to days by calculating difference(
4077
4104
// relativeTo + years, relativeTo + { years, months, weeks })
4078
- const yearsLater = ES . CalendarDateAdd ( calendar , relativeTo , new TemporalDuration ( years ) , { } ) ;
4105
+ const yearsDuration = new TemporalDuration ( years ) ;
4106
+ const dateAdd = ES . GetMethod ( calendar , 'dateAdd' ) ;
4107
+ const yearsLater = ES . CalendarDateAdd ( calendar , relativeTo , yearsDuration , { } , dateAdd ) ;
4079
4108
const yearsMonthsWeeks = new TemporalDuration ( years , months , weeks ) ;
4080
- const yearsMonthsWeeksLater = ES . CalendarDateAdd ( calendar , relativeTo , yearsMonthsWeeks , { } ) ;
4109
+ const yearsMonthsWeeksLater = ES . CalendarDateAdd ( calendar , relativeTo , yearsMonthsWeeks , { } , dateAdd ) ;
4081
4110
const monthsWeeksInDays = ES . DaysUntil ( yearsLater , yearsMonthsWeeksLater ) ;
4082
4111
relativeTo = yearsLater ;
4083
4112
days += monthsWeeksInDays ;
4084
4113
4085
- const daysLater = ES . CalendarDateAdd ( calendar , relativeTo , { days } , { } ) ;
4114
+ const daysLater = ES . CalendarDateAdd ( calendar , relativeTo , { days } , { } , dateAdd ) ;
4086
4115
const yearsPassed = ES . CalendarDateUntil ( calendar , relativeTo , daysLater , { largestUnit : 'years' } ) . years ;
4087
4116
years += yearsPassed ;
4088
4117
const oldRelativeTo = relativeTo ;
4089
- relativeTo = ES . CalendarDateAdd ( calendar , relativeTo , { years : yearsPassed } , { } ) ;
4118
+ relativeTo = ES . CalendarDateAdd ( calendar , relativeTo , { years : yearsPassed } , { } , dateAdd ) ;
4090
4119
const daysPassed = ES . DaysUntil ( oldRelativeTo , relativeTo ) ;
4091
4120
days -= daysPassed ;
4092
4121
const oneYear = new TemporalDuration ( days < 0 ? - 1 : 1 ) ;
@@ -4113,9 +4142,10 @@ export const ES = ObjectAssign({}, ES2020, {
4113
4142
// convert weeks to days by calculating difference(relativeTo +
4114
4143
// { years, months }, relativeTo + { years, months, weeks })
4115
4144
const yearsMonths = new TemporalDuration ( years , months ) ;
4116
- const yearsMonthsLater = ES . CalendarDateAdd ( calendar , relativeTo , yearsMonths , { } ) ;
4145
+ const dateAdd = ES . GetMethod ( calendar , 'dateAdd' ) ;
4146
+ const yearsMonthsLater = ES . CalendarDateAdd ( calendar , relativeTo , yearsMonths , { } , dateAdd ) ;
4117
4147
const yearsMonthsWeeks = new TemporalDuration ( years , months , weeks ) ;
4118
- const yearsMonthsWeeksLater = ES . CalendarDateAdd ( calendar , relativeTo , yearsMonthsWeeks , { } ) ;
4148
+ const yearsMonthsWeeksLater = ES . CalendarDateAdd ( calendar , relativeTo , yearsMonthsWeeks , { } , dateAdd ) ;
4119
4149
const weeksInDays = ES . DaysUntil ( yearsMonthsLater , yearsMonthsWeeksLater ) ;
4120
4150
relativeTo = yearsMonthsLater ;
4121
4151
days += weeksInDays ;
0 commit comments