New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

moment-duration-format

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

moment-duration-format - npm Package Compare versions

Comparing version 2.1.1 to 2.2.0

2

bower.json
{
"name": "moment-duration-format",
"version": "2.1.1",
"version": "2.2.0",
"description": "A moment.js plugin for formatting durations.",

@@ -5,0 +5,0 @@ "main": "lib/moment-duration-format.js",

@@ -1,4 +0,4 @@

/*! Moment Duration Format v2.1.1
/*! Moment Duration Format v2.2.0
* https://github.com/jsmreese/moment-duration-format
* Date: 2017-12-29
* Date: 2018-01-15
*

@@ -8,3 +8,3 @@ * Duration format plugin function for the Moment.js library

*
* Copyright 2017 John Madhavan-Reese
* Copyright 2018 John Madhavan-Reese
* Released under the MIT license

@@ -34,5 +34,59 @@ */

})(this, function (moment) {
// `Number#tolocaleString` is tested on plugin initialization.
// If the feature test passes, `toLocaleStringWorks` will be set to `true` and the
// native function will be used to generate formatted output. If the feature
// test fails, the fallback format function internal to this plugin will be
// used.
var toLocaleStringWorks = false;
// Token type names in order of descending magnitude.
var types = "escape years months weeks days hours minutes seconds milliseconds general".split(" ");
var bubbles = [
{
type: "seconds",
targets: [
{ type: "minutes", value: 60 },
{ type: "hours", value: 3600 },
{ type: "days", value: 86400 },
{ type: "weeks", value: 604800 },
{ type: "months", value: 2678400 },
{ type: "years", value: 31536000 }
]
},
{
type: "minutes",
targets: [
{ type: "hours", value: 60 },
{ type: "days", value: 1440 },
{ type: "weeks", value: 10080 },
{ type: "months", value: 44640 },
{ type: "years", value: 525600 }
]
},
{
type: "hours",
targets: [
{ type: "days", value: 24 },
{ type: "weeks", value: 168 },
{ type: "months", value: 744 },
{ type: "years", value: 8760 }
]
},
{
type: "days",
targets: [
{ type: "weeks", value: 7 },
{ type: "months", value: 31 },
{ type: "years", value: 365 }
]
},
{
type: "months",
targets: [
{ type: "years", value: 12 }
]
}
];
// stringIncludes

@@ -47,2 +101,218 @@ function stringIncludes(str, search) {

// repeatZero(qty)
// Returns "0" repeated `qty` times.
// `qty` must be a integer >= 0.
function repeatZero(qty) {
var result = "";
while (qty) {
result += "0";
qty -= 1;
}
return result;
}
function stringRound(digits) {
var digitsArray = digits.split("").reverse();
var i = 0;
var carry = true;
while (carry && i < digitsArray.length) {
if (i) {
if (digitsArray[i] === "9") {
digitsArray[i] = "0";
} else {
digitsArray[i] = (parseInt(digitsArray[i], 10) + 1).toString();
carry = false;
}
} else {
if (parseInt(digitsArray[i], 10) < 5) {
carry = false;
}
digitsArray[i] = "0";
}
i += 1;
}
if (carry) {
digitsArray.push("1");
}
return digitsArray.reverse().join("");
}
// formatNumber
// Formats any number greater than or equal to zero using these options:
// - userLocale
// - useToLocaleString
// - useGrouping
// - grouping
// - maximumSignificantDigits
// - minimumIntegerDigits
// - fractionDigits
// - groupingSeparator
// - decimalSeparator
//
// `useToLocaleString` will use `toLocaleString` for formatting.
// `userLocale` option is passed through to `toLocaleString`.
// `fractionDigits` is passed through to `maximumFractionDigits` and `minimumFractionDigits`
// Using `maximumSignificantDigits` will override `minimumIntegerDigits` and `fractionDigits`.
function formatNumber(number, options, userLocale) {
var useToLocaleString = options.useToLocaleString;
var useGrouping = options.useGrouping;
var grouping = useGrouping && options.grouping.slice();
var maximumSignificantDigits = options.maximumSignificantDigits;
var minimumIntegerDigits = options.minimumIntegerDigits || 1;
var fractionDigits = options.fractionDigits || 0;
var groupingSeparator = options.groupingSeparator;
var decimalSeparator = options.decimalSeparator;
if (useToLocaleString && userLocale) {
var localeStringOptions = {
minimumIntegerDigits: minimumIntegerDigits,
useGrouping: useGrouping
};
if (fractionDigits) {
localeStringOptions.maximumFractionDigits = fractionDigits;
localeStringOptions.minimumFractionDigits = fractionDigits;
}
if (maximumSignificantDigits) {
localeStringOptions.maximumSignificantDigits = maximumSignificantDigits;
}
return number.toLocaleString(userLocale, localeStringOptions);
}
var numberString;
// Add 1 to digit output length for floating point errors workaround. See below.
if (maximumSignificantDigits) {
numberString = number.toPrecision(maximumSignificantDigits + 1);
} else {
numberString = number.toFixed(fractionDigits + 1);
}
var integerString;
var fractionString;
var exponentString;
var temp = numberString.split("e");
exponentString = temp[1] || "";
temp = temp[0].split(".");
fractionString = temp[1] || "";
integerString = temp[0] || "";
// Workaround for floating point errors in `toFixed` and `toPrecision`.
// (3.55).toFixed(1); --> "3.5"
// (123.55 - 120).toPrecision(2); --> "3.5"
// (123.55 - 120); --> 3.549999999999997
// (123.55 - 120).toFixed(2); --> "3.55"
// Round by examing the string output of the next digit.
// *************** Implement String Rounding here ***********************
// Check integerString + fractionString length of toPrecision before rounding.
// Check length of fractionString from toFixed output before rounding.
var integerLength = integerString.length;
var fractionLength = fractionString.length;
var digitCount = integerLength + fractionLength;
var digits = integerString + fractionString;
if (maximumSignificantDigits && digitCount === (maximumSignificantDigits + 1) || !maximumSignificantDigits && fractionLength === (fractionDigits + 1)) {
// Round digits.
digits = stringRound(digits);
if (digits.length === digitCount + 1) {
integerLength = integerLength + 1;
}
// Discard final fractionDigit.
if (fractionLength) {
digits = digits.slice(0, -1);
}
// Separate integer and fraction.
integerString = digits.slice(0, integerLength);
fractionString = digits.slice(integerLength);
}
// Trim trailing zeroes from fractionString because toPrecision outputs
// precision, not significant digits.
if (maximumSignificantDigits) {
fractionString = fractionString.replace(/0*$/, "");
}
// Handle exponent.
var exponent = parseInt(exponentString, 10);
if (exponent > 0) {
if (fractionString.length <= exponent) {
fractionString = fractionString + repeatZero(exponent - fractionString.length);
integerString = integerString + fractionString;
fractionString = "";
} else {
integerString = integerString + fractionString.slice(0, exponent);
fractionString = fractionString.slice(exponent);
}
} else if (exponent < 0) {
fractionString = (repeatZero(Math.abs(exponent) - integerString.length) + integerString + fractionString);
integerString = "0";
}
if (!maximumSignificantDigits) {
// Trim or pad fraction when not using maximumSignificantDigits.
fractionString = fractionString.slice(0, fractionDigits);
if (fractionString.length < fractionDigits) {
fractionString = fractionString + repeatZero(fractionDigits - fractionString.length);
}
// Pad integer when using minimumIntegerDigits
// and not using maximumSignificantDigits.
if (integerString.length < minimumIntegerDigits) {
integerString = repeatZero(minimumIntegerDigits - integerString.length) + integerString;
}
}
var formattedString = "";
// Handle grouping.
if (useGrouping) {
temp = integerString;
var group;
while (temp.length) {
if (grouping.length) {
group = grouping.shift();
}
if (formattedString) {
formattedString = groupingSeparator + formattedString;
}
formattedString = temp.slice(-group) + formattedString;
temp = temp.slice(0, -group);
}
} else {
formattedString = integerString;
}
// Add decimalSeparator and fraction.
if (fractionString) {
formattedString = formattedString + decimalSeparator + fractionString;
}
return formattedString;
}
// durationLabelCompare

@@ -324,2 +594,48 @@ function durationLabelCompare(a, b) {

function toLocaleStringSupportsLocales() {
var number = 0;
try {
number.toLocaleString('i');
} catch (e) {
return e.name === 'RangeError';
}
return false;
}
function featureTestToLocaleString() {
var passed = true;
// Test locale.
passed = passed && toLocaleStringSupportsLocales();
if (!passed) { return false; }
// Test minimumIntegerDigits.
passed = passed && (1).toLocaleString("en", { minimumIntegerDigits: 1 }) === "1";
passed = passed && (1).toLocaleString("en", { minimumIntegerDigits: 2 }) === "01";
passed = passed && (1).toLocaleString("en", { minimumIntegerDigits: 3 }) === "001";
if (!passed) { return false; }
// Test maximumFractionDigits and minimumFractionDigits.
passed = passed && (99.99).toLocaleString("en", { maximumFractionDigits: 0, minimumFractionDigits: 0 }) === "100";
passed = passed && (99.99).toLocaleString("en", { maximumFractionDigits: 1, minimumFractionDigits: 1 }) === "100.0";
passed = passed && (99.99).toLocaleString("en", { maximumFractionDigits: 2, minimumFractionDigits: 2 }) === "99.99";
passed = passed && (99.99).toLocaleString("en", { maximumFractionDigits: 3, minimumFractionDigits: 3 }) === "99.990";
if (!passed) { return false; }
// Test maximumSignificantDigits.
passed = passed && (99.99).toLocaleString("en", { maximumSignificantDigits: 1 }) === "100";
passed = passed && (99.99).toLocaleString("en", { maximumSignificantDigits: 2 }) === "100";
passed = passed && (99.99).toLocaleString("en", { maximumSignificantDigits: 3 }) === "100";
passed = passed && (99.99).toLocaleString("en", { maximumSignificantDigits: 4 }) === "99.99";
passed = passed && (99.99).toLocaleString("en", { maximumSignificantDigits: 5 }) === "99.99";
if (!passed) { return false; }
// Test grouping.
passed = passed && (1000).toLocaleString("en", { useGrouping: true }) === "1,000";
passed = passed && (1000).toLocaleString("en", { useGrouping: false }) === "1000";
if (!passed) { return false; }
return true;
}
// durationsFormat(durations [, template] [, precision] [, settings])

@@ -398,2 +714,3 @@ function durationsFormat() {

asMilliseconds = 0;
asMonths = 0;
}

@@ -552,3 +869,10 @@

// formatNumber fallback options.
var useToLocaleString = settings.useToLocaleString;
var groupingSeparator = settings.groupingSeparator;
var decimalSeparator = settings.decimalSeparator;
var grouping = settings.grouping;
useToLocaleString = useToLocaleString && toLocaleStringWorks;
// Trim options.

@@ -741,3 +1065,4 @@ var trim = settings.trim;

var truncate = function (value, places) {
return truncMethod(value * Math.pow(10, places)) * Math.pow(10, -places);
var factor = Math.pow(10, places);
return truncMethod(value * factor) / factor;
};

@@ -749,4 +1074,8 @@

var formatValue = function (momentType, index) {
var localeStringOptions = {
useGrouping: useGrouping
var formatOptions = {
useGrouping: useGrouping,
groupingSeparator: groupingSeparator,
decimalSeparator: decimalSeparator,
grouping: grouping,
useToLocaleString: useToLocaleString
};

@@ -760,3 +1089,3 @@

} else {
localeStringOptions.maximumSignificantDigits = significantDigits;
formatOptions.maximumSignificantDigits = significantDigits;
momentType.significantDigits = significantDigits;

@@ -804,4 +1133,3 @@ }

} else {
localeStringOptions.minimumFractionDigits = precision;
localeStringOptions.maximumFractionDigits = precision;
formatOptions.fractionDigits = precision;

@@ -827,6 +1155,6 @@ if (trunc) {

if (momentType.tokenLength > 1 && (forceLength || foundFirst)) {
localeStringOptions.minimumIntegerDigits = momentType.tokenLength;
formatOptions.minimumIntegerDigits = momentType.tokenLength;
if (bubbled && localeStringOptions.maximumSignificantDigits < momentType.tokenLength) {
delete localeStringOptions.maximumSignificantDigits;
if (bubbled && formatOptions.maximumSignificantDigits < momentType.tokenLength) {
delete formatOptions.maximumSignificantDigits;
}

@@ -839,12 +1167,13 @@ }

momentType.formattedValue = momentType.value.toLocaleString(userLocale, localeStringOptions);
momentType.formattedValue = formatNumber(momentType.value, formatOptions, userLocale);
localeStringOptions.useGrouping = false;
momentType.formattedValueEnUS = momentType.value.toLocaleString("en-US", localeStringOptions);
formatOptions.useGrouping = false;
formatOptions.decimalSeparator = ".";
momentType.formattedValueEn = formatNumber(momentType.value, formatOptions, "en");
if (momentType.tokenLength === 2 && momentType.type === "milliseconds") {
momentType.formattedValueMS = momentType.value.toLocaleString("en-US", {
momentType.formattedValueMS = formatNumber(momentType.value, {
minimumIntegerDigits: 3,
useGrouping: false
}).slice(0, 2);
}, "en").slice(0, 2);
}

@@ -867,49 +1196,2 @@

var bubbles = [
{
type: "seconds",
targets: [
{ type: "minutes", value: 60 },
{ type: "hours", value: 3600 },
{ type: "days", value: 86400 },
{ type: "weeks", value: 604800 },
{ type: "months", value: 2678400 },
{ type: "years", value: 31536000 }
]
},
{
type: "minutes",
targets: [
{ type: "hours", value: 60 },
{ type: "days", value: 1440 },
{ type: "weeks", value: 10080 },
{ type: "months", value: 44640 },
{ type: "years", value: 525600 }
]
},
{
type: "hours",
targets: [
{ type: "days", value: 24 },
{ type: "weeks", value: 168 },
{ type: "months", value: 744 },
{ type: "years", value: 8760 }
]
},
{
type: "days",
targets: [
{ type: "weeks", value: 7 },
{ type: "months", value: 31 },
{ type: "years", value: 365 }
]
},
{
type: "months",
targets: [
{ type: "years", value: 12 }
]
}
];
var bubbleTypes = function (bubble) {

@@ -929,3 +1211,3 @@ var bubbleMomentType = findType(bubble.type);

if (parseInt(bubbleMomentType.formattedValueEnUS, 10) === target.value) {
if (parseInt(bubbleMomentType.formattedValueEn, 10) === target.value) {
bubbleMomentType.rawValue = 0;

@@ -937,3 +1219,3 @@ bubbleMomentType.wholeValue = 0;

targetMomentType.decimalValue = 0;
targetMomentType.formattedValueEnUS = targetMomentType.wholeValue.toString();
targetMomentType.formattedValueEn = targetMomentType.wholeValue.toString();
bubbled = true;

@@ -1013,17 +1295,3 @@ }

}
/*
// Max Value.
if (maxValue && momentTypes.length && momentTypes[0].isLargest && momentTypes[0].rawValue > maxValue) {
if (!outputTypes) {
momentTypes = momentTypes.slice(0, 1);
}
momentTypes[0].isMaxValue = true;
}
// Min Value.
if (minValue && momentTypes.length === 1 && momentTypes[0].isSmallest && momentTypes[0].rawValue < minValue) {
momentTypes[0].isMinValue = true;
}
*/
if (returnMomentTypes) {

@@ -1045,3 +1313,3 @@ return momentTypes;

var values = momentType.formattedValueEnUS.split(".");
var values = momentType.formattedValueEn.split(".");

@@ -1351,3 +1619,33 @@ values[0] = parseInt(values[0], 10);

// period, and colon characters are trimmed from the resulting string.
template: defaultFormatTemplate
template: defaultFormatTemplate,
// useToLocaleString
// Set this option to `false` to ignore the `toLocaleString` feature
// test and force the use of the `formatNumber` fallback function
// included in this plugin.
useToLocaleString: true,
// formatNumber fallback options.
// When `toLocaleString` is detected and passes the feature test, the
// following options will have no effect: `toLocaleString` will be used
// for formatting and the grouping separator, decimal separator, and
// integer digit grouping will be determined by the user locale.
// groupingSeparator
// The integer digit grouping separator used when using the fallback
// formatNumber function.
groupingSeparator: ",",
// decimalSeparator
// The decimal separator used when using the fallback formatNumber
// function.
decimalSeparator: ".",
// grouping
// The integer digit grouping used when using the fallback formatNumber
// function. Must be an array. The default value of `[3]` gives the
// standard 3-digit thousand/million/billion digit groupings for the
// "en" locale. Setting this option to `[3, 2]` would generate the
// thousand/lakh/crore digit groupings used in the "en-IN" locale.
grouping: [3]
};

@@ -1358,2 +1656,5 @@

// Run feature test for `Number#toLocaleString`.
toLocaleStringWorks = featureTestToLocaleString();
// Initialize duration format on the global moment instance.

@@ -1360,0 +1661,0 @@ init(moment);

{
"name": "moment-duration-format",
"version": "2.1.1",
"version": "2.2.0",
"description": "A moment.js plugin for formatting durations.",

@@ -5,0 +5,0 @@ "main": "lib/moment-duration-format.js",

@@ -13,2 +13,14 @@ # Moment Duration Format

## Important Note
Where it is available and functional, this plugin uses `Number#toLocaleString` to render formatted numerical output. Unfortunately, many environments do not fully implement the full suite of options in the `toLocaleString` spec, and some provide a buggy implementation.
This plugin runs a feature test for `toLocaleString`, and will revert to a fallback function to render formatted numerical output if the feature test fails. To force this plugin to always use the fallback function, set `useToLocaleString` to `false`. The fallback function output can be localized using options detailed below.
This plugin is tested using BrowserStack on a range of Android devices with OS versions from 2.2 to 7, and on a range of iOS devices with OS versions from 4.3 to 11. Also tested on Chrome, Firefox, IE 8-11, and Edge browsers.
<a href="https://www.browserstack.com"><img src="https://p3.zdusercontent.com/attachment/1015988/Y0ZmOS3862TDx3JYOUTMIixSG?token=eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0..xr8Y-gqQNBDnpIjxOFtAtA.JHb-wwL0uWT5ChR01yhMKp2lvM0iMdeYdoJYLCqs_DIiod7HmRoaGnRMoptV1GlrwF2Mo73Oj1d08E3rM7RHQPzwP3M10g4aN-vWcC2K743sf1qUKE_2TGVaz1HLkfUxF49U5hfE6AZ9V9ALE-Nu-GwfR0xcJVBz-FeV-H7YseaX_fXsO4pt1F3DjcwqhM1pcKfxoC5wYc2CHQnnqp1xS67KfTA6kuMiSDovZqSQpvg5VYZqAlDmxpKkZvOmzP_yEptqk4CDkl5IMItvxPjjaw.w7SKsx3c665glH7fgdcSIw" height="64"></a>
---
## Installation

@@ -809,3 +821,3 @@

Formatted numerical output is rendered using [`toLocaleString`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString).
Formatted numerical output is rendered using [`toLocaleString`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString) if that built-in function is available and passes a feature test on plugin initialization. If the feature test fails, a fallback format function is used. See below for details on localizing output from the fallback format function.

@@ -945,13 +957,2 @@ Unit labels are automatically localized and pluralized. Unit labels are detected using the [locale set in moment.js](https://momentjs.com/docs/#/i18n/), which can be different from the locale of user's environment. This plugin uses custom extensions to the moment.js locale object, which can be easily added for any locale (see below).

#### Decimal Separator
Previous versions of the plugin used a `decimalSeparator` option. That option is no longer used and will have no effect. Decimal separators are rendered using `toLocalString` and the user's locale.
```javascript
moment.duration(1234567, "seconds").format("m [minutes]", 3, {
userLocale: "de-DE"
});
// "20.576,117 minutes"
```
#### Extending Moment's `locale` object

@@ -1107,1 +1108,33 @@

Number. The decimal fraction portion of the token's value.
### Localization and the Fallback Format Function
#### `useToLocaleString`
Set this option to `false` to ignore the `toLocaleString` feature test and force the use of the `formatNumber` fallback function included in this plugin.
The following options will have no effect when `toLocaleString` is used. The grouping separator, decimal separator, and integer digit grouping will be determined by the user locale.
#### `groupingSeparator`
The integer digit grouping separator used when using the fallback formatNumber function. Default value is a `,` character.
#### `decimalSeparator`
The decimal separator used when using the fallback formatNumber function. Default value is a `.` character.
#### `grouping`
The integer digit grouping used when using the fallback formatNumber function. Must be an array. The default value of `[3]` gives the standard 3-digit thousand/million/billion digit groupings for the "en" locale. Setting this option to `[3, 2]` would generate the thousand/lakh/crore digit groupings used in the "en-IN" locale.
```javascript
// Some sort of strange hybrid french-indian locale...
moment.duration(100000000000, "seconds").format("m", {
useToLocaleString: false,
precision: 2,
decimalSeparator: ",",
groupingSeparator: " ",
grouping: [3, 2]
});
// "1 66 66 66 666,67");
```

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc