MCPcopy Create free account
hub / github.com/scottrogowski/code2flow / createDuration

Function createDuration

tests/test_code/js/moment/moment.js:3445–3515  ·  view source on GitHub ↗
(input, key)

Source from the content-addressed store, hash-verified

3443 isoRegex = /^(-|\+)?P(?:([-+]?[0-9,.]*)Y)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)W)?(?:([-+]?[0-9,.]*)D)?(?:T(?:([-+]?[0-9,.]*)H)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)S)?)?$/;
3444
3445 function createDuration(input, key) {
3446 var duration = input,
3447 // matching against regexp is expensive, do it on demand
3448 match = null,
3449 sign,
3450 ret,
3451 diffRes;
3452
3453 if (isDuration(input)) {
3454 duration = {
3455 ms: input._milliseconds,
3456 d: input._days,
3457 M: input._months,
3458 };
3459 } else if (isNumber(input) || !isNaN(+input)) {
3460 duration = {};
3461 if (key) {
3462 duration[key] = +input;
3463 } else {
3464 duration.milliseconds = +input;
3465 }
3466 } else if ((match = aspNetRegex.exec(input))) {
3467 sign = match[1] === '-' ? -1 : 1;
3468 duration = {
3469 y: 0,
3470 d: toInt(match[DATE]) * sign,
3471 h: toInt(match[HOUR]) * sign,
3472 m: toInt(match[MINUTE]) * sign,
3473 s: toInt(match[SECOND]) * sign,
3474 ms: toInt(absRound(match[MILLISECOND] * 1000)) * sign, // the millisecond decimal point is included in the match
3475 };
3476 } else if ((match = isoRegex.exec(input))) {
3477 sign = match[1] === '-' ? -1 : 1;
3478 duration = {
3479 y: parseIso(match[2], sign),
3480 M: parseIso(match[3], sign),
3481 w: parseIso(match[4], sign),
3482 d: parseIso(match[5], sign),
3483 h: parseIso(match[6], sign),
3484 m: parseIso(match[7], sign),
3485 s: parseIso(match[8], sign),
3486 };
3487 } else if (duration == null) {
3488 // checks for null or undefined
3489 duration = {};
3490 } else if (
3491 typeof duration === 'object' &&
3492 ('from' in duration || 'to' in duration)
3493 ) {
3494 diffRes = momentsDifference(
3495 createLocal(duration.from),
3496 createLocal(duration.to)
3497 );
3498
3499 duration = {};
3500 duration.ms = diffRes.milliseconds;
3501 duration.M = diffRes.months;
3502 }

Callers 8

createInvalid$1Function · 0.85
getSetOffsetFunction · 0.85
createAdderFunction · 0.85
fromFunction · 0.85
toFunction · 0.85
addSubtract$1Function · 0.85
clone$1Function · 0.85
relativeTime$1Function · 0.85

Calls 8

isDurationFunction · 0.85
isNumberFunction · 0.85
toIntFunction · 0.85
absRoundFunction · 0.85
parseIsoFunction · 0.85
momentsDifferenceFunction · 0.85
createLocalFunction · 0.85
hasOwnPropFunction · 0.85

Tested by

no test coverage detected