Socket
Socket
Sign inDemoInstall

humanizer.node

Package Overview
Dependencies
0
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.3 to 1.0.4

dist/metricNumerals.js

13

byteSize.ts

@@ -1,2 +0,1 @@

// @ts-ignore
import { ByteSize } from './src/Humanizer/Bytes/ByteSize'

@@ -16,23 +15,23 @@

Number.prototype.bits = function (): ByteSize {
return ByteSize.fromBits(this)
return ByteSize.fromBits(this as number)
}
Number.prototype.bytes = function (): ByteSize {
return ByteSize.fromBytes(this)
return ByteSize.fromBytes(this as number)
}
Number.prototype.kilobytes = function (): ByteSize {
return ByteSize.fromKilobytes(this)
return ByteSize.fromKilobytes(this as number)
}
Number.prototype.megabytes = function (): ByteSize {
return ByteSize.fromMegabytes(this)
return ByteSize.fromMegabytes(this as number)
}
Number.prototype.gigabytes = function (): ByteSize {
return ByteSize.fromGigabytes(this)
return ByteSize.fromGigabytes(this as number)
}
Number.prototype.terabytes = function (): ByteSize {
return ByteSize.fromTerabytes(this)
return ByteSize.fromTerabytes(this as number)
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// @ts-ignore
const ByteSize_1 = require("./src/Humanizer/Bytes/ByteSize");

@@ -5,0 +4,0 @@ Number.prototype.bits = function () {

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ordinalize = exports.byteSize = exports.vocabularies = exports.toQuantity = exports.romanNumeral = exports.numberToWords = void 0;
const numberToWords = __importStar(require("./numberToWords"));
exports.numberToWords = numberToWords;
const romanNumeral = __importStar(require("./romanNumeral"));
exports.romanNumeral = romanNumeral;
const romanNumerals = __importStar(require("./romanNumerals"));
exports.romanNumerals = romanNumerals;
const toQuantity = __importStar(require("./toQuantity"));

@@ -35,2 +22,6 @@ exports.toQuantity = toQuantity;

exports.ordinalize = ordinalize;
const numberToNumbers = __importStar(require("./numberToNumbers"));
exports.numberToNumbers = numberToNumbers;
const metricNumerals = __importStar(require("./metricNumerals"));
exports.metricNumerals = metricNumerals;
//# sourceMappingURL=index.js.map
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;

@@ -20,0 +8,0 @@ };

"use strict";
//The MIT License (MIT)
Object.defineProperty(exports, "__esModule", { value: true });
exports.ByteSize = void 0;
//Copyright (c) 2013-2014 Omar Khudeira (http://omar.io)

@@ -21,228 +20,224 @@ //Permission is hereby granted, free of charge, to any person obtaining a copy

//THE SOFTWARE.
let ByteSize = /** @class */ (() => {
class ByteSize {
constructor(byteSize) {
// Get ceiling because bis are whole units
this.bits = Math.ceil(byteSize * ByteSize.BitsInByte);
this.bytes = byteSize;
this.kilobytes = byteSize / ByteSize.BytesInKilobyte;
this.megabytes = byteSize / ByteSize.BytesInMegabyte;
this.gigabytes = byteSize / ByteSize.BytesInGigabyte;
this.terabytes = byteSize / ByteSize.BytesInTerabyte;
class ByteSize {
constructor(byteSize) {
// Get ceiling because bis are whole units
this.bits = Math.ceil(byteSize * ByteSize.BitsInByte);
this.bytes = byteSize;
this.kilobytes = byteSize / ByteSize.BytesInKilobyte;
this.megabytes = byteSize / ByteSize.BytesInMegabyte;
this.gigabytes = byteSize / ByteSize.BytesInGigabyte;
this.terabytes = byteSize / ByteSize.BytesInTerabyte;
}
largestWholeNumberSymbol() {
// Absolute value is used to deal with negative values
if (Math.abs(this.terabytes) >= 1) {
return ByteSize.TerabyteSymbol;
}
largestWholeNumberSymbol() {
// Absolute value is used to deal with negative values
if (Math.abs(this.terabytes) >= 1) {
return ByteSize.TerabyteSymbol;
}
if (Math.abs(this.gigabytes) >= 1) {
return ByteSize.GigabyteSymbol;
}
if (Math.abs(this.megabytes) >= 1) {
return ByteSize.MegabyteSymbol;
}
if (Math.abs(this.kilobytes) >= 1) {
return ByteSize.KilobyteSymbol;
}
if (Math.abs(this.bytes) >= 1) {
return ByteSize.ByteSymbol;
}
return ByteSize.BitSymbol;
if (Math.abs(this.gigabytes) >= 1) {
return ByteSize.GigabyteSymbol;
}
largestWholeNumberFullWord() {
// Absolute value is used to deal with negative values
if (Math.abs(this.terabytes) >= 1) {
return ByteSize.Terabyte;
}
if (Math.abs(this.gigabytes) >= 1) {
return ByteSize.Gigabyte;
}
if (Math.abs(this.megabytes) >= 1) {
return ByteSize.Megabyte;
}
if (Math.abs(this.kilobytes) >= 1) {
return ByteSize.Kilobyte;
}
if (Math.abs(this.bytes) >= 1) {
return ByteSize.Byte;
}
return ByteSize.Bit;
if (Math.abs(this.megabytes) >= 1) {
return ByteSize.MegabyteSymbol;
}
largestWholeNumberValue() {
// Absolute value is used to deal with negative values
if (Math.abs(this.terabytes) >= 1) {
return this.terabytes;
}
if (Math.abs(this.gigabytes) >= 1) {
return this.gigabytes;
}
if (Math.abs(this.megabytes) >= 1) {
return this.megabytes;
}
if (Math.abs(this.kilobytes) >= 1) {
return this.kilobytes;
}
if (Math.abs(this.bytes) >= 1) {
return this.bytes;
}
return this.bits;
if (Math.abs(this.kilobytes) >= 1) {
return ByteSize.KilobyteSymbol;
}
static fromBits(value) {
return new ByteSize(value / ByteSize.BitsInByte);
if (Math.abs(this.bytes) >= 1) {
return ByteSize.ByteSymbol;
}
static fromBytes(value) {
return new ByteSize(value);
return ByteSize.BitSymbol;
}
largestWholeNumberFullWord() {
// Absolute value is used to deal with negative values
if (Math.abs(this.terabytes) >= 1) {
return ByteSize.Terabyte;
}
static fromKilobytes(value) {
return new ByteSize(value * ByteSize.BytesInKilobyte);
if (Math.abs(this.gigabytes) >= 1) {
return ByteSize.Gigabyte;
}
static fromMegabytes(value) {
return new ByteSize(value * ByteSize.BytesInMegabyte);
if (Math.abs(this.megabytes) >= 1) {
return ByteSize.Megabyte;
}
static fromGigabytes(value) {
return new ByteSize(value * ByteSize.BytesInGigabyte);
if (Math.abs(this.kilobytes) >= 1) {
return ByteSize.Kilobyte;
}
static fromTerabytes(value) {
return new ByteSize(value * ByteSize.BytesInTerabyte);
if (Math.abs(this.bytes) >= 1) {
return ByteSize.Byte;
}
/// <summary>
/// Converts the value of the current ByteSize object to a string.
/// The metric prefix symbol (bit, byte, kilo, mega, giga, tera) used is
/// the largest metric prefix such that the corresponding value is greater
/// than or equal to one.
/// </summary>
toString() {
return this.largestWholeNumberValue() + " " + this.largestWholeNumberSymbol();
return ByteSize.Bit;
}
largestWholeNumberValue() {
// Absolute value is used to deal with negative values
if (Math.abs(this.terabytes) >= 1) {
return this.terabytes;
}
/// <summary>
/// Converts the value of the current ByteSize object to a string with
/// full words. The metric prefix symbol (bit, byte, kilo, mega, giga,
/// tera) used is the largest metric prefix such that the corresponding
/// value is greater than or equal to one.
/// </summary>
toFullWords() {
var byteSizeAsFullWords = this.toString();
// If there is more than one unit, make the word plural
return this.largestWholeNumberValue() > 1 ? byteSizeAsFullWords + "s" : byteSizeAsFullWords;
if (Math.abs(this.gigabytes) >= 1) {
return this.gigabytes;
}
equals(other) {
return this.bits === other.bits;
if (Math.abs(this.megabytes) >= 1) {
return this.megabytes;
}
compareTo(other) {
if (this.bits - other.bits < 0) {
return -1;
}
if (this.bits - other.bits > 0) {
return 1;
}
return 0;
if (Math.abs(this.kilobytes) >= 1) {
return this.kilobytes;
}
add(other) {
return ByteSize.fromBits(this.bits + other.bits);
if (Math.abs(this.bytes) >= 1) {
return this.bytes;
}
addBits(value) {
return ByteSize.fromBits(this.bits + value);
return this.bits;
}
static fromBits(value) {
return new ByteSize(value / ByteSize.BitsInByte);
}
static fromBytes(value) {
return new ByteSize(value);
}
static fromKilobytes(value) {
return new ByteSize(value * ByteSize.BytesInKilobyte);
}
static fromMegabytes(value) {
return new ByteSize(value * ByteSize.BytesInMegabyte);
}
static fromGigabytes(value) {
return new ByteSize(value * ByteSize.BytesInGigabyte);
}
static fromTerabytes(value) {
return new ByteSize(value * ByteSize.BytesInTerabyte);
}
/// <summary>
/// Converts the value of the current ByteSize object to a string.
/// The metric prefix symbol (bit, byte, kilo, mega, giga, tera) used is
/// the largest metric prefix such that the corresponding value is greater
/// than or equal to one.
/// </summary>
toString(fixePoint = 2) {
return this.largestWholeNumberValue().toFixed(fixePoint) + " " + this.largestWholeNumberSymbol();
}
/// <summary>
/// Converts the value of the current ByteSize object to a string with
/// full words. The metric prefix symbol (bit, byte, kilo, mega, giga,
/// tera) used is the largest metric prefix such that the corresponding
/// value is greater than or equal to one.
/// </summary>
toFullWords() {
const byteSizeAsFullWords = this.toString();
// If there is more than one unit, make the word plural
return this.largestWholeNumberValue() > 1 ? byteSizeAsFullWords + "s" : byteSizeAsFullWords;
}
equals(other) {
return this.bits === other.bits;
}
compareTo(other) {
if (this.bits - other.bits < 0) {
return -1;
}
addBytes(value) {
return ByteSize.fromBytes(this.bytes + value);
if (this.bits - other.bits > 0) {
return 1;
}
addKilobytes(value) {
return ByteSize.fromKilobytes(this.kilobytes + value);
return 0;
}
add(other) {
return ByteSize.fromBits(this.bits + other.bits);
}
addBits(value) {
return ByteSize.fromBits(this.bits + value);
}
addBytes(value) {
return ByteSize.fromBytes(this.bytes + value);
}
addKilobytes(value) {
return ByteSize.fromKilobytes(this.kilobytes + value);
}
addMegabytes(value) {
return ByteSize.fromMegabytes(this.megabytes + value);
}
addGigabytes(value) {
return ByteSize.fromGigabytes(this.gigabytes + value);
}
addTerabytes(value) {
return ByteSize.fromTerabytes(this.terabytes + value);
}
subtract(other) {
return ByteSize.fromBits(this.bits - other.bits);
}
static parse(s) {
// Arg checking
if (!s) {
throw new Error("The input is null or empty");
}
addMegabytes(value) {
return ByteSize.fromMegabytes(this.megabytes + value);
// Setup the result
let result = new ByteSize(4);
// Get the index of the first non-digit character
s = s.trimStart(); // Protect against leading spaces, https://github.com/microsoft/TypeScript/blob/master/lib/lib.es2019.string.d.ts
let num;
let found = false;
// Acquiring culture specific decimal separator
const decSep = "."; // Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
// Pick first non-digit number
for (num = 0; num < s.length; num++) {
if (!(!isNaN(Number(s[num])) || s[num] === decSep)) {
found = true;
break;
}
}
addGigabytes(value) {
return ByteSize.fromGigabytes(this.gigabytes + value);
if (found === false) {
return new ByteSize(1); // TODO: should raize error
}
addTerabytes(value) {
return ByteSize.fromTerabytes(this.terabytes + value);
const lastNumber = num;
// Cut the input string in half
const numberPart = s.substring(0, lastNumber).trim();
const sizePart = s.substring(lastNumber, s.length).trim();
// Get the numeric part
if (isNaN(Number(numberPart))) {
return new ByteSize(2); // TODO: should raize error
}
subtract(other) {
return ByteSize.fromBits(this.bits - other.bits);
}
static parse(s) {
// Arg checking
if (!s) {
throw new Error("The input is null or empty");
}
// Setup the result
let result = new ByteSize(4);
// Get the index of the first non-digit character
s = s.trimStart(); // Protect against leading spaces, https://github.com/microsoft/TypeScript/blob/master/lib/lib.es2019.string.d.ts
let num;
let found = false;
// Acquiring culture specific decimal separator
let decSep = "."; //Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
// Pick first non-digit number
for (num = 0; num < s.length; num++) {
if (!(!isNaN(Number(s[num])) || s[num] == decSep)) {
found = true;
break;
const nb = Number(numberPart);
// Get the magnitude part
switch (sizePart.toUpperCase()) {
case ByteSize.ByteSymbol:
if (sizePart === ByteSize.BitSymbol) { // Bits
if (nb % 1 !== 0) {
return new ByteSize(3); // TODO: should raize error
}
result = ByteSize.fromBits(nb);
}
}
if (found == false) {
return new ByteSize(1); //TODO: should raize error
}
let lastNumber = num;
// Cut the input string in half
var numberPart = s.substring(0, lastNumber).trim();
var sizePart = s.substring(lastNumber, s.length).trim();
// Get the numeric part
if (isNaN(Number(numberPart))) {
return new ByteSize(2); //TODO: should raize error
}
let nb = Number(numberPart);
// Get the magnitude part
switch (sizePart.toUpperCase()) {
case ByteSize.ByteSymbol:
if (sizePart == ByteSize.BitSymbol) { // Bits
if (nb % 1 != 0) // Can't have partial bits
{
return new ByteSize(3); //TODO: should raize error
}
result = ByteSize.fromBits(nb);
}
else { // Bytes
result = ByteSize.fromBytes(nb);
}
break;
case ByteSize.KilobyteSymbol:
result = ByteSize.fromKilobytes(nb);
break;
case ByteSize.MegabyteSymbol:
result = ByteSize.fromMegabytes(nb);
break;
case ByteSize.GigabyteSymbol:
result = ByteSize.fromGigabytes(nb);
break;
case ByteSize.TerabyteSymbol:
result = ByteSize.fromTerabytes(nb);
break;
}
return result;
else { // Bytes
result = ByteSize.fromBytes(nb);
}
break;
case ByteSize.KilobyteSymbol:
result = ByteSize.fromKilobytes(nb);
break;
case ByteSize.MegabyteSymbol:
result = ByteSize.fromMegabytes(nb);
break;
case ByteSize.GigabyteSymbol:
result = ByteSize.fromGigabytes(nb);
break;
case ByteSize.TerabyteSymbol:
result = ByteSize.fromTerabytes(nb);
break;
}
return result;
}
ByteSize.MinValue = ByteSize.fromBits(Number.MIN_VALUE);
ByteSize.MaxValue = ByteSize.fromBits(Number.MAX_VALUE);
ByteSize.BitsInByte = 8;
ByteSize.BytesInKilobyte = 1024;
ByteSize.BytesInMegabyte = 1048576;
ByteSize.BytesInGigabyte = 1073741824;
ByteSize.BytesInTerabyte = 1099511627776;
ByteSize.BitSymbol = "b";
ByteSize.Bit = "bit";
ByteSize.ByteSymbol = "B";
ByteSize.Byte = "byte";
ByteSize.KilobyteSymbol = "KB";
ByteSize.Kilobyte = "kilobyte";
ByteSize.MegabyteSymbol = "MB";
ByteSize.Megabyte = "megabyte";
ByteSize.GigabyteSymbol = "GB";
ByteSize.Gigabyte = "gigabyte";
ByteSize.TerabyteSymbol = "TB";
ByteSize.Terabyte = "terabyte";
return ByteSize;
})();
}
exports.ByteSize = ByteSize;
ByteSize.MinValue = ByteSize.fromBits(Number.MIN_VALUE);
ByteSize.MaxValue = ByteSize.fromBits(Number.MAX_VALUE);
ByteSize.BitsInByte = 8;
ByteSize.BytesInKilobyte = 1024;
ByteSize.BytesInMegabyte = 1048576;
ByteSize.BytesInGigabyte = 1073741824;
ByteSize.BytesInTerabyte = 1099511627776;
ByteSize.BitSymbol = "b";
ByteSize.Bit = "bit";
ByteSize.ByteSymbol = "B";
ByteSize.Byte = "byte";
ByteSize.KilobyteSymbol = "KB";
ByteSize.Kilobyte = "kilobyte";
ByteSize.MegabyteSymbol = "MB";
ByteSize.Megabyte = "megabyte";
ByteSize.GigabyteSymbol = "GB";
ByteSize.Gigabyte = "gigabyte";
ByteSize.TerabyteSymbol = "TB";
ByteSize.Terabyte = "terabyte";
//# sourceMappingURL=ByteSize.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.convert = void 0;
exports.convert = function (nb, nbString) {
let nMod100 = nb % 100;
const nMod100 = nb % 100;
if (nMod100 >= 11 && nMod100 <= 13) {

@@ -7,0 +6,0 @@ return nbString + "th";

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Vocabularies = void 0;
// @ts-ignore

@@ -22,3 +21,3 @@ const Vocabulary_1 = require("./Vocabulary");

static BuildDefault() {
var _default = new Vocabulary_1.Vocabulary();
const _default = new Vocabulary_1.Vocabulary();
_default.addPlural("$", "s");

@@ -85,3 +84,3 @@ _default.addPlural("s$", "s");

_default.addIrregular("personnel", "personnel");
//Fix #78a
// Fix #78a
_default.addIrregular("cache", "caches");

@@ -88,0 +87,0 @@ _default.addIrregular("is", "are", false);

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Vocabulary = void 0;
/// <summary>

@@ -20,3 +19,4 @@ /// A container for exceptions to simple pluralization/singularization rules.

/// <param name="plural">The plural form of the irregular word, e.g. "people".</param>
/// <param name="matchEnding">True to match these words on their own as well as at the end of longer words. False, otherwise.</param>
/// <param name="matchEnding">True to match these words on their own as well
/// as at the end of longer words. False, otherwise.</param>
addIrregular(singular, plural, matchEnding = true) {

@@ -59,12 +59,13 @@ if (matchEnding) {

/// <param name="word">Word to be pluralized</param>
/// <param name="inputIsKnownToBeSingular">Normally you call Pluralize on singular words; but if you're unsure call it with false</param>
/// <param name="inputIsKnownToBeSingular">Normally you call Pluralize on singular words;
/// but if you're unsure call it with false</param>
/// <returns></returns>
pluralize(word, inputIsKnownToBeSingular = true) {
var result = this.applyRules(this._plurals, word, false);
const result = this.applyRules(this._plurals, word, false);
if (inputIsKnownToBeSingular) {
return result !== null && result !== void 0 ? result : word;
return (result !== null && result !== void 0 ? result : word);
}
var asSingular = this.applyRules(this._singulars, word, false);
var asSingularAsPlural = this.applyRules(this._plurals, asSingular, false);
if (asSingular != null && asSingular != word && asSingular + "s" != word && asSingularAsPlural == word && result != word) {
const asSingular = this.applyRules(this._singulars, word, false);
const asSingularAsPlural = this.applyRules(this._plurals, asSingular, false);
if (asSingular !== "" && asSingular !== word && asSingular + "s" !== word && asSingularAsPlural === word && result !== word) {
return word;

@@ -78,20 +79,22 @@ }

/// <param name="word">Word to be singularized</param>
/// <param name="inputIsKnownToBePlural">Normally you call Singularize on plural words; but if you're unsure call it with false</param>
/// <param name="inputIsKnownToBePlural">Normally you call Singularize on plural words; but if you're unsure
/// call it with false</param>
/// <param name="skipSimpleWords">Skip singularizing single words that have an 's' on the end</param>
/// <returns></returns>
singularize(word, inputIsKnownToBePlural = true, skipSimpleWords = false) {
var result = this.applyRules(this._singulars, word, skipSimpleWords);
const result = this.applyRules(this._singulars, word, skipSimpleWords);
if (inputIsKnownToBePlural) {
return result !== null && result !== void 0 ? result : word;
return (result !== null && result !== void 0 ? result : word);
}
// the Plurality is unknown so we should check all possibilities
var asPlural = this.applyRules(this._plurals, word, false);
var asPluralAsSingular = this.applyRules(this._singulars, asPlural, false);
if (asPlural != word && word + "s" != asPlural && asPluralAsSingular == word && result != word) {
const asPlural = this.applyRules(this._plurals, word, false);
const asPluralAsSingular = this.applyRules(this._singulars, asPlural, false);
if (asPlural !== word && word + "s" !== asPlural && asPluralAsSingular === word && result !== word) {
return word;
}
return result !== null && result !== void 0 ? result : word;
return result === "" ? word : result;
}
applyRules(rules, word, skipFirstRule) {
if (word == null) {
// @ts-ignore
return null;

@@ -102,6 +105,6 @@ }

}
var result = word;
var end = skipFirstRule ? 1 : 0;
for (var i = rules.length - 1; i >= end; i--) {
if ((result = rules[i].apply(word)) != null) {
let result = word;
const end = skipFirstRule ? 1 : 0;
for (let i = rules.length - 1; i >= end; i--) {
if ((result = rules[i].apply(word)) !== "") {
break;

@@ -119,3 +122,3 @@ }

constructor(pattern, replacement) {
this._regex = new RegExp(pattern, 'i');
this._regex = new RegExp(pattern, "i");
this._replacement = replacement;

@@ -125,3 +128,3 @@ }

if (!this._regex.test(word)) {
return null;
return "";
}

@@ -128,0 +131,0 @@ return word.replace(this._regex, this._replacement);

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnglishNumberToWordsConverter = void 0;
let EnglishNumberToWordsConverter = /** @class */ (() => {
class EnglishNumberToWordsConverter {
static convertToOrdinal(number) {
return EnglishNumberToWordsConverter.convert(number, true);
class EnglishNumberToWordsConverter {
static convertToOrdinal(number) {
return EnglishNumberToWordsConverter.convert(number, true);
}
static convert(number, isOrdinal = false) {
number = Math.floor(number);
if (number === 0) {
return EnglishNumberToWordsConverter.getUnitValue(0, isOrdinal);
}
static convert(number, isOrdinal = false) {
number = Math.floor(number);
if (number == 0) {
return EnglishNumberToWordsConverter.getUnitValue(0, isOrdinal);
if (number < 0) {
return `minus ${EnglishNumberToWordsConverter.convert(-number)}`;
}
const parts = new Array();
if (Math.floor(number / 1000000000000000000) > 0) {
parts.push(`${EnglishNumberToWordsConverter.convert(number / 1000000000000000000)} quintillion`);
number %= 1000000000000000000;
}
if (Math.floor(number / 1000000000000000) > 0) {
parts.push(`${EnglishNumberToWordsConverter.convert(number / 1000000000000000)} quadrillion`);
number %= 1000000000000000;
}
if (Math.floor(number / 1000000000000) > 0) {
parts.push(`${EnglishNumberToWordsConverter.convert(number / 1000000000000)} trillion`);
number %= 1000000000000;
}
if (Math.floor(number / 1000000000) > 0) {
parts.push(`${EnglishNumberToWordsConverter.convert(number / 1000000000)} billion`);
number %= 1000000000;
}
if (Math.floor(number / 1000000) > 0) {
parts.push(`${EnglishNumberToWordsConverter.convert(number / 1000000)} million`);
number %= 1000000;
}
if (Math.floor(number / 1000) > 0) {
parts.push(`${EnglishNumberToWordsConverter.convert(number / 1000)} thousand`);
number %= 1000;
}
if (Math.floor(number / 100) > 0) {
parts.push(`${EnglishNumberToWordsConverter.convert(number / 100)} hundred`);
number %= 100;
}
if (Math.floor(number) > 0) {
if (parts.length !== 0) {
parts.push("and");
}
if (number < 0) {
return `minus ${EnglishNumberToWordsConverter.convert(-number)}`;
if (number < 20) {
parts.push(EnglishNumberToWordsConverter.getUnitValue(number, isOrdinal));
}
var parts = new Array();
if (Math.floor(number / 1000000000000000000) > 0) {
parts.push(`${EnglishNumberToWordsConverter.convert(number / 1000000000000000000)} quintillion`);
number %= 1000000000000000000;
}
if (Math.floor(number / 1000000000000000) > 0) {
parts.push(`${EnglishNumberToWordsConverter.convert(number / 1000000000000000)} quadrillion`);
number %= 1000000000000000;
}
if (Math.floor(number / 1000000000000) > 0) {
parts.push(`${EnglishNumberToWordsConverter.convert(number / 1000000000000)} trillion`);
number %= 1000000000000;
}
if (Math.floor(number / 1000000000) > 0) {
parts.push(`${EnglishNumberToWordsConverter.convert(number / 1000000000)} billion`);
number %= 1000000000;
}
if (Math.floor(number / 1000000) > 0) {
parts.push(`${EnglishNumberToWordsConverter.convert(number / 1000000)} million`);
number %= 1000000;
}
if (Math.floor(number / 1000) > 0) {
parts.push(`${EnglishNumberToWordsConverter.convert(number / 1000)} thousand`);
number %= 1000;
}
if (Math.floor(number / 100) > 0) {
parts.push(`${EnglishNumberToWordsConverter.convert(number / 100)} hundred`);
number %= 100;
}
if (Math.floor(number) > 0) {
if (parts.length != 0) {
parts.push("and");
else {
let lastPart = EnglishNumberToWordsConverter.TensMap[Math.floor(number / 10)];
if ((number % 10) > 0) {
lastPart += `-${EnglishNumberToWordsConverter.getUnitValue(number % 10, isOrdinal)}`;
}
if (number < 20) {
parts.push(EnglishNumberToWordsConverter.getUnitValue(number, isOrdinal));
else if (isOrdinal) {
const trimmed = lastPart.replace(/\y+$/g, ""); // lastPart.trimEnd('y')
lastPart = trimmed + "ieth";
}
else {
var lastPart = EnglishNumberToWordsConverter.TensMap[Math.floor(number / 10)];
if ((number % 10) > 0) {
lastPart += `-${EnglishNumberToWordsConverter.getUnitValue(number % 10, isOrdinal)}`;
}
else if (isOrdinal) {
let trimmed = lastPart.replace(/\y+$/g, ''); //lastPart.trimEnd('y')
lastPart = trimmed + "ieth";
}
parts.push(lastPart);
}
parts.push(lastPart);
}
else if (isOrdinal) {
parts[parts.length - 1] += "th";
}
var toWords = parts.join(" ");
if (isOrdinal) {
toWords = EnglishNumberToWordsConverter.removeOnePrefix(toWords);
}
return toWords;
}
static getUnitValue(number, isOrdinal) {
if (isOrdinal) {
let exceptionString = EnglishNumberToWordsConverter.exceptionNumbersToWords(number);
if (exceptionString != "NOTFOUND") {
return exceptionString;
}
else {
return EnglishNumberToWordsConverter.UnitsMap[number] + "th";
}
else if (isOrdinal) {
parts[parts.length - 1] += "th";
}
let toWords = parts.join(" ");
if (isOrdinal) {
toWords = EnglishNumberToWordsConverter.removeOnePrefix(toWords);
}
return toWords;
}
static getUnitValue(number, isOrdinal) {
if (isOrdinal) {
const exceptionString = EnglishNumberToWordsConverter.exceptionNumbersToWords(number);
if (exceptionString !== "NOTFOUND") {
return exceptionString;
}
else {
return EnglishNumberToWordsConverter.UnitsMap[number];
return EnglishNumberToWordsConverter.UnitsMap[number] + "th";
}
}
static removeOnePrefix(toWords) {
// one hundred => hundredth
if (toWords.indexOf("one") == 0) {
toWords = toWords.substring(4); //toWords.remove(0, 4);
}
return toWords;
else {
return EnglishNumberToWordsConverter.UnitsMap[number];
}
static exceptionNumbersToWords(number) {
let rtnVal = "NOTFOUND";
EnglishNumberToWordsConverter.OrdinalExceptions.forEach(element => {
if (element[0] == number) {
rtnVal = element[1].toString();
}
});
return rtnVal;
}
static removeOnePrefix(toWords) {
// one hundred => hundredth
if (toWords.indexOf("one") === 0) {
toWords = toWords.substring(4); // toWords.remove(0, 4);
}
return toWords;
}
EnglishNumberToWordsConverter.UnitsMap = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];
EnglishNumberToWordsConverter.TensMap = ["zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];
EnglishNumberToWordsConverter.OrdinalExceptions = [
[1, "first"],
[2, "second"],
[3, "third"],
[4, "fourth"],
[5, "fifth"],
[8, "eighth"],
[9, "ninth"],
[12, "twelfth"],
];
return EnglishNumberToWordsConverter;
})();
static exceptionNumbersToWords(number) {
let rtnVal = "NOTFOUND";
EnglishNumberToWordsConverter.OrdinalExceptions.forEach(element => {
// tslint:disable-next-line
if (element[0] == number) {
rtnVal = element[1].toString();
}
});
return rtnVal;
}
}
exports.EnglishNumberToWordsConverter = EnglishNumberToWordsConverter;
EnglishNumberToWordsConverter.UnitsMap = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];
EnglishNumberToWordsConverter.TensMap = ["zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];
EnglishNumberToWordsConverter.OrdinalExceptions = [
[1, "first"],
[2, "second"],
[3, "third"],
[4, "fourth"],
[5, "fifth"],
[8, "eighth"],
[9, "ninth"],
[12, "twelfth"],
];
//# sourceMappingURL=EnglishNumberToWordsConverter.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ShowQuantityAs = void 0;
// @ts-ignore

@@ -32,9 +31,9 @@ const Vocabularies_1 = require("./src/Humanizer/Inflections/Vocabularies");

String.prototype.toQuantity = function (quantity, showQuantityAs = ShowQuantityAs.Numeric) {
let transformedInput = quantity == 1
const transformedInput = quantity === 1
? Vocabularies_1.Vocabularies.Default().singularize(this, false)
: Vocabularies_1.Vocabularies.Default().pluralize(this, false);
if (showQuantityAs == ShowQuantityAs.None) {
if (showQuantityAs === ShowQuantityAs.None) {
return transformedInput;
}
if (showQuantityAs == ShowQuantityAs.Numeric) {
if (showQuantityAs === ShowQuantityAs.Numeric) {
return `${quantity} ${transformedInput}`;

@@ -41,0 +40,0 @@ }

import * as numberToWords from './numberToWords'
import * as romanNumeral from './romanNumeral'
import * as romanNumerals from './romanNumerals'
import * as toQuantity from './toQuantity'

@@ -7,10 +7,14 @@ import * as vocabularies from './vocabularies'

import * as ordinalize from './ordinalize'
import * as numberToNumbers from './numberToNumbers'
import * as metricNumerals from './metricNumerals'
export {
numberToWords,
romanNumeral,
toQuantity,
vocabularies,
byteSize,
ordinalize
numberToNumbers,
romanNumerals,
toQuantity,
vocabularies,
byteSize,
ordinalize,
metricNumerals,
numberToWords
}

@@ -16,3 +16,3 @@ // @ts-ignore

Number.prototype.toWords = function (): string {
return EnglishNumberToWordsConverter.convert(this)
return EnglishNumberToWordsConverter.convert(this as number)
}

@@ -25,4 +25,4 @@

Number.prototype.toOrdinalWords = function (): string {
return EnglishNumberToWordsConverter.convert(this, true)
return EnglishNumberToWordsConverter.convert(this as number, true)
}

@@ -15,7 +15,7 @@ // @ts-ignor

Number.prototype.ordinalize = function (): string {
return ord.convert(this, this.toString())
return ord.convert(this as number, this.toString())
}
String.prototype.ordinalize = function (): string {
return ord.convert(Number(this), this)
return ord.convert(Number(this), this as string)
}
{
"name": "humanizer.node",
"version": "1.0.3",
"version": "1.0.4",
"description": "Humanizer meets all your Node needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantitie",

@@ -9,3 +9,4 @@ "main": "dist/index.js",

"build": "rimraf irf dist/* && tsc",
"test": "mocha"
"test": "mocha",
"local": "npm run build && node ./local_test.js"
},

@@ -34,4 +35,4 @@ "repository": {

"ts-node": "^8.10.1",
"typescript": "^3.7.4"
"typescript": "3.7.4"
}
}

@@ -10,3 +10,20 @@ # Humanizer.node

```
### Table of contents
- [Humanizer.node](#humanizernode)
- [Table of contents](#table-of-contents)
- [Implemented](#implemented)
- [Usage](#usage)
- [Examples](#examples)
- [ByteSize](#bytesize)
- [Vocabularies](#vocabularies)
- [Pluralize](#pluralize)
- [Singularize](#singularize)
- [Ordinalize](#ordinalize)
- [ToQuantity](#toquantity)
- [Number to words](#number-to-words)
- [Number to ordinal words](#number-to-ordinal-words)
- [Roman Numerals](#roman-numerals)
- [Metric numerals](#metric-numerals)
## Implemented

@@ -25,8 +42,3 @@

```js
require('humanizer.node').toQuantity
require('humanizer.node').numberToWords
require('humanizer.node').byteSize
require('humanizer.node').vocabularies
require('humanizer.node').romanNumeral
require('humanizer.node').ordinalize
require('humanizer.node')
```

@@ -40,3 +52,3 @@

let result = (10).megabytes().toString()
console.log(result) // -> 10 MB
console.log(result) // -> 10.00 MB
```

@@ -51,3 +63,3 @@

```js
require('humanizer.node').byteSize
require('humanizer.node')

@@ -88,3 +100,3 @@ let fileSize = (10).kilobytes()

```js
require('humanizer.node').vocabularies
require('humanizer.node')

@@ -108,3 +120,3 @@ "Man".pluralize() // -> Men

```js
require('humanizer.node').ordinalize
require('humanizer.node')

@@ -118,3 +130,3 @@ (1).ordinalize() => "1st"

```js
require('humanizer.node').toQuantity
require('humanizer.node')

@@ -151,3 +163,3 @@ "case".toQuantity(0) => "0 cases"

```js
require('humanizer.node').numberToWords
require('humanizer.node')

@@ -164,3 +176,3 @@ (1).toWords() => "one"

```js
require('humanizer.node').numberToWords
require('humanizer.node')

@@ -176,3 +188,48 @@ (0).toOrdinalWords() => "zeroth"

(21).toOrdinalWords() => "twenty first"
(121).toOrdinalWords() => "hundred and twenty first"
(121).toOrdinalWords() => "hundred and twenty-first"
```
## Roman Numerals
Humanizer can change numbers to Roman numerals using the `toRoman` extension. The numbers 1 to 10 can be expressed in Roman numerals as follows:
```ts
(1).toRoman() => "I"
(2).toRoman() => "II"
(3).toRoman() => "III"
(4).toRoman() => "IV"
(5).toRoman() => "V"
(6).toRoman() => "VI"
(7).toRoman() => "VII"
(8).toRoman() => "VIII"
(9).toRoman() => "IX"
(10).toRoman() => "X"
```
Also the reverse operation using the `fromRoman` extension.
```ts
"I".fromRoman() => 1
"II".fromRoman() => 2
"III".fromRoman() => 3
"IV".fromRoman() => 4
"V".fromRoman() => 5
```
## Metric numerals
Humanizer can change numbers to Metric numerals using the `toMetric` extension. The numbers 1, 1230 and 0.1 can be expressed in Metric numerals as follows:
```js
(1).toMetric() => "1"
(1230).toMetric() => "1.23k"
(0.1).toMetric() => "100m"
```
Also the reverse operation using the `fromMetric` extension.
```js
"1".fromMetric() => 1
"1.23k".fromMetric() => 1230
"100m".fromMetric() => 0.1
```

@@ -173,4 +173,4 @@ //The MIT License (MIT)

/// </summary>
public toString(): string {
return this.largestWholeNumberValue() + " " + this.largestWholeNumberSymbol();
public toString(fixePoint: number = 2): string {
return this.largestWholeNumberValue().toFixed(fixePoint) + " " + this.largestWholeNumberSymbol();
}

@@ -186,3 +186,3 @@

public toFullWords(): string {
var byteSizeAsFullWords = this.toString()
const byteSizeAsFullWords = this.toString()

@@ -257,7 +257,7 @@ // If there is more than one unit, make the word plural

let decSep = "." //Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
const decSep = "." // Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
// Pick first non-digit number
for (num = 0; num < s.length; num++) {
if (!(!isNaN(Number(s[num])) || s[num] == decSep)) {
if (!(!isNaN(Number(s[num])) || s[num] === decSep)) {
found = true;

@@ -268,28 +268,26 @@ break;

if (found == false) {
return new ByteSize(1); //TODO: should raize error
if (found === false) {
return new ByteSize(1); // TODO: should raize error
}
let lastNumber = num;
const lastNumber = num;
// Cut the input string in half
var numberPart = s.substring(0, lastNumber).trim()
var sizePart = s.substring(lastNumber, s.length).trim()
const numberPart = s.substring(0, lastNumber).trim()
const sizePart = s.substring(lastNumber, s.length).trim()
// Get the numeric part
if (isNaN(Number(numberPart))) {
return new ByteSize(2); //TODO: should raize error
return new ByteSize(2); // TODO: should raize error
}
let nb = Number(numberPart)
const nb = Number(numberPart)
// Get the magnitude part
switch (sizePart.toUpperCase()) {
case ByteSize.ByteSymbol:
if (sizePart == ByteSize.BitSymbol) { // Bits
if (nb % 1 != 0) // Can't have partial bits
{
return new ByteSize(3); //TODO: should raize error
if (sizePart === ByteSize.BitSymbol) { // Bits
if (nb % 1 !== 0) {
return new ByteSize(3); // TODO: should raize error
}
result = ByteSize.fromBits(nb);
}
else { // Bytes
} else { // Bytes
result = ByteSize.fromBytes(nb);

@@ -296,0 +294,0 @@ }

export const convert = function(nb: number, nbString: string): string {
let nMod100 = nb % 100;
const nMod100 = nb % 100;
if (nMod100 >= 11 && nMod100 <= 13)
{
if (nMod100 >= 11 && nMod100 <= 13) {
return nbString + "th";
}
switch (nb % 10)
{
switch (nb % 10) {
case 1:

@@ -12,0 +10,0 @@ return nbString + "st";

// @ts-ignore
import { Vocabulary } from "./Vocabulary";
/// <summary>
/// Container for registered Vocabularies. At present, only a single vocabulary is supported: Default.
/// </summary>
export abstract class Vocabularies {
private static Instance: Vocabulary;
/// <summary>
/// Container for registered Vocabularies. At present, only a single vocabulary is supported: Default.
/// The default vocabulary used for singular/plural irregularities.
/// Rules can be added to this vocabulary and will be picked up by called to Singularize() and Pluralize().
/// At this time, multiple vocabularies and removing existing rules are not supported.
/// </summary>
export abstract class Vocabularies
{
private static Instance: Vocabulary;
/// <summary>
/// The default vocabulary used for singular/plural irregularities.
/// Rules can be added to this vocabulary and will be picked up by called to Singularize() and Pluralize().
/// At this time, multiple vocabularies and removing existing rules are not supported.
/// </summary>
public static Default(): Vocabulary {
if (!Vocabularies.Instance) {
Vocabularies.Instance = Vocabularies.BuildDefault()
}
return Vocabularies.Instance
public static Default(): Vocabulary {
if (!Vocabularies.Instance) {
Vocabularies.Instance = Vocabularies.BuildDefault()
}
return Vocabularies.Instance
}
private static BuildDefault(): Vocabulary
{
var _default = new Vocabulary();
private static BuildDefault(): Vocabulary {
const _default = new Vocabulary();
_default.addPlural("$", "s");
_default.addPlural("s$", "s");
_default.addPlural("(ax|test)is$", "$1es");
_default.addPlural("(octop|vir|alumn|fung|cact|foc|hippopotam|radi|stimul|syllab|nucle)us$", "$1i");
_default.addPlural("(alias|bias|iris|status|campus|apparatus|virus|walrus|trellis)$", "$1es");
_default.addPlural("(buffal|tomat|volcan|ech|embarg|her|mosquit|potat|torped|vet)o$", "$1oes");
_default.addPlural("([dti])um$", "$1a");
_default.addPlural("sis$", "ses");
_default.addPlural("(?:([^f])fe|([lr])f)$", "$1$2ves");
_default.addPlural("(hive)$", "$1s");
_default.addPlural("([^aeiouy]|qu)y$", "$1ies");
_default.addPlural("(x|ch|ss|sh)$", "$1es");
_default.addPlural("(matr|vert|ind|d)ix|ex$", "$1ices");
_default.addPlural("([m|l])ouse$", "$1ice");
_default.addPlural("^(ox)$", "$1en");
_default.addPlural("(quiz)$", "$1zes");
_default.addPlural("(buz|blit|walt)z$", "$1zes");
_default.addPlural("(hoo|lea|loa|thie)f$", "$1ves");
_default.addPlural("(alumn|alg|larv|vertebr)a$", "$1ae");
_default.addPlural("(criteri|phenomen)on$", "$1a");
_default.addPlural("$", "s");
_default.addPlural("s$", "s");
_default.addPlural("(ax|test)is$", "$1es");
_default.addPlural("(octop|vir|alumn|fung|cact|foc|hippopotam|radi|stimul|syllab|nucle)us$", "$1i");
_default.addPlural("(alias|bias|iris|status|campus|apparatus|virus|walrus|trellis)$", "$1es");
_default.addPlural("(buffal|tomat|volcan|ech|embarg|her|mosquit|potat|torped|vet)o$", "$1oes");
_default.addPlural("([dti])um$", "$1a");
_default.addPlural("sis$", "ses");
_default.addPlural("(?:([^f])fe|([lr])f)$", "$1$2ves");
_default.addPlural("(hive)$", "$1s");
_default.addPlural("([^aeiouy]|qu)y$", "$1ies");
_default.addPlural("(x|ch|ss|sh)$", "$1es");
_default.addPlural("(matr|vert|ind|d)ix|ex$", "$1ices");
_default.addPlural("([m|l])ouse$", "$1ice");
_default.addPlural("^(ox)$", "$1en");
_default.addPlural("(quiz)$", "$1zes");
_default.addPlural("(buz|blit|walt)z$", "$1zes");
_default.addPlural("(hoo|lea|loa|thie)f$", "$1ves");
_default.addPlural("(alumn|alg|larv|vertebr)a$", "$1ae");
_default.addPlural("(criteri|phenomen)on$", "$1a");
_default.addSingular("s$", "");
_default.addSingular("(n)ews$", "$1ews");
_default.addSingular("([dti])a$", "$1um");
_default.addSingular("(analy|ba|diagno|parenthe|progno|synop|the|ellip|empha|neuro|oa|paraly)ses$", "$1sis");
_default.addSingular("([^f])ves$", "$1fe");
_default.addSingular("(hive)s$", "$1");
_default.addSingular("(tive)s$", "$1");
_default.addSingular("([lr]|hoo|lea|loa|thie)ves$", "$1f");
_default.addSingular("(^zomb)?([^aeiouy]|qu)ies$", "$2y");
_default.addSingular("(s)eries$", "$1eries");
_default.addSingular("(m)ovies$", "$1ovie");
_default.addSingular("(x|ch|ss|sh)es$", "$1");
_default.addSingular("([m|l])ice$", "$1ouse");
_default.addSingular("(o)es$", "$1");
_default.addSingular("(shoe)s$", "$1");
_default.addSingular("(cris|ax|test)es$", "$1is");
_default.addSingular("(octop|vir|alumn|fung|cact|foc|hippopotam|radi|stimul|syllab|nucle)i$", "$1us");
_default.addSingular("(alias|bias|iris|status|campus|apparatus|virus|walrus|trellis)es$", "$1");
_default.addSingular("^(ox)en", "$1");
_default.addSingular("(matr|d)ices$", "$1ix");
_default.addSingular("(vert|ind)ices$", "$1ex");
_default.addSingular("(quiz)zes$", "$1");
_default.addSingular("(buz|blit|walt)zes$", "$1z");
_default.addSingular("(alumn|alg|larv|vertebr)ae$", "$1a");
_default.addSingular("(criteri|phenomen)a$", "$1on");
_default.addSingular("([b|r|c]ook|room|smooth)ies$", "$1ie");
_default.addSingular("s$", "");
_default.addSingular("(n)ews$", "$1ews");
_default.addSingular("([dti])a$", "$1um");
_default.addSingular("(analy|ba|diagno|parenthe|progno|synop|the|ellip|empha|neuro|oa|paraly)ses$", "$1sis");
_default.addSingular("([^f])ves$", "$1fe");
_default.addSingular("(hive)s$", "$1");
_default.addSingular("(tive)s$", "$1");
_default.addSingular("([lr]|hoo|lea|loa|thie)ves$", "$1f");
_default.addSingular("(^zomb)?([^aeiouy]|qu)ies$", "$2y");
_default.addSingular("(s)eries$", "$1eries");
_default.addSingular("(m)ovies$", "$1ovie");
_default.addSingular("(x|ch|ss|sh)es$", "$1");
_default.addSingular("([m|l])ice$", "$1ouse");
_default.addSingular("(o)es$", "$1");
_default.addSingular("(shoe)s$", "$1");
_default.addSingular("(cris|ax|test)es$", "$1is");
_default.addSingular("(octop|vir|alumn|fung|cact|foc|hippopotam|radi|stimul|syllab|nucle)i$", "$1us");
_default.addSingular("(alias|bias|iris|status|campus|apparatus|virus|walrus|trellis)es$", "$1");
_default.addSingular("^(ox)en", "$1");
_default.addSingular("(matr|d)ices$", "$1ix");
_default.addSingular("(vert|ind)ices$", "$1ex");
_default.addSingular("(quiz)zes$", "$1");
_default.addSingular("(buz|blit|walt)zes$", "$1z");
_default.addSingular("(alumn|alg|larv|vertebr)ae$", "$1a");
_default.addSingular("(criteri|phenomen)a$", "$1on");
_default.addSingular("([b|r|c]ook|room|smooth)ies$", "$1ie");
_default.addIrregular("person", "people");
_default.addIrregular("man", "men");
_default.addIrregular("human", "humans");
_default.addIrregular("child", "children");
_default.addIrregular("sex", "sexes");
_default.addIrregular("move", "moves");
_default.addIrregular("goose", "geese");
_default.addIrregular("wave", "waves");
_default.addIrregular("die", "dice");
_default.addIrregular("foot", "feet");
_default.addIrregular("tooth", "teeth");
_default.addIrregular("curriculum", "curricula");
_default.addIrregular("database", "databases");
_default.addIrregular("zombie", "zombies");
_default.addIrregular("personnel", "personnel");
//Fix #78a
_default.addIrregular("cache", "caches");
_default.addIrregular("person", "people");
_default.addIrregular("man", "men");
_default.addIrregular("human", "humans");
_default.addIrregular("child", "children");
_default.addIrregular("sex", "sexes");
_default.addIrregular("move", "moves");
_default.addIrregular("goose", "geese");
_default.addIrregular("wave", "waves");
_default.addIrregular("die", "dice");
_default.addIrregular("foot", "feet");
_default.addIrregular("tooth", "teeth");
_default.addIrregular("curriculum", "curricula");
_default.addIrregular("database", "databases");
_default.addIrregular("zombie", "zombies");
_default.addIrregular("personnel", "personnel");
// Fix #78a
_default.addIrregular("cache", "caches");
_default.addIrregular("is", "are", false);
_default.addIrregular("that", "those", false);
_default.addIrregular("this", "these", false);
_default.addIrregular("bus", "buses", false);
_default.addIrregular("staff", "staff", false);
_default.addIrregular("training", "training", false);
_default.addIrregular("is", "are", false);
_default.addIrregular("that", "those", false);
_default.addIrregular("this", "these", false);
_default.addIrregular("bus", "buses", false);
_default.addIrregular("staff", "staff", false);
_default.addIrregular("training", "training", false);
_default.addUncountable("equipment");
_default.addUncountable("information");
_default.addUncountable("corn");
_default.addUncountable("milk");
_default.addUncountable("rice");
_default.addUncountable("money");
_default.addUncountable("species");
_default.addUncountable("series");
_default.addUncountable("fish");
_default.addUncountable("sheep");
_default.addUncountable("deer");
_default.addUncountable("aircraft");
_default.addUncountable("oz");
_default.addUncountable("tsp");
_default.addUncountable("tbsp");
_default.addUncountable("ml");
_default.addUncountable("l");
_default.addUncountable("water");
_default.addUncountable("waters");
_default.addUncountable("semen");
_default.addUncountable("sperm");
_default.addUncountable("bison");
_default.addUncountable("grass");
_default.addUncountable("hair");
_default.addUncountable("mud");
_default.addUncountable("elk");
_default.addUncountable("luggage");
_default.addUncountable("moose");
_default.addUncountable("offspring");
_default.addUncountable("salmon");
_default.addUncountable("shrimp");
_default.addUncountable("someone");
_default.addUncountable("swine");
_default.addUncountable("trout");
_default.addUncountable("tuna");
_default.addUncountable("corps");
_default.addUncountable("scissors");
_default.addUncountable("means");
_default.addUncountable("mail");
_default.addUncountable("equipment");
_default.addUncountable("information");
_default.addUncountable("corn");
_default.addUncountable("milk");
_default.addUncountable("rice");
_default.addUncountable("money");
_default.addUncountable("species");
_default.addUncountable("series");
_default.addUncountable("fish");
_default.addUncountable("sheep");
_default.addUncountable("deer");
_default.addUncountable("aircraft");
_default.addUncountable("oz");
_default.addUncountable("tsp");
_default.addUncountable("tbsp");
_default.addUncountable("ml");
_default.addUncountable("l");
_default.addUncountable("water");
_default.addUncountable("waters");
_default.addUncountable("semen");
_default.addUncountable("sperm");
_default.addUncountable("bison");
_default.addUncountable("grass");
_default.addUncountable("hair");
_default.addUncountable("mud");
_default.addUncountable("elk");
_default.addUncountable("luggage");
_default.addUncountable("moose");
_default.addUncountable("offspring");
_default.addUncountable("salmon");
_default.addUncountable("shrimp");
_default.addUncountable("someone");
_default.addUncountable("swine");
_default.addUncountable("trout");
_default.addUncountable("tuna");
_default.addUncountable("corps");
_default.addUncountable("scissors");
_default.addUncountable("means");
_default.addUncountable("mail");
return _default;
}
}
return _default;
}
}

@@ -17,3 +17,4 @@ /// <summary>

/// <param name="plural">The plural form of the irregular word, e.g. "people".</param>
/// <param name="matchEnding">True to match these words on their own as well as at the end of longer words. False, otherwise.</param>
/// <param name="matchEnding">True to match these words on their own as well
/// as at the end of longer words. False, otherwise.</param>
public addIrregular(singular: string, plural: string, matchEnding: boolean = true): void {

@@ -23,4 +24,3 @@ if (matchEnding) {

this.addSingular("(" + plural[0] + ")" + plural.substring(1) + "$", "$1" + singular.substring(1));
}
else {
} else {
this.addPlural(`^${singular}$`, plural);

@@ -61,6 +61,7 @@ this.addSingular(`^${plural}$`, singular);

/// <param name="word">Word to be pluralized</param>
/// <param name="inputIsKnownToBeSingular">Normally you call Pluralize on singular words; but if you're unsure call it with false</param>
/// <param name="inputIsKnownToBeSingular">Normally you call Pluralize on singular words;
/// but if you're unsure call it with false</param>
/// <returns></returns>
public pluralize(word: string, inputIsKnownToBeSingular: boolean = true): string {
var result = this.applyRules(this._plurals, word, false);
const result = this.applyRules(this._plurals, word, false);

@@ -71,5 +72,5 @@ if (inputIsKnownToBeSingular) {

var asSingular = this.applyRules(this._singulars, word, false);
var asSingularAsPlural = this.applyRules(this._plurals, asSingular, false);
if (asSingular != null && asSingular != word && asSingular + "s" != word && asSingularAsPlural == word && result != word) {
const asSingular = this.applyRules(this._singulars, word, false);
const asSingularAsPlural = this.applyRules(this._plurals, asSingular, false);
if (asSingular !== "" && asSingular !== word && asSingular + "s" !== word && asSingularAsPlural === word && result !== word) {
return word;

@@ -85,8 +86,9 @@ }

/// <param name="word">Word to be singularized</param>
/// <param name="inputIsKnownToBePlural">Normally you call Singularize on plural words; but if you're unsure call it with false</param>
/// <param name="inputIsKnownToBePlural">Normally you call Singularize on plural words; but if you're unsure
/// call it with false</param>
/// <param name="skipSimpleWords">Skip singularizing single words that have an 's' on the end</param>
/// <returns></returns>
public singularize(word: string, inputIsKnownToBePlural: boolean = true, skipSimpleWords: boolean = false): string {
var result = this.applyRules(this._singulars, word, skipSimpleWords);
const result = this.applyRules(this._singulars, word, skipSimpleWords);
if (inputIsKnownToBePlural) {

@@ -97,9 +99,9 @@ return result ?? word;

// the Plurality is unknown so we should check all possibilities
var asPlural = this.applyRules(this._plurals, word, false);
var asPluralAsSingular = this.applyRules(this._singulars, asPlural, false);
if (asPlural != word && word + "s" != asPlural && asPluralAsSingular == word && result != word) {
const asPlural = this.applyRules(this._plurals, word, false);
const asPluralAsSingular = this.applyRules(this._singulars, asPlural, false);
if (asPlural !== word && word + "s" !== asPlural && asPluralAsSingular === word && result !== word) {
return word;
}
return result ?? word;
return result === "" ? word : result;
}

@@ -109,2 +111,3 @@

if (word == null) {
// @ts-ignore
return null;

@@ -117,6 +120,6 @@ }

var result = word;
var end = skipFirstRule ? 1 : 0;
for (var i = rules.length - 1; i >= end; i--) {
if ((result = rules[i].apply(word)) != null) {
let result = word;
const end = skipFirstRule ? 1 : 0;
for (let i = rules.length - 1; i >= end; i--) {
if ((result = rules[i].apply(word)) !== "") {
break;

@@ -138,3 +141,3 @@ }

public constructor(pattern: string, replacement: string) {
this._regex = new RegExp(pattern, 'i');
this._regex = new RegExp(pattern, "i");
this._replacement = replacement;

@@ -145,3 +148,3 @@ }

if (!this._regex.test(word)) {
return null;
return "";
}

@@ -148,0 +151,0 @@ return word.replace(this._regex, this._replacement)

@@ -1,3 +0,2 @@

export class EnglishNumberToWordsConverter
{
export class EnglishNumberToWordsConverter {
private static readonly UnitsMap = [ "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" ];

@@ -18,24 +17,19 @@ private static readonly TensMap = [ "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" ];

static convertToOrdinal(number: number): string
{
static convertToOrdinal(number: number): string {
return EnglishNumberToWordsConverter.convert(number, true);
}
static convert(number: number, isOrdinal: boolean = false): string
{
static convert(number: number, isOrdinal: boolean = false): string {
number = Math.floor(number)
if (number == 0)
{
if (number === 0) {
return EnglishNumberToWordsConverter.getUnitValue(0, isOrdinal);
}
if (number < 0)
{
if (number < 0) {
return `minus ${EnglishNumberToWordsConverter.convert(-number)}`
}
var parts = new Array<string>();
const parts = new Array<string>();
if (Math.floor(number / 1000000000000000000) > 0)
{
if (Math.floor(number / 1000000000000000000) > 0) {
parts.push(`${EnglishNumberToWordsConverter.convert(number / 1000000000000000000)} quintillion`)

@@ -45,4 +39,3 @@ number %= 1000000000000000000;

if (Math.floor(number / 1000000000000000) > 0)
{
if (Math.floor(number / 1000000000000000) > 0) {
parts.push(`${EnglishNumberToWordsConverter.convert(number / 1000000000000000)} quadrillion`);

@@ -52,4 +45,3 @@ number %= 1000000000000000;

if (Math.floor(number / 1000000000000) > 0)
{
if (Math.floor(number / 1000000000000) > 0) {
parts.push(`${EnglishNumberToWordsConverter.convert(number / 1000000000000)} trillion`)

@@ -59,4 +51,3 @@ number %= 1000000000000;

if (Math.floor(number / 1000000000) > 0)
{
if (Math.floor(number / 1000000000) > 0) {
parts.push(`${EnglishNumberToWordsConverter.convert(number / 1000000000)} billion`);

@@ -66,4 +57,3 @@ number %= 1000000000;

if (Math.floor(number / 1000000) > 0)
{
if (Math.floor(number / 1000000) > 0) {
parts.push(`${EnglishNumberToWordsConverter.convert(number / 1000000)} million`);

@@ -73,4 +63,3 @@ number %= 1000000;

if (Math.floor(number / 1000) > 0)
{
if (Math.floor(number / 1000) > 0) {
parts.push(`${EnglishNumberToWordsConverter.convert(number / 1000)} thousand`);

@@ -80,4 +69,3 @@ number %= 1000;

if (Math.floor(number / 100) > 0)
{
if (Math.floor(number / 100) > 0) {
parts.push(`${EnglishNumberToWordsConverter.convert(number / 100)} hundred`);

@@ -87,23 +75,15 @@ number %= 100;

if (Math.floor(number) > 0)
{
if (parts.length != 0)
{
if (Math.floor(number) > 0) {
if (parts.length !== 0) {
parts.push("and");
}
if (number < 20)
{
if (number < 20) {
parts.push(EnglishNumberToWordsConverter.getUnitValue(number, isOrdinal));
}
else
{
var lastPart = EnglishNumberToWordsConverter.TensMap[Math.floor(number / 10)];
if ((number % 10) > 0)
{
} else {
let lastPart = EnglishNumberToWordsConverter.TensMap[Math.floor(number / 10)];
if ((number % 10) > 0) {
lastPart += `-${EnglishNumberToWordsConverter.getUnitValue(number % 10, isOrdinal)}`;
}
else if (isOrdinal)
{
let trimmed = lastPart.replace(/\y+$/g, ''); //lastPart.trimEnd('y')
} else if (isOrdinal) {
const trimmed = lastPart.replace(/\y+$/g, ""); // lastPart.trimEnd('y')
lastPart = trimmed + "ieth";

@@ -114,12 +94,9 @@ }

}
}
else if (isOrdinal)
{
} else if (isOrdinal) {
parts[parts.length - 1] += "th";
}
var toWords = parts.join(" ");
let toWords = parts.join(" ");
if (isOrdinal)
{
if (isOrdinal) {
toWords = EnglishNumberToWordsConverter.removeOnePrefix(toWords);

@@ -131,18 +108,11 @@ }

private static getUnitValue(number: number, isOrdinal: boolean): string
{
if (isOrdinal)
{
let exceptionString = EnglishNumberToWordsConverter.exceptionNumbersToWords(number)
if (exceptionString != "NOTFOUND")
{
private static getUnitValue(number: number, isOrdinal: boolean): string {
if (isOrdinal) {
const exceptionString = EnglishNumberToWordsConverter.exceptionNumbersToWords(number)
if (exceptionString !== "NOTFOUND") {
return exceptionString;
}
else
{
} else {
return EnglishNumberToWordsConverter.UnitsMap[number] + "th";
}
}
else
{
} else {
return EnglishNumberToWordsConverter.UnitsMap[number];

@@ -152,8 +122,6 @@ }

private static removeOnePrefix(toWords: string): string
{
private static removeOnePrefix(toWords: string): string {
// one hundred => hundredth
if (toWords.indexOf("one") == 0)
{
toWords = toWords.substring(4); //toWords.remove(0, 4);
if (toWords.indexOf("one") === 0) {
toWords = toWords.substring(4); // toWords.remove(0, 4);
}

@@ -164,6 +132,6 @@

private static exceptionNumbersToWords(number: number): string
{
private static exceptionNumbersToWords(number: number): string {
let rtnVal = "NOTFOUND";
EnglishNumberToWordsConverter.OrdinalExceptions.forEach(element => {
// tslint:disable-next-line
if (element[0] == number) {

@@ -175,2 +143,2 @@ rtnVal = element[1].toString()

}
}
}
let assert = require('assert');
require('../dist/numberToWords')
require('../dist/byteSize')
require('../dist')
describe('NumberToWords', function() {
describe('.toWords()', function() {
it('Trying (number).toWord', function() {
describe('ByteSize', function() {
const fileSize = (10).kilobytes()
describe('.bits()', function() {
it('Trying (number).bits()', function() {
assert.equal(fileSize.bits, 81920);
});
});
describe('.bytes()', function() {
it('Trying (number).bytes()', function() {
assert.equal(fileSize.bytes, 10240);
});
});
describe('.kilobytes()', function() {
it('Trying (number).kilobytes()', function() {
assert.equal(fileSize.kilobytes, 10);
});
});
describe('.megabytes()', function() {
it('Trying (number).megabytes()', function() {
assert.equal(fileSize.megabytes, 0.009765625);
});
});
describe('.gigabytes()', function() {
it('Trying (number).gigabytes()', function() {
assert.equal(fileSize.gigabytes, 0.0000095367431640625);
});
});
describe('.terabytes()', function() {
it('Trying (number).terabytes()', function() {
assert.equal(fileSize.terabytes, 9.313225746154785e-9);
});
});
});
describe('vocabularies', function() {
describe('.pluralize()', function() {
it('Trying (string).pluralize()', function() {
assert.equal("Man".pluralize(), "Men");
assert.equal("string".pluralize(), "strings");
});
});
});
describe('Ordinalize', function() {
describe('.ordinalize()', function() {
it('Trying (string).ordinalize()', function() {
assert.equal((1).ordinalize(), "1st");
assert.equal((5).ordinalize(), "5th");
});
});
});
describe('ToQuantity', function() {
describe('.toQuantity()', function() {
it('Trying (string).toQuantity()', function() {
assert.equal("case".toQuantity(0), "0 cases");
assert.equal("case".toQuantity(1), "1 case");
assert.equal("case".toQuantity(5), "5 cases");
assert.equal("man".toQuantity(2), "2 men");
assert.equal("case".toQuantity(5, 2), "five cases");
});
});
});
describe('Number to words', function() {
describe('.numberToWords()', function() {
it('Trying (string).numberToWords()', function() {
assert.equal((1).toWords(), "one");
assert.equal((10).toWords(), "ten");
assert.equal((11).toWords(), "eleven");
assert.equal((122).toWords(), "one hundred and twenty-two");
assert.equal((3501).toWords(), "three thousand five hundred and one");
});

@@ -14,8 +81,11 @@ });

describe('ByteSize', function() {
describe('.megabytes()', function() {
it('Trying (number).megabytes()', function() {
assert.equal((10).megabytes().toString(), "10 MB");
describe('Number to ordinal words', function() {
describe('.numberToWords()', function() {
it('Trying (string).numberToWords()', function() {
assert.equal((1).toOrdinalWords(), "first");
assert.equal((10).toOrdinalWords(), "tenth");
assert.equal((11).toOrdinalWords(), "eleventh");
assert.equal((121).toOrdinalWords(), "hundred and twenty-first");
});
});
});
});

@@ -8,3 +8,3 @@

interface String {
toQuantity(quantity: number, showQuantityAs: ShowQuantityAs): string
toQuantity(quantity: number, showQuantityAs?: ShowQuantityAs): string
}

@@ -38,11 +38,11 @@ }

String.prototype.toQuantity = function (quantity: number, showQuantityAs: ShowQuantityAs = ShowQuantityAs.Numeric): string {
let transformedInput = quantity == 1
? Vocabularies.Default().singularize(this, false)
: Vocabularies.Default().pluralize(this, false)
const transformedInput = quantity === 1
? Vocabularies.Default().singularize(this as string, false)
: Vocabularies.Default().pluralize(this as string, false)
if (showQuantityAs == ShowQuantityAs.None) {
if (showQuantityAs === ShowQuantityAs.None) {
return transformedInput;
}
if (showQuantityAs == ShowQuantityAs.Numeric) {
if (showQuantityAs === ShowQuantityAs.Numeric) {
return `${quantity} ${transformedInput}`

@@ -49,0 +49,0 @@ }

@@ -13,9 +13,9 @@

String.prototype.pluralize = function (): string {
return Vocabularies.Default().pluralize(this)
return Vocabularies.Default().pluralize(this as string)
}
String.prototype.singularize = function (): string {
return Vocabularies.Default().singularize(this)
return Vocabularies.Default().singularize(this as string)
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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