currencyformatter.ts
Advanced tools
Comparing version 2.2.1 to 2.2.2
@@ -1,2 +0,1 @@ | ||
"use strict"; | ||
// CurrencyFormatter.js | ||
@@ -10,5 +9,3 @@ // --------------------------------------------------------------------- | ||
// If you use this library in a commercial project, we appreciate a link back to https://osrec.co.uk :) | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.parse = exports.format = exports.getFormatter = exports.toFixed = exports.getFormatDetails = exports.locales = exports.defaultLocales = exports.symbols = void 0; | ||
exports.symbols = { | ||
export const symbols = { | ||
AED: 'د.إ.', | ||
@@ -171,3 +168,3 @@ AFN: '؋', | ||
}; | ||
exports.defaultLocales = { | ||
export const defaultLocales = { | ||
AED: 'ar_AE', | ||
@@ -332,3 +329,3 @@ AFN: 'fa_AF', | ||
}; | ||
exports.locales = { | ||
export const locales = { | ||
af: { p: '!#,##0.00', g: ' ', d: ',' }, | ||
@@ -1050,26 +1047,24 @@ af_NA: { h: 'af' }, | ||
}; | ||
exports.getFormatDetails = function (p) { | ||
export const getFormatDetails = function (p = {}) { | ||
// Perform checks on inputs and set up defaults as needed (defaults to en, USD) | ||
var _a, _b; | ||
if (p === void 0) { p = {}; } | ||
var currency = p.currency || 'USD'; | ||
var locale = exports.locales[p.locale || exports.defaultLocales[currency]]; | ||
const currency = p.currency || 'USD'; | ||
let locale = locales[p.locale || defaultLocales[currency]]; | ||
if (typeof locale.h !== 'undefined') { | ||
locale = exports.locales[locale.h]; | ||
locale = locales[locale.h]; | ||
} // Locale inheritance | ||
var symbol = typeof p.symbol === 'undefined' | ||
? exports.symbols[currency] || currency | ||
const symbol = typeof p.symbol === 'undefined' | ||
? symbols[currency] || currency | ||
: p.symbol === null | ||
? '' | ||
: p.symbol; | ||
var pattern = p.pattern || locale.p; | ||
var decimal = (_b = (_a = p.decimal) !== null && _a !== void 0 ? _a : locale.d) !== null && _b !== void 0 ? _b : '.'; | ||
var group = p.group || locale.g; | ||
var valueOnError = typeof p.valueOnError === 'undefined' ? 0 : p.valueOnError; | ||
var formatDetails = { | ||
pattern: pattern, | ||
decimal: decimal, | ||
group: group, | ||
symbol: symbol, | ||
valueOnError: valueOnError, | ||
const pattern = p.pattern || locale.p; | ||
const decimal = p.decimal ?? locale.d ?? '.'; | ||
const group = p.group || locale.g; | ||
const valueOnError = typeof p.valueOnError === 'undefined' ? 0 : p.valueOnError; | ||
const formatDetails = { | ||
pattern, | ||
decimal, | ||
group, | ||
symbol, | ||
valueOnError, | ||
postFormatFunction: p.postFormatFunction, | ||
@@ -1079,29 +1074,28 @@ }; | ||
}; | ||
exports.toFixed = function (n, precision) { | ||
export const toFixed = function (n, precision) { | ||
return (Math.round(Number(n) * Math.pow(10, precision)) / Math.pow(10, precision)).toFixed(precision); | ||
}; | ||
function getFormatter(p) { | ||
var formatDetails = exports.getFormatDetails(p); | ||
var pattern = formatDetails.pattern; | ||
var decimal = formatDetails.decimal; | ||
var group = formatDetails.group; | ||
var symbol = formatDetails.symbol; | ||
var valueOnError = formatDetails.valueOnError; | ||
var postFormatFunction = formatDetails.postFormatFunction; | ||
export function getFormatter(p) { | ||
const formatDetails = getFormatDetails(p); | ||
const pattern = formatDetails.pattern; | ||
const decimal = formatDetails.decimal; | ||
const group = formatDetails.group; | ||
const symbol = formatDetails.symbol; | ||
const valueOnError = formatDetails.valueOnError; | ||
const postFormatFunction = formatDetails.postFormatFunction; | ||
// encodePattern Function - returns a few simple characteristics of the pattern provided | ||
function encodePattern(pattern) { | ||
var _a; | ||
var matches = pattern.trim().match(/[#0,.]+/); | ||
var numberFormatPattern = (_a = matches === null || matches === void 0 ? void 0 : matches[0]) !== null && _a !== void 0 ? _a : ''; | ||
var split = numberFormatPattern.split('.'); | ||
var c = split[0]; // Decimal chars | ||
var m = split[1]; // Decimal mantissa | ||
var groups = c.split(','); | ||
var groupLengths = groups.map(function (g) { return g.length; }); | ||
var zeroLength = (groups[groups.length - 1].match(/0/g) || []).length; | ||
var decimalPlaces = typeof m === 'undefined' ? 0 : m.length; | ||
var paddingSplit = pattern.split(numberFormatPattern); | ||
var encodedPattern = { | ||
pattern: pattern, | ||
decimalPlaces: decimalPlaces, | ||
const matches = pattern.trim().match(/[#0,.]+/); | ||
const numberFormatPattern = matches?.[0] ?? ''; | ||
const split = numberFormatPattern.split('.'); | ||
const c = split[0]; // Decimal chars | ||
const m = split[1]; // Decimal mantissa | ||
const groups = c.split(','); | ||
const groupLengths = groups.map((g) => g.length); | ||
const zeroLength = (groups[groups.length - 1].match(/0/g) || []).length; | ||
const decimalPlaces = typeof m === 'undefined' ? 0 : m.length; | ||
const paddingSplit = pattern.split(numberFormatPattern); | ||
const encodedPattern = { | ||
pattern, | ||
decimalPlaces, | ||
frontPadding: paddingSplit[0], | ||
@@ -1115,4 +1109,4 @@ backPadding: paddingSplit[1], | ||
// Zero Padding helper function | ||
var pad = function (input, width) { | ||
var n = "" + input; | ||
const pad = function (input, width) { | ||
const n = `${input}`; | ||
return n.length >= width | ||
@@ -1124,8 +1118,8 @@ ? n | ||
function format(n, f) { | ||
var formattedNumber = exports.toFixed(Math.abs(n), f.decimalPlaces); | ||
var splitNumber = formattedNumber.split('.'); | ||
var segment = ''; | ||
var cursor = splitNumber[0].length; | ||
var maxGroupIndex = f.groupLengths.length - 1; | ||
var groupIndex = maxGroupIndex; | ||
const formattedNumber = toFixed(Math.abs(n), f.decimalPlaces); | ||
const splitNumber = formattedNumber.split('.'); | ||
let segment = ''; | ||
let cursor = splitNumber[0].length; | ||
const maxGroupIndex = f.groupLengths.length - 1; | ||
let groupIndex = maxGroupIndex; | ||
if (maxGroupIndex > 0) { | ||
@@ -1136,5 +1130,5 @@ while (cursor > 0) { | ||
} // Always reset to the last group length (useful for big numbers) | ||
var currentGroupLength = f.groupLengths[groupIndex]; | ||
var start = cursor - currentGroupLength; | ||
segment = "" + splitNumber[0].substring(start, cursor) + f.group + segment; | ||
const currentGroupLength = f.groupLengths[groupIndex]; | ||
const start = cursor - currentGroupLength; | ||
segment = `${splitNumber[0].substring(start, cursor)}${f.group}${segment}`; | ||
cursor -= currentGroupLength; | ||
@@ -1151,12 +1145,12 @@ --groupIndex; | ||
} | ||
var formatted = "" + f.frontPadding + segment + (splitNumber[1] === undefined ? '' : "" + f.decimal + splitNumber[1]) + f.backPadding; | ||
const formatted = `${f.frontPadding}${segment}${splitNumber[1] === undefined ? '' : `${f.decimal}${splitNumber[1]}`}${f.backPadding}`; | ||
return formatted.replace(/!/g, symbol).trim(); | ||
} | ||
// Use encode function to work out pattern | ||
var patternArray = (pattern !== null && pattern !== void 0 ? pattern : '').split(';'); | ||
var positiveFormat = encodePattern(patternArray[0]); | ||
const patternArray = (pattern ?? '').split(';'); | ||
const positiveFormat = encodePattern(patternArray[0]); | ||
positiveFormat.symbol = symbol; | ||
positiveFormat.decimal = decimal; | ||
positiveFormat.group = group; | ||
var negativeFormat = (patternArray[1] === undefined | ||
const negativeFormat = (patternArray[1] === undefined | ||
? encodePattern('-' + patternArray[0]) | ||
@@ -1167,8 +1161,8 @@ : encodePattern(patternArray[1])); | ||
negativeFormat.group = group; | ||
var zero = patternArray[2] === undefined ? format(0, positiveFormat) : patternArray[2]; | ||
return function (n) { | ||
const zero = patternArray[2] === undefined ? format(0, positiveFormat) : patternArray[2]; | ||
return (n) => { | ||
if (isNaN(n)) { | ||
return valueOnError; | ||
} | ||
var formattedNumber; | ||
let formattedNumber; | ||
n = Number(n); | ||
@@ -1189,16 +1183,13 @@ if (n > 0) { | ||
} | ||
exports.getFormatter = getFormatter; | ||
function format(n, p) { | ||
var formatterFunction = getFormatter(p); | ||
export function format(n, p) { | ||
const formatterFunction = getFormatter(p); | ||
return formatterFunction(n); | ||
} | ||
exports.format = format; | ||
function parse(str, p) { | ||
var decimal = exports.getFormatDetails(p).decimal; | ||
var mult = str.indexOf('-') >= 0 ? -1 : 1; | ||
export function parse(str, p) { | ||
const decimal = getFormatDetails(p).decimal; | ||
const mult = str.indexOf('-') >= 0 ? -1 : 1; | ||
return (Math.abs(Number(str | ||
.replace(new RegExp("[^0-9" + decimal + "]", 'g'), '') | ||
.replace(new RegExp(`[^0-9${decimal}]`, 'g'), '') | ||
.replace(decimal, '.'))) * mult); | ||
} | ||
exports.parse = parse; | ||
//# sourceMappingURL=currencyFormatter.js.map |
@@ -1,14 +0,2 @@ | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./currencyFormatter"), exports); | ||
export * from './currencyFormatter'; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "currencyformatter.ts", | ||
"version": "2.2.1", | ||
"version": "2.2.2", | ||
"description": "A super simple currency formatting library by OSREC Technologies https://osrec.co.uk", | ||
@@ -28,5 +28,4 @@ "directories": { | ||
"build": "tsc && npm run uglify", | ||
"uglify": "uglifyjs dist/currencyFormatter.js --compress --mangle --output dist/currencyFormatter.min.js", | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"prepublish": "npm build" | ||
"postinstall": "npm build" | ||
}, | ||
@@ -33,0 +32,0 @@ "devDependencies": { |
@@ -6,3 +6,3 @@ { | ||
"forceConsistentCasingInFileNames": true, | ||
"module": "commonjs", | ||
"module": "ES2020", | ||
"outDir": "dist", | ||
@@ -15,3 +15,3 @@ "noEmit": false, | ||
"isolatedModules": true, | ||
"target": "es5", | ||
"target": "es2020", | ||
"rootDir": "src" | ||
@@ -18,0 +18,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
124838
15
2440
2