format-money-js
Advanced tools
Comparing version 1.4.4 to 1.5.0
/*! | ||
* format-money-js v1.4.4 | ||
* format-money-js v1.5.0 | ||
* (c) 2020-2022 Yurii Derevych | ||
@@ -14,2 +14,3 @@ * Sponsored by https://currencyrate.today/ | ||
append?: boolean; | ||
leadZeros: boolean; | ||
} | ||
@@ -29,4 +30,4 @@ export interface FormatMoneyParse { | ||
constructor(options?: FormatMoneyOptions); | ||
from: (number: number, options: FormatMoneyOptions, parse?: boolean) => string | number | FormatMoneyParse; | ||
un: (value: (string | number), options: FormatMoneyOptions) => number; | ||
from: (number: number, options: FormatMoneyOptions, parse?: boolean) => string | number | FormatMoneyParse | undefined; | ||
un: (value: (string | number), options: FormatMoneyOptions) => number | undefined; | ||
} |
"use strict"; | ||
/*! | ||
* format-money-js v1.4.4 | ||
* format-money-js v1.5.0 | ||
* (c) 2020-2022 Yurii Derevych | ||
@@ -13,3 +13,3 @@ * Sponsored by https://currencyrate.today/ | ||
this.options = options; | ||
this.version = '1.4.4'; | ||
this.version = '1.5.0'; | ||
this.defaults = { | ||
@@ -22,2 +22,3 @@ grouping: true, | ||
append: false, | ||
leadZeros: true, | ||
}; | ||
@@ -37,3 +38,6 @@ this.from = (number, options, parse = false) => { | ||
result = Math.abs(number).toFixed(opt.decimals); | ||
result += ''; | ||
if (opt.leadZeros === false) { | ||
const resultFloat = parseFloat(result); | ||
result = resultFloat.toString(); | ||
} | ||
x = result.split('.'); | ||
@@ -40,0 +44,0 @@ x1 = x[0]; |
@@ -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.4.4",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; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.FormatMoney=void 0;class FormatMoney{constructor(e){this.options=e,this.version="1.5.0",this.defaults={grouping:!0,separator:",",decimalPoint:".",decimals:0,symbol:"",append:!1,leadZeros:!0},this.from=((e,t,s=!1)=>{const o=Object.assign(Object.assign({},this.options),t);if("number"!=typeof e)return;const i=e<0?"-":"";let n,r,a,l,c,p,m;if(n=Math.abs(e).toFixed(o.decimals),!1===o.leadZeros){n=parseFloat(n).toString()}if(a=(r=n.split("."))[0],l=r.length>1?o.decimalPoint+r[1]:"",o.grouping){c="";for(let e=0,t=a.length;e<t;e+=1)0!==e&&e%3==0&&(c=o.separator+c),c=a[t-e-1]+c;a=c}return p=m="",o.append?m=o.symbol:p=o.symbol,s?{source:e,negative:e<0,fullAmount:a+l,amount:a,decimals:l,symbol:o.symbol}:i+p+a+l+m}),this.un=((e,t)=>{const s=Object.assign(Object.assign({},this.options),t);if("number"==typeof e)return e;if("string"!=typeof e)return;const o=new RegExp(`[^0-9-${s.decimalPoint}]`,"g"),i=parseFloat(e.replace(/\((?=\d+)(.*)\)/,"-$1").replace(o,"").replace(`${s.decimalPoint}`,"."));return isNaN(i)?0:i}),this.options=Object.assign(Object.assign({},this.defaults),e)}}exports.FormatMoney=FormatMoney; |
{ | ||
"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.4", | ||
"version": "1.5.0", | ||
"license": "BSD-2-Clause", | ||
@@ -6,0 +6,0 @@ "main": "./dist/format-money.js", |
@@ -66,9 +66,10 @@ # format-money-js | ||
| Name | Default | Type | Example | ||
|---------------|----------|---------|------------------------------| | ||
| grouping | true | Boolean | 1,000 (true) vs 1000 (false) | | ||
| separator | , | String | 1,000 | | ||
| decimalPoint | . | String | 1,234.56 | | ||
| decimals | 0 | Number | 1,234 (0 - without decimals) | | ||
| symbol | None | String | $1,234.56 (if append false) | | ||
| append | false | String | 1,234.56$ (if append true) | | ||
|---------------|----------|---------|------------------------------------------| | ||
| grouping | true | Boolean | 1,000 (true) vs 1000 (false) | | ||
| separator | , | String | 1,000 | | ||
| decimalPoint | . | String | 1,234.56 | | ||
| decimals | 0 | Number | 1,234 (0 - without decimals) | | ||
| symbol | None | String | $1,234.56 (if append false) | | ||
| append | false | String | 1,234.56$ (if append true) | | ||
| leadZeros | true | Boolean | true - 1.10, false - 1.1 (leading zeros) | | ||
@@ -75,0 +76,0 @@ ## Source |
/*! | ||
* format-money-js v1.4.4 | ||
* format-money-js v1.5.0 | ||
* (c) 2020-2022 Yurii Derevych | ||
@@ -15,2 +15,3 @@ * Sponsored by https://currencyrate.today/ | ||
append?: boolean; | ||
leadZeros: boolean; | ||
} | ||
@@ -29,3 +30,3 @@ | ||
version = '1.4.4'; | ||
version = '1.5.0'; | ||
private defaults: FormatMoneyOptions = { | ||
@@ -38,2 +39,3 @@ grouping: true, | ||
append: false, | ||
leadZeros: true, | ||
}; | ||
@@ -52,3 +54,3 @@ | ||
options: FormatMoneyOptions, | ||
parse: boolean = false): string | number | FormatMoneyParse => { | ||
parse: boolean = false): string | number | FormatMoneyParse | undefined => { | ||
const opt = { | ||
@@ -72,3 +74,6 @@ ...this.options, | ||
result = Math.abs(number).toFixed(opt.decimals); | ||
result += ''; | ||
if (opt.leadZeros === false) { | ||
const resultFloat = parseFloat(result) | ||
result = resultFloat.toString() | ||
} | ||
x = result.split('.'); | ||
@@ -106,3 +111,3 @@ x1 = x[0]; | ||
un = (value: (string | number), options: FormatMoneyOptions): number => { | ||
un = (value: (string | number), options: FormatMoneyOptions): number| undefined => { | ||
const opt = { | ||
@@ -109,0 +114,0 @@ ...this.options, |
10
test.js
@@ -15,3 +15,13 @@ const test = require('tape') | ||
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.equal('1.1', fm1.from(1.1, {decimalPoint: '.', decimals: 2, leadZeros: false}), 'should return 1.1') | ||
t.equal('1.2', fm1.from(1.20, {decimalPoint: '.', decimals: 2, leadZeros: false}), 'should return 1.2') | ||
t.equal('1.3', fm1.from(1.300, {decimalPoint: '.', decimals: 2, leadZeros: false}), 'should return 1.3') | ||
t.equal('1.4', fm1.from(1.401, {decimalPoint: '.', decimals: 2, leadZeros: false}), 'should return 1.4') | ||
t.equal('1.501', fm1.from(1.501, {decimalPoint: '.', decimals: 3, leadZeros: false}), 'should return 1.501') | ||
t.equal('1.60', fm1.from(1.6, {decimalPoint: '.', decimals: 2, leadZeros: true}), 'should return 1.60') | ||
t.equal('1.70', fm1.from(1.70, {decimalPoint: '.', decimals: 2, leadZeros: true}), 'should return 1.70') | ||
t.equal('1.80', fm1.from(1.800, {decimalPoint: '.', decimals: 2, leadZeros: true}), 'should return 1.80') | ||
t.equal('1.90', fm1.from(1.901, {decimalPoint: '.', decimals: 2, leadZeros: true}), 'should return 1.90') | ||
t.equal('1.901', fm1.from(1.901, {decimalPoint: '.', decimals: 3, leadZeros: true}), 'should return 1.901') | ||
t.end() | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23401
403
84