format-money-js
Advanced tools
Comparing version 1.4.0 to 1.4.3
/*! | ||
* format-money-js v1.4.0 | ||
* format-money-js v1.4.3 | ||
* (c) 2020 Yurii Derevych | ||
@@ -14,4 +14,6 @@ * Released under the BSD-2-Clause License. | ||
} | ||
export interface FormatMoneyFragment { | ||
export interface FormatMoneyParse { | ||
source?: number; | ||
negative?: boolean; | ||
fullAmount?: string; | ||
amount?: string; | ||
@@ -26,4 +28,4 @@ decimals?: string; | ||
constructor(options?: FormatMoneyOptions); | ||
from: (number: number, options: FormatMoneyOptions, fragment?: boolean) => string | number | FormatMoneyFragment; | ||
from: (number: number, options: FormatMoneyOptions, parse?: boolean) => string | number | FormatMoneyParse; | ||
un: (value: (string | number), options: FormatMoneyOptions) => number; | ||
} |
"use strict"; | ||
/*! | ||
* format-money-js v1.4.0 | ||
* format-money-js v1.4.3 | ||
* (c) 2020 Yurii Derevych | ||
@@ -21,6 +21,6 @@ * Released under the BSD-2-Clause License. | ||
}; | ||
this.from = (number, options, fragment = false) => { | ||
this.from = (number, options, parse = false) => { | ||
const opt = Object.assign(Object.assign({}, this.options), options); | ||
if (typeof number === 'string') | ||
return number; | ||
if (typeof number !== 'number') | ||
return undefined; | ||
const neg = (number < 0) ? '-' : ''; | ||
@@ -56,5 +56,7 @@ let result; | ||
} | ||
if (fragment) { | ||
if (parse) { | ||
return { | ||
source: number, | ||
negative: (number < 0), | ||
fullAmount: x1 + x2, | ||
amount: x1, | ||
@@ -69,8 +71,9 @@ decimals: x2, | ||
const opt = Object.assign(Object.assign({}, this.options), options); | ||
const val = value || 0; | ||
if (typeof val === 'number') | ||
return val; | ||
if (typeof value === 'number') | ||
return value; | ||
if (typeof value !== 'string') | ||
return undefined; | ||
// 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((val) | ||
const unformatted = parseFloat((value) | ||
.replace(/\((?=\d+)(.*)\)/, '-$1') // replace bracketed values with negatives | ||
@@ -77,0 +80,0 @@ .replace(regex, '') // strip out any cruft |
@@ -1,1 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.FormatMoney=void 0;class FormatMoney{constructor(t){this.options=t,this.version="1.3.3",this.defaults={grouping:!0,separator:",",decimalPoint:".",decimals:0,symbol:"",append:!1},this.from=((t,e,s=!1)=>{const o=Object.assign(Object.assign({},this.options),e);if("string"==typeof t)return t;const i=t<0?"-":"";let n,a,r,l,c,p,m;if(n=Math.abs(t).toFixed(o.decimals),r=(a=(n+="").split("."))[0],l=a.length>1?o.decimalPoint+a[1]:"",o.grouping){c="";for(let t=0,e=r.length;t<e;t+=1)0!==t&&t%3==0&&(c=o.separator+c),c=r[e-t-1]+c;r=c}return p=m="",o.append?m=o.symbol:p=o.symbol,s?{negative:t<0,amount:r,decimals:l,symbol:o.symbol}:i+p+r+l+m}),this.un=((t,e)=>{const s=Object.assign(Object.assign({},this.options),e),o=t||0;if("number"==typeof o)return o;const i=new RegExp(`[^0-9-${s.decimalPoint}]`,"g"),n=parseFloat(o.replace(/\((?=\d+)(.*)\)/,"-$1").replace(i,"").replace(`${s.decimalPoint}`,"."));return isNaN(n)?0:n}),this.options=Object.assign(Object.assign({},this.defaults),t)}}exports.FormatMoney=FormatMoney; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.FormatMoney=void 0;class FormatMoney{constructor(t){this.options=t,this.version="1.3.3",this.defaults={grouping:!0,separator:",",decimalPoint:".",decimals:0,symbol:"",append:!1},this.from=((t,e,s=!1)=>{const o=Object.assign(Object.assign({},this.options),e);if("number"!=typeof t)return;const i=t<0?"-":"";let n,r,a,l,c,p,m;if(n=Math.abs(t).toFixed(o.decimals),a=(r=(n+="").split("."))[0],l=r.length>1?o.decimalPoint+r[1]:"",o.grouping){c="";for(let t=0,e=a.length;t<e;t+=1)0!==t&&t%3==0&&(c=o.separator+c),c=a[e-t-1]+c;a=c}return p=m="",o.append?m=o.symbol:p=o.symbol,s?{source:t,negative:t<0,fullAmount:a+l,amount:a,decimals:l,symbol:o.symbol}:i+p+a+l+m}),this.un=((t,e)=>{const s=Object.assign(Object.assign({},this.options),e);if("number"==typeof t)return t;if("string"!=typeof t)return;const o=new RegExp(`[^0-9-${s.decimalPoint}]`,"g"),i=parseFloat(t.replace(/\((?=\d+)(.*)\)/,"-$1").replace(o,"").replace(`${s.decimalPoint}`,"."));return isNaN(i)?0:i}),this.options=Object.assign(Object.assign({},this.defaults),t)}}exports.FormatMoney=FormatMoney; |
@@ -7,5 +7,5 @@ const { FormatMoney } = require('./dist/format-money.js'); | ||
console.log(fm1.from(12345.67, { symbol: '$' }), 'typeof: ' + typeof fm1.from(12345.67, { symbol: '$' })); // $12,345.67 | ||
console.log(fm1.un('€12,345.67'), 'typeof: ' + typeof fm1.un('€12,345.67')); // 12345.67 | ||
console.log(fm1.un('€12.345,67', { decimalPoint: ',' }), 'typeof: ' + typeof fm1.un('€12.345,67', { decimalPoint: ',' })); // 12345.67 | ||
console.log(fm1.from(12345.67, { symbol: '$' }), 'typeof: ' + typeof fm1.from(12345.67, { symbol: '$' })); | ||
console.log(fm1.un('€12,345.67'), 'typeof: ' + typeof fm1.un('€12,345.67')); | ||
console.log(fm1.un('€12.345,67', { decimalPoint: ',' }), 'typeof: ' + typeof fm1.un('€12.345,67', { decimalPoint: ',' })); | ||
@@ -31,9 +31,10 @@ // | ||
1234567890123456, | ||
12345678901234567, | ||
123456789012345678, | ||
1234567890123456789, | ||
'text', | ||
undefined, | ||
null, | ||
'', | ||
]; | ||
array0.forEach(element => { | ||
console.log(fm1.from(element), 'typeof: ' + typeof fm1.from(element)); | ||
console.log(element, ' -> ' , fm1.from(element), 'typeof: ' + typeof fm1.from(element)); | ||
}); | ||
@@ -44,3 +45,3 @@ | ||
const fm2 = new FormatMoney({ symbol: '$' }); | ||
console.log(fm2.from(12345.67, { symbol: '€' }), 'typeof: ' + typeof fm2.from(12345.67, { symbol: '€' })); // €12,345.67 | ||
console.log(fm2.from(12345.67, { symbol: '€' }), 'typeof: ' + typeof fm2.from(12345.67, { symbol: '€' })); | ||
@@ -50,3 +51,3 @@ // | ||
const fm3 = new FormatMoney({ symbol: '$' }); | ||
console.log(fm3.from(12345.67, { append: true }), 'typeof: ' + typeof fm3.from(12345.67, { append: true })); // 12,345.67$ | ||
console.log(fm3.from(12345.67, { append: true }), 'typeof: ' + typeof fm3.from(12345.67, { append: true })); | ||
@@ -56,8 +57,8 @@ // Fragment | ||
const fm4 = new FormatMoney({ symbol: '$' }); | ||
console.log(fm4.from(12345.67, { append: true }, true), 'typeof: ' + typeof fm4.from(12345.67, { append: true }, true)); // { negative: false, amount: '12,346', decimals: '', symbol: '$' } | ||
console.log(fm4.from(12345.67, { append: true }, true), 'typeof: ' + typeof fm4.from(12345.67, { append: true }, true)); | ||
const fm5 = new FormatMoney({ symbol: '$' }); | ||
console.log(fm5.from(-12345.67, { append: true }, true), 'typeof: ' + typeof fm5.from(-12345.67, { append: true }, true)); // { negative: true, amount: '12,346', decimals: '', symbol: '$' } | ||
console.log(fm5.from(-12345.67, { grouping: false, decimals: 2 }, true), 'typeof: ' + typeof fm5.from(-12345.67, { grouping: false, decimals: 2 }, true)); // { negative: true, amount: '12345', decimals: '.67', symbol: '$' } | ||
console.log(fm5.from(-12345.67, { append: true }, true), 'typeof: ' + typeof fm5.from(-12345.67, { append: true }, true)); | ||
console.log(fm5.from(-12345.67, { grouping: false, decimals: 2 }, true), 'typeof: ' + typeof fm5.from(-12345.67, { grouping: false, decimals: 2 }, true)); | ||
console.log(fm5.from('1234567.890', { grouping: false, decimals: 2 }, true), 'typeof: ' + typeof fm5.from('1234567.890', { grouping: true, decimals: 2 }, true)); | ||
// | ||
@@ -77,3 +78,8 @@ | ||
'12 345.67 USD', | ||
123, | ||
123.45, | ||
'text', | ||
undefined, | ||
null, | ||
'', | ||
]; | ||
@@ -90,3 +96,8 @@ | ||
'12 345,67 USD', | ||
123, | ||
123.45, | ||
'text', | ||
undefined, | ||
null, | ||
'', | ||
]; | ||
@@ -93,0 +104,0 @@ |
{ | ||
"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.4.0", | ||
"version": "1.4.3", | ||
"license": "BSD-2-Clause", | ||
@@ -6,0 +6,0 @@ "main": "./dist/format-money.js", |
@@ -40,5 +40,14 @@ # format-money-js | ||
{ symbol: '$' }, | ||
true // Fragment, return object | ||
true // Parse, return object | ||
) | ||
); // return object: { negative: false, amount: '12345', decimals: '.67', symbol: '$' } | ||
); | ||
/* return object: | ||
{ | ||
source: 12345.67, | ||
negative: false, | ||
fullAmount: '12,345.67', | ||
amount: '12,345', | ||
decimals: '.67', | ||
symbol: '$' | ||
}*/ | ||
``` | ||
@@ -45,0 +54,0 @@ |
/*! | ||
* format-money-js v1.4.0 | ||
* format-money-js v1.4.3 | ||
* (c) 2020 Yurii Derevych | ||
@@ -16,4 +16,6 @@ * Released under the BSD-2-Clause License. | ||
export interface FormatMoneyFragment { // (default) | ||
export interface FormatMoneyParse { // Parse | ||
source?: number; | ||
negative?: boolean; | ||
fullAmount?: string; | ||
amount?: string; | ||
@@ -45,3 +47,5 @@ decimals?: string; | ||
from = (number: number, options: FormatMoneyOptions, fragment: boolean = false): string | number | FormatMoneyFragment => { | ||
from = (number: number, | ||
options: FormatMoneyOptions, | ||
parse: boolean = false): string | number | FormatMoneyParse => { | ||
const opt = { | ||
@@ -52,3 +56,3 @@ ...this.options, | ||
if (typeof number === 'string') return number; | ||
if (typeof number !== 'number') return undefined; | ||
@@ -86,5 +90,7 @@ const neg = (number < 0) ? '-' : ''; | ||
} | ||
if (fragment) { | ||
if (parse) { | ||
return { | ||
source: number, | ||
negative: (number < 0), | ||
fullAmount: x1 + x2, | ||
amount: x1, | ||
@@ -104,10 +110,9 @@ decimals: x2, | ||
const val: (string | number) = value || 0; | ||
if (typeof value === 'number') return value; | ||
if (typeof value !== 'string') return undefined; | ||
if (typeof val === 'number') return val; | ||
// 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( | ||
(val) | ||
(value) | ||
.replace(/\((?=\d+)(.*)\)/, '-$1') // replace bracketed values with negatives | ||
@@ -114,0 +119,0 @@ .replace(regex, '') // strip out any cruft |
@@ -14,3 +14,4 @@ const test = require('tape') | ||
t.equal('1.000,20$', fm1.from(1000.2, {decimalPoint: ',', decimals: 2, grouping: true, separator: '.', symbol: '$', append: true}), 'should return 1.000,20$') | ||
t.equal('1.234.567.890.123.456,00$', fm1.from(1234567890123456, {decimalPoint: ',', decimals: 2, grouping: true, separator: '.', symbol: '$', append: true}), 'should return 1.234.567.890.123.456,00$') | ||
t.end() | ||
}) |
Sorry, the diff of this file is not supported yet
21151
375
79