currencyformatter.ts
Advanced tools
Comparing version 2.2.2 to 2.2.3
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
// CurrencyFormatter.js | ||
@@ -9,3 +10,5 @@ // --------------------------------------------------------------------- | ||
// If you use this library in a commercial project, we appreciate a link back to https://osrec.co.uk :) | ||
export const symbols = { | ||
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 = { | ||
AED: 'د.إ.', | ||
@@ -168,3 +171,3 @@ AFN: '؋', | ||
}; | ||
export const defaultLocales = { | ||
exports.defaultLocales = { | ||
AED: 'ar_AE', | ||
@@ -329,3 +332,3 @@ AFN: 'fa_AF', | ||
}; | ||
export const locales = { | ||
exports.locales = { | ||
af: { p: '!#,##0.00', g: ' ', d: ',' }, | ||
@@ -1047,24 +1050,26 @@ af_NA: { h: 'af' }, | ||
}; | ||
export const getFormatDetails = function (p = {}) { | ||
exports.getFormatDetails = function (p) { | ||
// Perform checks on inputs and set up defaults as needed (defaults to en, USD) | ||
const currency = p.currency || 'USD'; | ||
let locale = locales[p.locale || defaultLocales[currency]]; | ||
var _a, _b; | ||
if (p === void 0) { p = {}; } | ||
var currency = p.currency || 'USD'; | ||
var locale = exports.locales[p.locale || exports.defaultLocales[currency]]; | ||
if (typeof locale.h !== 'undefined') { | ||
locale = locales[locale.h]; | ||
locale = exports.locales[locale.h]; | ||
} // Locale inheritance | ||
const symbol = typeof p.symbol === 'undefined' | ||
? symbols[currency] || currency | ||
var symbol = typeof p.symbol === 'undefined' | ||
? exports.symbols[currency] || currency | ||
: p.symbol === null | ||
? '' | ||
: p.symbol; | ||
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, | ||
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, | ||
postFormatFunction: p.postFormatFunction, | ||
@@ -1074,28 +1079,29 @@ }; | ||
}; | ||
export const toFixed = function (n, precision) { | ||
exports.toFixed = function (n, precision) { | ||
return (Math.round(Number(n) * Math.pow(10, precision)) / Math.pow(10, precision)).toFixed(precision); | ||
}; | ||
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; | ||
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; | ||
// encodePattern Function - returns a few simple characteristics of the pattern provided | ||
function encodePattern(pattern) { | ||
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, | ||
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, | ||
frontPadding: paddingSplit[0], | ||
@@ -1109,4 +1115,4 @@ backPadding: paddingSplit[1], | ||
// Zero Padding helper function | ||
const pad = function (input, width) { | ||
const n = `${input}`; | ||
var pad = function (input, width) { | ||
var n = "" + input; | ||
return n.length >= width | ||
@@ -1118,8 +1124,8 @@ ? n | ||
function format(n, f) { | ||
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; | ||
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; | ||
if (maxGroupIndex > 0) { | ||
@@ -1130,5 +1136,5 @@ while (cursor > 0) { | ||
} // Always reset to the last group length (useful for big numbers) | ||
const currentGroupLength = f.groupLengths[groupIndex]; | ||
const start = cursor - currentGroupLength; | ||
segment = `${splitNumber[0].substring(start, cursor)}${f.group}${segment}`; | ||
var currentGroupLength = f.groupLengths[groupIndex]; | ||
var start = cursor - currentGroupLength; | ||
segment = "" + splitNumber[0].substring(start, cursor) + f.group + segment; | ||
cursor -= currentGroupLength; | ||
@@ -1145,12 +1151,12 @@ --groupIndex; | ||
} | ||
const formatted = `${f.frontPadding}${segment}${splitNumber[1] === undefined ? '' : `${f.decimal}${splitNumber[1]}`}${f.backPadding}`; | ||
var 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 | ||
const patternArray = (pattern ?? '').split(';'); | ||
const positiveFormat = encodePattern(patternArray[0]); | ||
var patternArray = (pattern !== null && pattern !== void 0 ? pattern : '').split(';'); | ||
var positiveFormat = encodePattern(patternArray[0]); | ||
positiveFormat.symbol = symbol; | ||
positiveFormat.decimal = decimal; | ||
positiveFormat.group = group; | ||
const negativeFormat = (patternArray[1] === undefined | ||
var negativeFormat = (patternArray[1] === undefined | ||
? encodePattern('-' + patternArray[0]) | ||
@@ -1161,8 +1167,8 @@ : encodePattern(patternArray[1])); | ||
negativeFormat.group = group; | ||
const zero = patternArray[2] === undefined ? format(0, positiveFormat) : patternArray[2]; | ||
return (n) => { | ||
var zero = patternArray[2] === undefined ? format(0, positiveFormat) : patternArray[2]; | ||
return function (n) { | ||
if (isNaN(n)) { | ||
return valueOnError; | ||
} | ||
let formattedNumber; | ||
var formattedNumber; | ||
n = Number(n); | ||
@@ -1183,13 +1189,16 @@ if (n > 0) { | ||
} | ||
export function format(n, p) { | ||
const formatterFunction = getFormatter(p); | ||
exports.getFormatter = getFormatter; | ||
function format(n, p) { | ||
var formatterFunction = getFormatter(p); | ||
return formatterFunction(n); | ||
} | ||
export function parse(str, p) { | ||
const decimal = getFormatDetails(p).decimal; | ||
const mult = str.indexOf('-') >= 0 ? -1 : 1; | ||
exports.format = format; | ||
function parse(str, p) { | ||
var decimal = exports.getFormatDetails(p).decimal; | ||
var 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,2 +0,14 @@ | ||
export * from './currencyFormatter'; | ||
"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); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "currencyformatter.ts", | ||
"version": "2.2.2", | ||
"version": "2.2.3", | ||
"description": "A super simple currency formatting library by OSREC Technologies https://osrec.co.uk", | ||
@@ -5,0 +5,0 @@ "directories": { |
@@ -6,3 +6,3 @@ { | ||
"forceConsistentCasingInFileNames": true, | ||
"module": "ES2020", | ||
"module": "commonjs", | ||
"outDir": "dist", | ||
@@ -15,7 +15,7 @@ "noEmit": false, | ||
"isolatedModules": true, | ||
"target": "es2020", | ||
"target": "es5", | ||
"rootDir": "src" | ||
}, | ||
"exclude": ["node_modules"], | ||
"include": ["src", "index.ts", "index.d.ts"] | ||
"include": ["src", "index.ts"] | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
126096
2461