Skip to content

Commit

Permalink
Allow unicode minus sign (U+2212) in negative durations as well
Browse files Browse the repository at this point in the history
See: #814
  • Loading branch information
ptomato committed Aug 24, 2020
1 parent 249e34e commit da69c47
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export const ES = ObjectAssign({}, ES2019, {
if (match.slice(2).every((element) => element === undefined)) {
throw new RangeError(`invalid duration: ${isoString}`);
}
const sign = match[1] === '-' ? -1 : 1;
const sign = match[1] === '-' || match[1] === '\u2212' ? -1 : 1;
const years = ES.ToInteger(match[2]) * sign;
const months = ES.ToInteger(match[3]) * sign;
const weeks = ES.ToInteger(match[4]) * sign;
Expand Down
2 changes: 1 addition & 1 deletion polyfill/lib/regex.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ export const time = new RegExp(`^${timesplit.source}(?:${zonesplit.source})?(?:$
export const yearmonth = new RegExp(`^(${yearpart.source})-?(\\d{2})$`);
export const monthday = /^(?:--)?(\d{2})-?(\d{2})$/;

export const duration = /^([+-])?P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)W)?(?:(\d+)D)?(?:T(?!$)(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)(?:[.,](\d{1,9}))?S)?)?$/i;
export const duration = /^([+-\u2212])?P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)W)?(?:(\d+)D)?(?:T(?!$)(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)(?:[.,](\d{1,9}))?S)?)?$/i;
4 changes: 4 additions & 0 deletions polyfill/test/duration.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ describe('Duration', () => {
const d = Duration.from('-P1D');
equal(d.days, -1);
});
it('variant minus sign', () => {
const d = Duration.from('\u2212P1D');
equal(d.days, -1);
});
it('all units have the same sign', () => {
const d = Duration.from('-P1Y1M1W1DT1H1M1.123456789S');
equal(d.years, -1);
Expand Down
2 changes: 1 addition & 1 deletion polyfill/test/validStrings.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ const durationYears = seq(
);
const durationDate = seq(choice(durationYears, durationMonths, durationWeeks, durationDays), [durationTime]);
const duration = seq(
withCode([sign], (data, result) => (data.factor = result === '-' ? -1 : 1)),
withCode([sign], (data, result) => (data.factor = result === '-' || result === '\u2212' ? -1 : 1)),
durationDesignator,
choice(durationDate, durationTime)
);
Expand Down
2 changes: 1 addition & 1 deletion spec/abstractops.html
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ <h1>ParseTemporalDurationString ( _isoString_ )</h1>
1. If _isoString_ does not satisfy the syntax of a |TemporalDurationString| (see <emu-xref href="#sec-temporal-iso8601grammar"></emu-xref>), then
1. Throw a *RangeError* exception.
1. Let _sign_, _years_, _months_, _weeks_, _days_, _hours_, _minutes_, _seconds_, and _fraction_ be the parts of _isoString_ produced respectively by the |Sign|, |DurationYears|, |DurationMonths|, |DurationWeeks|, |DurationDays|, |DurationHours|, |DurationMinutes|, |DurationSeconds|, and |TimeFractionalPart| productions, or *undefined* if not present.
1. If _sign_ is *"-"*, then
1. If _sign_ is the code unit 0x002D (HYPHEN-MINUS) or 0x2212 (MINUS SIGN), then
1. Let _factor_ be &minus;1;.
1. Else,
1. Let _factor_ be 1.
Expand Down

0 comments on commit da69c47

Please sign in to comment.