Socket
Socket
Sign inDemoInstall

format-money-js

Package Overview
Dependencies
0
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.5.5 to 1.6.0

dist/format-money.min.js.zip

11

dist/format-money.d.ts
/*!
* 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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc