@nlo/currency-formatter
Advanced tools
Comparing version 1.0.8 to 1.1.0
declare class CurrencyFormatter { | ||
private userFormatter; | ||
private gtmFormatter; | ||
formatCents(inputCents: number | string, allowNegative?: boolean): string | undefined; | ||
formatCentsForUser(inputCents: number): string; | ||
formatCentsForGTM(inputCents: number): string; | ||
formatCentsRounded(cents: number | string, allowNegative?: boolean): string | undefined; | ||
@@ -4,0 +8,0 @@ formatCentsForText(cents: number | string, allowNegative?: boolean): string | undefined; |
@@ -6,3 +6,9 @@ "use strict"; | ||
function CurrencyFormatter() { | ||
// The currency style isn't used here because it requires a currency and we don't want to prefix with a € sign | ||
this.userFormatter = new Intl.NumberFormat('nl-NL', { style: 'decimal', minimumFractionDigits: 2, maximumFractionDigits: 2 }); | ||
this.gtmFormatter = new Intl.NumberFormat('us-US', { useGrouping: false, style: 'decimal', minimumFractionDigits: 2, maximumFractionDigits: 2 }); | ||
} | ||
/* @deprecated from of 7-2-2023, it's still used in production environments | ||
* Replaced by formatCentsForUser and formatCentsForGTM | ||
*/ | ||
CurrencyFormatter.prototype.formatCents = function (inputCents, allowNegative) { | ||
@@ -14,38 +20,13 @@ if (allowNegative === void 0) { allowNegative = true; } | ||
} | ||
var isNegative = false; | ||
if (cents < 0) { | ||
isNegative = true; | ||
if (cents < 0 && !allowNegative) { | ||
cents *= -1; | ||
} | ||
var centsStr = null; | ||
if (!cents) { | ||
centsStr = '000'; | ||
} | ||
else if (cents < 10) { | ||
centsStr = '00' + cents; | ||
} | ||
else if (cents < 100) { | ||
centsStr = '0' + cents; | ||
} | ||
var money = (centsStr || cents + '') | ||
.split('') | ||
.reverse() | ||
.map(function (element, index) { | ||
if (index === 2) { | ||
// This is the last cent digit, so add a comma before it. | ||
return element + ','; | ||
} | ||
if (index > 3 && ((index + 1) % 3) === 0) { | ||
// Add period character at the thousand indicator. | ||
return element + '.'; | ||
} | ||
// This is a regular character. | ||
return element; | ||
}) | ||
.reverse().join(''); | ||
if (isNegative && allowNegative) { | ||
money = '-' + money; | ||
} | ||
return money; | ||
return this.formatCentsForUser(cents); | ||
}; | ||
CurrencyFormatter.prototype.formatCentsForUser = function (inputCents) { | ||
return this.userFormatter.format(inputCents / 100); | ||
}; | ||
CurrencyFormatter.prototype.formatCentsForGTM = function (inputCents) { | ||
return this.gtmFormatter.format(inputCents / 100); | ||
}; | ||
CurrencyFormatter.prototype.formatCentsRounded = function (cents, allowNegative) { | ||
@@ -52,0 +33,0 @@ var money = this.formatCents(cents, allowNegative); |
{ | ||
"name": "@nlo/currency-formatter", | ||
"version": "1.0.8", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -27,3 +27,3 @@ "main": "dist/index.js", | ||
}, | ||
"gitHead": "0c50485a48183bfeed8a6caef6d4a376d0a42349" | ||
"gitHead": "8160d27a0be6286b57d48787001ee1052c299b77" | ||
} |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
4137
77
1