format-money-js
Advanced tools
Comparing version 1.5.5 to 1.6.0
/*! | ||
* format-money-js v1.5.5 | ||
* (c) 2020-2022 Yurii Derevych | ||
* Sponsored by https://currencyrate.today/ | ||
* format-money-js v1.6.0 | ||
* (c) 2020-2023 Yurii Derevych | ||
* URL: https://github.com/dejurin/format-money-js | ||
* Sponsored: | ||
* https://cr.today/ | ||
* https://currencyrate.today/ | ||
* Released under the BSD-2-Clause License. | ||
@@ -29,4 +32,4 @@ */ | ||
constructor(options?: FormatMoneyOptions); | ||
from: (number: number, options?: FormatMoneyOptions | {}, parse?: boolean) => FormatMoneyParse | string | undefined; | ||
from: (value: number, options?: FormatMoneyOptions | {}, parse?: boolean) => FormatMoneyParse | string | undefined; | ||
un: (value: (string | number), options: FormatMoneyOptions) => number | undefined; | ||
} |
"use strict"; | ||
/*! | ||
* format-money-js v1.5.5 | ||
* (c) 2020-2022 Yurii Derevych | ||
* Sponsored by https://currencyrate.today/ | ||
* format-money-js v1.6.0 | ||
* (c) 2020-2023 Yurii Derevych | ||
* URL: https://github.com/dejurin/format-money-js | ||
* Sponsored: | ||
* https://cr.today/ | ||
* https://currencyrate.today/ | ||
* Released under the BSD-2-Clause License. | ||
@@ -13,3 +16,3 @@ */ | ||
this.options = options; | ||
this.version = '1.5.5'; | ||
this.version = '1.6.0'; | ||
this.defaults = { | ||
@@ -24,53 +27,59 @@ grouping: true, | ||
}; | ||
this.from = (number, options = {}, parse = false) => { | ||
const opt = Object.assign(Object.assign({}, this.options), options); | ||
if (typeof number !== 'number') | ||
// Format | ||
this.from = (value, options = {}, parse = false) => { | ||
// Merge custom options | ||
const customOptions = Object.assign(Object.assign({}, this.options), options); | ||
// If value not number return undefined | ||
if (typeof value !== 'number') | ||
return undefined; | ||
const neg = (number < 0) ? '-' : ''; | ||
// Set a sign for negative number | ||
let negativeSign = (value < 0) ? '-' : ''; | ||
let result; | ||
let x; | ||
let x1; | ||
let x2; | ||
let x3; | ||
let prefix; | ||
let suffix; | ||
result = Math.abs(number).toFixed(opt.decimals); | ||
if (!opt.leadZeros) { | ||
let left; | ||
let body; | ||
let prefix = ''; | ||
let suffix = ''; | ||
result = Math.abs(value).toFixed(customOptions.decimals); | ||
if (parseFloat(result) === 0 || result === '0') { | ||
negativeSign = ''; | ||
} | ||
if (!customOptions.leadZeros) { | ||
const resultFloat = parseFloat(result); | ||
result = resultFloat.toString(); | ||
} | ||
x = result.split('.'); | ||
x1 = x[0]; | ||
x2 = x.length > 1 ? opt.decimalPoint + x[1] : ''; | ||
if (opt.grouping) { | ||
x3 = ''; | ||
for (let i = 0, len = x1.length; i < len; i += 1) { | ||
const resultArr = result.split('.'); | ||
[left] = resultArr; | ||
const right = resultArr.length > 1 ? customOptions.decimalPoint + resultArr[1] : ''; | ||
if (customOptions.grouping) { | ||
body = ''; | ||
for (let i = 0, len = left.length; i < len; i += 1) { | ||
if (i !== 0 && (i % 3) === 0) { | ||
x3 = opt.separator + x3; | ||
body = customOptions.separator + body; | ||
} | ||
x3 = x1[len - i - 1] + x3; | ||
body = left[len - i - 1] + body; | ||
} | ||
x1 = x3; | ||
left = body; | ||
} | ||
prefix = suffix = ''; | ||
if (opt.append) { | ||
suffix = opt.symbol; | ||
if (customOptions.append) { | ||
suffix = customOptions.symbol; | ||
} | ||
else { | ||
prefix = opt.symbol; | ||
prefix = customOptions.symbol; | ||
} | ||
if (parse) { | ||
return { | ||
source: number, | ||
negative: (number < 0), | ||
fullAmount: x1 + x2, | ||
amount: x1, | ||
decimals: x2, | ||
symbol: opt.symbol, | ||
source: value, | ||
negative: (value < 0), | ||
fullAmount: left + right, | ||
amount: left, | ||
decimals: right, | ||
symbol: customOptions.symbol, | ||
}; | ||
} | ||
return neg + prefix + x1 + x2 + suffix; | ||
return negativeSign + prefix + left + right + suffix; | ||
}; | ||
// Unformat | ||
this.un = (value, options) => { | ||
const opt = Object.assign(Object.assign({}, this.options), options); | ||
// Merge custom options | ||
const customOptions = Object.assign(Object.assign({}, this.options), options); | ||
if (typeof value === 'number') | ||
@@ -81,9 +90,10 @@ return value; | ||
// Build regex to strip out everything except digits, decimal point and minus sign: | ||
const regex = new RegExp(`[^0-9-${opt.decimalPoint}]`, 'g'); | ||
const unformatted = parseFloat((value) | ||
const regex = new RegExp(`[^0-9-${customOptions.decimalPoint}]`, 'g'); | ||
const unFormatted = parseFloat((value) | ||
.replace(/\((?=\d+)(.*)\)/, '-$1') // replace bracketed values with negatives | ||
.replace(regex, '') // strip out any cruft | ||
.replace(`${opt.decimalPoint}`, '.')); | ||
return !isNaN(unformatted) ? unformatted : 0; | ||
.replace(`${customOptions.decimalPoint}`, '.')); | ||
return !Number.isNaN(unFormatted) ? unFormatted : 0; | ||
}; | ||
// Merge options | ||
this.options = Object.assign(Object.assign({}, this.defaults), options); | ||
@@ -90,0 +100,0 @@ } |
@@ -1,2 +0,2 @@ | ||
Copyright (c) 2020-2021 Yurii Derevych | ||
Copyright (c) 2020-2023 Yurii Derevych | ||
@@ -3,0 +3,0 @@ All rights reserved. |
{ | ||
"name": "format-money-js", | ||
"description": "Zero dependency tiny JavaScript library (1kB bytes) by CurrencyRate.today, providing simple way and advanced number, money and currency formatting and removes all formatting/cruft and returns the raw float value.", | ||
"version": "1.5.5", | ||
"version": "1.6.0", | ||
"license": "BSD-2-Clause", | ||
@@ -12,4 +12,4 @@ "main": "./dist/format-money.js", | ||
"scripts": { | ||
"build": "npm run clean && tsc && gulp", | ||
"clean": "gulp clean", | ||
"build": "tsc --build", | ||
"clean": "tsc --build --clean", | ||
"lint": "tslint --project tsconfig.json" | ||
@@ -53,5 +53,2 @@ }, | ||
"eslint-plugin-react-hooks": "^4.2.0", | ||
"gulp": "^4.0.2", | ||
"gulp-concat": "^2.6.1", | ||
"gulp-uglify": "^3.0.2", | ||
"jest-diff": "^25.1.0", | ||
@@ -64,3 +61,4 @@ "tslint-config-airbnb": "^5.11.2", | ||
"@typescript-eslint/eslint-plugin": "^4.6.0", | ||
"tslint": "^6.1.3" | ||
"tslint": "^5.11.0", | ||
"tape": "^5.1.1" | ||
}, | ||
@@ -71,6 +69,3 @@ "repository": { | ||
}, | ||
"types": "./dist/format-money.d.ts", | ||
"dependencies": { | ||
"tape": "^5.1.1" | ||
} | ||
"types": "./dist/format-money.d.ts" | ||
} |
@@ -7,3 +7,4 @@ # format-money-js | ||
**Zero dependency** tiny JavaScript library (1kB bytes) by CurrencyRate.today, providing simple way and advanced number, money and currency formatting and removes all formatting/cruft and returns the raw float value. | ||
**Zero dependency** tiny JavaScript library (841 bytes) by CurrencyRate.today, providing simple way and advanced number, money and currency formatting and removes all formatting/cruft and returns the raw float value. | ||
Size of library: 841 bytes in zip and 1.2 kByte in min formats. | ||
@@ -10,0 +11,0 @@ |
/*! | ||
* format-money-js v1.5.5 | ||
* (c) 2020-2022 Yurii Derevych | ||
* Sponsored by https://currencyrate.today/ | ||
* format-money-js v1.6.0 | ||
* (c) 2020-2023 Yurii Derevych | ||
* URL: https://github.com/dejurin/format-money-js | ||
* Sponsored: | ||
* https://cr.today/ | ||
* https://currencyrate.today/ | ||
* Released under the BSD-2-Clause License. | ||
@@ -28,100 +31,112 @@ */ | ||
export class FormatMoney { | ||
version = '1.6.0'; | ||
version = '1.5.5'; | ||
private defaults: FormatMoneyOptions = { | ||
grouping: true, | ||
separator: ',', | ||
decimalPoint: '.', | ||
decimals: 0, | ||
symbol: '', | ||
append: false, | ||
leadZeros: true, | ||
private defaults: FormatMoneyOptions = { | ||
grouping: true, | ||
separator: ',', | ||
decimalPoint: '.', | ||
decimals: 0, | ||
symbol: '', | ||
append: false, | ||
leadZeros: true, | ||
}; | ||
constructor( | ||
private options?: FormatMoneyOptions, | ||
) { | ||
// Merge options | ||
this.options = { | ||
...this.defaults, | ||
...options, | ||
}; | ||
} | ||
constructor( | ||
private options?: FormatMoneyOptions, | ||
) { | ||
this.options = { | ||
...this.defaults, | ||
...options, | ||
}; | ||
} | ||
// Format | ||
from = ( | ||
value: number, | ||
options: FormatMoneyOptions | {} = {}, | ||
parse: boolean = false, | ||
): FormatMoneyParse | string | undefined => { | ||
// Merge custom options | ||
const customOptions = { | ||
...this.options, | ||
...options, | ||
}; | ||
from = (number: number, | ||
options: FormatMoneyOptions | {} = {}, | ||
parse: boolean = false): FormatMoneyParse | string | undefined => { | ||
const opt = { | ||
...this.options, | ||
...options, | ||
}; | ||
// If value not number return undefined | ||
if (typeof value !== 'number') return undefined; | ||
if (typeof number !== 'number') return undefined; | ||
// Set a sign for negative number | ||
let negativeSign: string = (value < 0) ? '-' : ''; | ||
let result: string; | ||
let left: string; | ||
let body: string; | ||
let prefix: string = ''; | ||
let suffix: string = ''; | ||
const neg = (number < 0) ? '-' : ''; | ||
result = Math.abs(value).toFixed(customOptions.decimals); | ||
if (parseFloat(result) === 0 || result === '0') { | ||
negativeSign = ''; | ||
} | ||
let result: string; | ||
let x: string[]; | ||
let x1: string; | ||
let x2: string; | ||
let x3: string; | ||
let prefix: string | undefined; | ||
let suffix: string | undefined; | ||
if (!customOptions.leadZeros) { | ||
const resultFloat = parseFloat(result); | ||
result = resultFloat.toString(); | ||
} | ||
result = Math.abs(number).toFixed(opt.decimals); | ||
if (!opt.leadZeros) { | ||
const resultFloat = parseFloat(result); | ||
result = resultFloat.toString(); | ||
} | ||
x = result.split('.'); | ||
x1 = x[0]; | ||
x2 = x.length > 1 ? opt.decimalPoint + x[1] : ''; | ||
if (opt.grouping) { | ||
x3 = ''; | ||
for (let i = 0, len = x1.length; i < len; i += 1) { | ||
if (i !== 0 && (i % 3) === 0) { | ||
x3 = opt.separator + x3; | ||
} | ||
x3 = x1[len - i - 1] + x3; | ||
const resultArr: string[] = result.split('.'); | ||
[left] = resultArr; | ||
const right = resultArr.length > 1 ? customOptions.decimalPoint + resultArr[1] : ''; | ||
if (customOptions.grouping) { | ||
body = ''; | ||
for (let i = 0, len = left.length; i < len; i += 1) { | ||
if (i !== 0 && (i % 3) === 0) { | ||
body = customOptions.separator + body; | ||
} | ||
x1 = x3; | ||
body = left[len - i - 1] + body; | ||
} | ||
prefix = suffix = ''; | ||
if (opt.append) { | ||
suffix = opt.symbol; | ||
} else { | ||
prefix = opt.symbol; | ||
} | ||
if (parse) { | ||
return { | ||
source: number, | ||
negative: (number < 0), | ||
fullAmount: x1 + x2, | ||
amount: x1, | ||
decimals: x2, | ||
symbol: opt.symbol, | ||
}; | ||
} | ||
return neg + prefix + x1 + x2 + suffix; | ||
left = body; | ||
} | ||
un = (value: (string | number), options: FormatMoneyOptions): number | undefined => { | ||
const opt = { | ||
...this.options, | ||
...options, | ||
if (customOptions.append) { | ||
suffix = customOptions.symbol as string; | ||
} else { | ||
prefix = customOptions.symbol as string; | ||
} | ||
if (parse) { | ||
return { | ||
source: value, | ||
negative: (value < 0), | ||
fullAmount: left + right, | ||
amount: left, | ||
decimals: right, | ||
symbol: customOptions.symbol as string, | ||
}; | ||
} | ||
return negativeSign + prefix + left + right + suffix; | ||
}; | ||
if (typeof value === 'number') return value; | ||
if (typeof value !== 'string') return undefined; | ||
// Unformat | ||
un = (value: (string | number), options: FormatMoneyOptions): number | undefined => { | ||
// Merge custom options | ||
const customOptions = { | ||
...this.options, | ||
...options, | ||
}; | ||
// Build regex to strip out everything except digits, decimal point and minus sign: | ||
const regex: RegExp = new RegExp(`[^0-9-${opt.decimalPoint}]`, 'g'); | ||
const unformatted = parseFloat( | ||
(value) | ||
.replace(/\((?=\d+)(.*)\)/, '-$1') // replace bracketed values with negatives | ||
.replace(regex, '') // strip out any cruft | ||
.replace(`${opt.decimalPoint}`, '.'), // make sure decimal point is standard | ||
); | ||
if (typeof value === 'number') return value; | ||
if (typeof value !== 'string') return undefined; | ||
return !isNaN(unformatted) ? unformatted : 0; | ||
} | ||
// Build regex to strip out everything except digits, decimal point and minus sign: | ||
const regex: RegExp = new RegExp(`[^0-9-${customOptions.decimalPoint}]`, 'g'); | ||
const unFormatted = parseFloat( | ||
(value) | ||
.replace(/\((?=\d+)(.*)\)/, '-$1') // replace bracketed values with negatives | ||
.replace(regex, '') // strip out any cruft | ||
.replace(`${customOptions.decimalPoint}`, '.'), // make sure decimal point is standard | ||
); | ||
return !Number.isNaN(unFormatted) ? unFormatted : 0; | ||
}; | ||
} |
Sorry, the diff of this file is not supported yet
26942
0
26
19
456
85
- Removedtape@^5.1.1
- Removed@ljharb/resumer@0.1.3(transitive)
- Removed@ljharb/through@2.3.13(transitive)
- Removedarray-buffer-byte-length@1.0.1(transitive)
- Removedarray.prototype.every@1.1.6(transitive)
- Removedarraybuffer.prototype.slice@1.0.3(transitive)
- Removedavailable-typed-arrays@1.0.7(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedcall-bind@1.0.7(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removeddata-view-buffer@1.0.1(transitive)
- Removeddata-view-byte-length@1.0.1(transitive)
- Removeddata-view-byte-offset@1.0.0(transitive)
- Removeddeep-equal@2.2.3(transitive)
- Removeddefine-data-property@1.1.4(transitive)
- Removeddefine-properties@1.2.1(transitive)
- Removeddefined@1.0.1(transitive)
- Removeddotignore@0.1.2(transitive)
- Removedes-abstract@1.23.3(transitive)
- Removedes-define-property@1.0.0(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedes-get-iterator@1.1.3(transitive)
- Removedes-object-atoms@1.0.0(transitive)
- Removedes-set-tostringtag@2.0.3(transitive)
- Removedes-to-primitive@1.2.1(transitive)
- Removedfor-each@0.3.3(transitive)
- Removedfs.realpath@1.0.0(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedfunction.prototype.name@1.1.6(transitive)
- Removedfunctions-have-names@1.2.3(transitive)
- Removedget-intrinsic@1.2.4(transitive)
- Removedget-package-type@0.1.0(transitive)
- Removedget-symbol-description@1.0.2(transitive)
- Removedglob@7.2.3(transitive)
- Removedglobalthis@1.0.4(transitive)
- Removedgopd@1.0.1(transitive)
- Removedhas-bigints@1.0.2(transitive)
- Removedhas-dynamic-import@2.1.0(transitive)
- Removedhas-property-descriptors@1.0.2(transitive)
- Removedhas-proto@1.0.3(transitive)
- Removedhas-symbols@1.0.3(transitive)
- Removedhas-tostringtag@1.0.2(transitive)
- Removedhasown@2.0.2(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinherits@2.0.4(transitive)
- Removedinternal-slot@1.0.7(transitive)
- Removedis-arguments@1.1.1(transitive)
- Removedis-array-buffer@3.0.4(transitive)
- Removedis-bigint@1.0.4(transitive)
- Removedis-boolean-object@1.1.2(transitive)
- Removedis-callable@1.2.7(transitive)
- Removedis-core-module@2.13.1(transitive)
- Removedis-data-view@1.0.1(transitive)
- Removedis-date-object@1.0.5(transitive)
- Removedis-map@2.0.3(transitive)
- Removedis-negative-zero@2.0.3(transitive)
- Removedis-number-object@1.0.7(transitive)
- Removedis-regex@1.1.4(transitive)
- Removedis-set@2.0.3(transitive)
- Removedis-shared-array-buffer@1.0.3(transitive)
- Removedis-string@1.0.7(transitive)
- Removedis-symbol@1.0.4(transitive)
- Removedis-typed-array@1.1.13(transitive)
- Removedis-weakmap@2.0.2(transitive)
- Removedis-weakref@1.0.2(transitive)
- Removedis-weakset@2.0.3(transitive)
- Removedisarray@2.0.5(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmock-property@1.0.3(transitive)
- Removedobject-inspect@1.13.1(transitive)
- Removedobject-is@1.1.6(transitive)
- Removedobject-keys@1.1.1(transitive)
- Removedobject.assign@4.1.5(transitive)
- Removedonce@1.4.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedpath-parse@1.0.7(transitive)
- Removedpossible-typed-array-names@1.0.0(transitive)
- Removedregexp.prototype.flags@1.5.2(transitive)
- Removedresolve@2.0.0-next.5(transitive)
- Removedsafe-array-concat@1.1.2(transitive)
- Removedsafe-regex-test@1.0.3(transitive)
- Removedset-function-length@1.2.2(transitive)
- Removedset-function-name@2.0.2(transitive)
- Removedside-channel@1.0.6(transitive)
- Removedstop-iteration-iterator@1.0.0(transitive)
- Removedstring.prototype.trim@1.2.9(transitive)
- Removedstring.prototype.trimend@1.0.8(transitive)
- Removedstring.prototype.trimstart@1.0.8(transitive)
- Removedsupports-preserve-symlinks-flag@1.0.0(transitive)
- Removedtape@5.7.5(transitive)
- Removedtyped-array-buffer@1.0.2(transitive)
- Removedtyped-array-byte-length@1.0.1(transitive)
- Removedtyped-array-byte-offset@1.0.2(transitive)
- Removedtyped-array-length@1.0.6(transitive)
- Removedunbox-primitive@1.0.2(transitive)
- Removedwhich-boxed-primitive@1.0.2(transitive)
- Removedwhich-collection@1.0.2(transitive)
- Removedwhich-typed-array@1.1.15(transitive)
- Removedwrappy@1.0.2(transitive)