From 279ada1507c06a015e680e2e503c15f2c0c2352c Mon Sep 17 00:00:00 2001 From: Kshitij Bhardwaj Date: Sun, 11 Jun 2017 23:34:21 +0530 Subject: [PATCH] Support "+" / positive sign as well. Updated regex to support positive sign in addition to a negative sign. The `sign` is assigned a value of 1 if the regex finds "+" in the beginning. --- src/lib/duration/create.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/duration/create.js b/src/lib/duration/create.js index 9b6d5a964..656fb2337 100644 --- a/src/lib/duration/create.js +++ b/src/lib/duration/create.js @@ -9,12 +9,12 @@ import { createLocal } from '../create/local'; import { createInvalid as invalid } from './valid'; // ASP.NET json date format regex -var aspNetRegex = /^(\-)?(?:(\d*)[. ])?(\d+)\:(\d+)(?:\:(\d+)(\.\d*)?)?$/; +var aspNetRegex = /^(\-|\+)?(?:(\d*)[. ])?(\d+)\:(\d+)(?:\:(\d+)(\.\d*)?)?$/; // from http://docs.closure-library.googlecode.com/git/closure_goog_date_date.js.source.html // somewhat more in line with 4.4.3.2 2004 spec, but allows decimal anywhere // and further modified to allow for strings containing both week and day -var isoRegex = /^(-)?P(?:(-?[0-9,.]*)Y)?(?:(-?[0-9,.]*)M)?(?:(-?[0-9,.]*)W)?(?:(-?[0-9,.]*)D)?(?:T(?:(-?[0-9,.]*)H)?(?:(-?[0-9,.]*)M)?(?:(-?[0-9,.]*)S)?)?$/; +var isoRegex = /^(-|\+)?P(?:(-?[0-9,.]*)Y)?(?:(-?[0-9,.]*)M)?(?:(-?[0-9,.]*)W)?(?:(-?[0-9,.]*)D)?(?:T(?:(-?[0-9,.]*)H)?(?:(-?[0-9,.]*)M)?(?:(-?[0-9,.]*)S)?)?$/; export function createDuration (input, key) { var duration = input, @@ -38,7 +38,7 @@ export function createDuration (input, key) { duration.milliseconds = input; } } else if (!!(match = aspNetRegex.exec(input))) { - sign = (match[1] === '-') ? -1 : 1; + sign = (match[1] === '-') ? -1 : (match[1] === '+') ? 1 : 1; duration = { y : 0, d : toInt(match[DATE]) * sign, @@ -48,7 +48,7 @@ export function createDuration (input, key) { ms : toInt(absRound(match[MILLISECOND] * 1000)) * sign // the millisecond decimal point is included in the match }; } else if (!!(match = isoRegex.exec(input))) { - sign = (match[1] === '-') ? -1 : 1; + sign = (match[1] === '-') ? -1 : (match[1] === '+') ? 1 : 1; duration = { y : parseIso(match[2], sign), M : parseIso(match[3], sign), -- 2.47.2