Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

to-words

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

to-words - npm Package Compare versions

Comparing version 2.2.3 to 2.2.4

__tests__/fa-IR.test.ts

3

.prettierrc.js
module.exports = {
trailingComma: 'all',
singleQuote: true,
};
endOfLine: 'auto',
};

@@ -5,2 +5,4 @@ # Changelog

### [2.2.4](https://github.com/mastermunj/to-words/compare/v2.2.3...v2.2.4) (2020-11-08)
### [2.2.3](https://github.com/mastermunj/to-words/compare/v2.2.2...v2.2.3) (2020-09-25)

@@ -7,0 +9,0 @@

@@ -12,2 +12,7 @@ export interface LocaleInterface {

};
options?: {
namedLessThan1000?: boolean;
splitWord?: string;
ignoreZeroInDecimals?: boolean;
};
texts: {

@@ -23,2 +28,5 @@ and: string;

}[];
decimalLengthWordMapping?: {
[decimalLength: number]: string;
};
}

@@ -19,14 +19,20 @@ "use strict";

getLocaleClass() {
// eslint-disable-next-line @typescript-eslint/no-var-requires
return require(`./locales/${this.options.localeCode}`).Locale;
/* eslint-disable @typescript-eslint/no-var-requires */
switch (this.options.localeCode) {
case 'en-IN':
return require('./locales/en-IN').Locale;
case 'en-MU':
return require('./locales/en-MU').Locale;
case 'en-US':
return require('./locales/en-US').Locale;
case 'fa-IR':
return require('./locales/fa-IR').Locale;
}
/* eslint-enable @typescript-eslint/no-var-requires */
throw new Error(`Unknown Locale "${this.options.localeCode}"`);
}
getLocale() {
if (this.locale === undefined) {
try {
const LocaleClass = this.getLocaleClass();
this.locale = new LocaleClass();
}
catch (e) {
throw new Error(`Unknown Locale "${this.options.localeCode}"`);
}
const LocaleClass = this.getLocaleClass();
this.locale = new LocaleClass();
}

@@ -36,2 +42,3 @@ return this.locale;

convert(number, options = {}) {
var _a, _b, _c, _d;
options = Object.assign({}, this.options.converterOptions, options);

@@ -55,6 +62,8 @@ if (!this.isValidNumber(number)) {

isFloat = this.isFloat(number);
const split = number.toString().split('.');
let words = `${this.convertInternal(Number(split[0]), options)}${locale.currency.plural ? ` ${locale.currency.plural}` : ''}`;
const isNumberZero = number >= 0 && number < 1;
const split = number.toString().split('.');
let words = `${this.convertInternal(Number(split[0]), options)} ${locale.currency.plural}`;
if (isNumberZero && options.ignoreZeroCurrency) {
const ignoreZero = options.ignoreZeroCurrency ||
(((_a = locale.options) === null || _a === void 0 ? void 0 : _a.ignoreZeroInDecimals) && number !== 0);
if (isNumberZero && ignoreZero) {
words = '';

@@ -64,7 +73,14 @@ }

if (isFloat) {
if (!isNumberZero || !options.ignoreZeroCurrency) {
if (!isNumberZero || !ignoreZero) {
wordsWithDecimal += ` ${locale.texts.and} `;
}
wordsWithDecimal += `${this.convertInternal(Number(split[1]) * Math.pow(10, 2 - split[1].length), options)} ${locale.currency.fractionalUnit.plural}`;
const decimalLengthWord = (_b = locale === null || locale === void 0 ? void 0 : locale.decimalLengthWordMapping) === null || _b === void 0 ? void 0 : _b[split[1].length];
wordsWithDecimal += `${this.convertInternal(Number(split[1]) *
(!locale.decimalLengthWordMapping
? Math.pow(10, 2 - split[1].length)
: 1), options)}${decimalLengthWord ? ` ${decimalLengthWord}` : ''} ${locale.currency.fractionalUnit.plural}`;
}
else if (locale.decimalLengthWordMapping && words !== '') {
words += ` ${locale.currency.fractionalUnit.plural}`;
}
const isEmpty = words.length <= 0 && wordsWithDecimal.length <= 0;

@@ -74,11 +90,17 @@ return ((!isEmpty && isNegativeNumber ? `${locale.texts.minus} ` : '') +

wordsWithDecimal +
(!isEmpty ? ` ${locale.texts.only}` : ''));
(!isEmpty && locale.texts.only ? ` ${locale.texts.only}` : ''));
}
else {
const isNumberZero = number >= 0 && number < 1;
const split = number.toString().split('.');
const words = this.convertInternal(Number(split[0]), options);
const ignoreZero = isNumberZero && ((_c = locale.options) === null || _c === void 0 ? void 0 : _c.ignoreZeroInDecimals);
const words = isFloat && ignoreZero
? ''
: this.convertInternal(Number(split[0]), options);
let wordsWithDecimal = '';
if (isFloat) {
wordsWithDecimal += ` ${locale.texts.point} `;
if (split[1].startsWith('0')) {
const decimalLengthWord = (_d = locale === null || locale === void 0 ? void 0 : locale.decimalLengthWordMapping) === null || _d === void 0 ? void 0 : _d[split[1].length];
if (!ignoreZero)
wordsWithDecimal += ` ${locale.texts.point} `;
if (split[1].startsWith('0') && !locale.decimalLengthWordMapping) {
const zeroWords = [];

@@ -91,3 +113,3 @@ for (const num of split[1]) {

else {
wordsWithDecimal += this.convertInternal(Number(split[1]), options);
wordsWithDecimal += `${this.convertInternal(Number(split[1]), options)}${decimalLengthWord ? ` ${decimalLengthWord}` : ''}`;
}

@@ -102,3 +124,6 @@ }

convertInternal(number, options = {}) {
var _a, _b, _c;
const locale = this.getLocale();
const splitWord = ((_a = locale.options) === null || _a === void 0 ? void 0 : _a.splitWord) ? `${(_b = locale.options) === null || _b === void 0 ? void 0 : _b.splitWord} `
: '';
const match = locale.numberWordsMapping.find((elem) => {

@@ -111,7 +136,7 @@ return number >= elem.number;

let words = '';
if (number <= 100) {
if (number <= 100 || (number < 1000 && ((_c = locale.options) === null || _c === void 0 ? void 0 : _c.namedLessThan1000))) {
words += match.value;
number -= match.number;
if (number > 0) {
words += ` ${this.convertInternal(number, options)}`;
words += ` ${splitWord}${this.convertInternal(number, options)}`;
}

@@ -123,3 +148,3 @@ }

if (remainder > 0) {
return `${this.convertInternal(quotient, options)} ${match.value} ${this.convertInternal(remainder, options)}`;
return `${this.convertInternal(quotient, options)} ${match.value} ${splitWord}${this.convertInternal(remainder, options)}`;
}

@@ -126,0 +151,0 @@ else {

{
"name": "to-words",
"version": "2.2.3",
"version": "2.2.4",
"description": "Converts numbers (including decimal points) into words & currency.",

@@ -47,27 +47,26 @@ "keywords": [

},
"dependencies": {},
"devDependencies": {
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@types/jest": "^26.0.0",
"@types/lodash": "^4.14.150",
"@types/node": "^14.0.1",
"@types/jest": "^26.0.15",
"@types/lodash": "^4.14.165",
"@types/node": "^14.14.6",
"@types/source-map-support": "^0.5.1",
"@typescript-eslint/eslint-plugin": "^4.2.0",
"@typescript-eslint/parser": "^4.2.0",
"eslint": "^7.2.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-jest": "^24.0.0",
"@typescript-eslint/eslint-plugin": "^4.6.1",
"@typescript-eslint/parser": "^4.6.1",
"eslint": "^7.13.0",
"eslint-config-prettier": "^6.15.0",
"eslint-plugin-jest": "^24.1.0",
"eslint-plugin-prettier": "^3.1.3",
"husky": "^4.2.5",
"jest": "^26.0.1",
"lint-staged": "^10.2.2",
"jest": "^26.6.3",
"lint-staged": "^10.5.1",
"lodash": "^4.17.15",
"prettier": "^2.0.5",
"rimraf": "^3.0.2",
"sort-package-json": "^1.42.1",
"sort-package-json": "^1.46.1",
"source-map-support": "^0.5.19",
"standard-version": "^9.0.0",
"ts-jest": "^26.1.0",
"typescript": "^4.0.2"
"ts-jest": "^26.4.3",
"typescript": "^4.0.5"
},

@@ -74,0 +73,0 @@ "engines": {

@@ -13,2 +13,8 @@ export interface LocaleInterface {

options?: {
namedLessThan1000?: boolean;
splitWord?: string;
ignoreZeroInDecimals?: boolean;
};
texts: {

@@ -25,2 +31,6 @@ and: string;

}[];
decimalLengthWordMapping?: {
[decimalLength: number]: string;
};
}

@@ -47,2 +47,4 @@ import { LocaleInterface } from './locales/locale.interface';

return require('./locales/en-US').Locale;
case 'fa-IR':
return require('./locales/fa-IR').Locale;
}

@@ -85,21 +87,32 @@ /* eslint-enable @typescript-eslint/no-var-requires */

isFloat = this.isFloat(number);
const isNumberZero = number >= 0 && number < 1;
const split = number.toString().split('.');
let words = `${this.convertInternal(Number(split[0]), options)} ${
locale.currency.plural
let words = `${this.convertInternal(Number(split[0]), options)}${
locale.currency.plural ? ` ${locale.currency.plural}` : ''
}`;
const isNumberZero = number >= 0 && number < 1;
const ignoreZero =
options.ignoreZeroCurrency ||
(locale.options?.ignoreZeroInDecimals && number !== 0);
if (isNumberZero && options.ignoreZeroCurrency) {
if (isNumberZero && ignoreZero) {
words = '';
}
let wordsWithDecimal = '';
if (isFloat) {
if (!isNumberZero || !options.ignoreZeroCurrency) {
if (!isNumberZero || !ignoreZero) {
wordsWithDecimal += ` ${locale.texts.and} `;
}
const decimalLengthWord =
locale?.decimalLengthWordMapping?.[split[1].length];
wordsWithDecimal += `${this.convertInternal(
Number(split[1]) * Math.pow(10, 2 - split[1].length),
Number(split[1]) *
(!locale.decimalLengthWordMapping
? Math.pow(10, 2 - split[1].length)
: 1),
options,
)} ${locale.currency.fractionalUnit.plural}`;
)}${decimalLengthWord ? ` ${decimalLengthWord}` : ''} ${
locale.currency.fractionalUnit.plural
}`;
} else if (locale.decimalLengthWordMapping && words !== '') {
words += ` ${locale.currency.fractionalUnit.plural}`;
}

@@ -111,13 +124,19 @@ const isEmpty = words.length <= 0 && wordsWithDecimal.length <= 0;

wordsWithDecimal +
(!isEmpty ? ` ${locale.texts.only}` : '')
(!isEmpty && locale.texts.only ? ` ${locale.texts.only}` : '')
);
} else {
const isNumberZero = number >= 0 && number < 1;
const split = number.toString().split('.');
const words = this.convertInternal(Number(split[0]), options);
const ignoreZero = isNumberZero && locale.options?.ignoreZeroInDecimals;
const words =
isFloat && ignoreZero
? ''
: this.convertInternal(Number(split[0]), options);
let wordsWithDecimal = '';
if (isFloat) {
wordsWithDecimal += ` ${locale.texts.point} `;
if (split[1].startsWith('0')) {
const decimalLengthWord =
locale?.decimalLengthWordMapping?.[split[1].length];
if (!ignoreZero) wordsWithDecimal += ` ${locale.texts.point} `;
if (split[1].startsWith('0') && !locale.decimalLengthWordMapping) {
const zeroWords = [];

@@ -129,3 +148,6 @@ for (const num of split[1]) {

} else {
wordsWithDecimal += this.convertInternal(Number(split[1]), options);
wordsWithDecimal += `${this.convertInternal(
Number(split[1]),
options,
)}${decimalLengthWord ? ` ${decimalLengthWord}` : ''}`;
}

@@ -144,2 +166,5 @@ }

const locale = this.getLocale();
const splitWord = locale.options?.splitWord
? `${locale.options?.splitWord} `
: '';
const match = locale.numberWordsMapping.find((elem) => {

@@ -154,7 +179,7 @@ return number >= elem.number;

let words = '';
if (number <= 100) {
if (number <= 100 || (number < 1000 && locale.options?.namedLessThan1000)) {
words += match.value;
number -= match.number;
if (number > 0) {
words += ` ${this.convertInternal(number, options)}`;
words += ` ${splitWord}${this.convertInternal(number, options)}`;
}

@@ -167,3 +192,3 @@ } else {

match.value
} ${this.convertInternal(remainder, options)}`;
} ${splitWord}${this.convertInternal(remainder, options)}`;
} else {

@@ -170,0 +195,0 @@ return `${this.convertInternal(quotient, options)} ${match.value}`;

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc