Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

comma separated values and documentation #818

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion dist/ToWords.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,35 @@ export declare class ToWords {
private options;
private locale;
constructor(options?: ToWordsOptions);
/**
* Get locale class for the locale passed in the options.
* @returns {class} - based on selected currency
*/
getLocaleClass(): ConstructorOf<LocaleInterface>;
/**
* Create instance of the locale class.
*
* @returns {class}
*/
getLocale(): InstanceType<ConstructorOf<LocaleInterface>>;
convert(number: number, options?: ConverterOptions): string;
/**
*
* @param number - The number to be converted into the words.
* @param options - Converter Options object.
* @returns {string} - converted number to words
*/
convert(number: number | string, options?: ConverterOptions): string;
/**
*
* @param number
* @returns {Array<string>} converted words as array of strings
*/
protected convertNumber(number: number): string[];
protected convertCurrency(number: number, options?: ConverterOptions): string[];
protected convertInternal(number: number): string[];
toFixed(number: number, precision?: number): number;
isFloat(number: number | string): boolean;
isValidNumber(number: number | string): boolean;
isNumberZero(number: number): boolean;
clean(value: string | number): number;
}
86 changes: 86 additions & 0 deletions dist/ToWords.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,52 +35,129 @@ class ToWords {
this.locale = undefined;
this.options = Object.assign({}, exports.DefaultToWordsOptions, options);
}
/**
* Get locale class for the locale passed in the options.
* @returns {class} - based on selected currency
*/
getLocaleClass() {
/* eslint-disable @typescript-eslint/no-var-requires */
switch (this.options.localeCode) {
case 'en-BD':
return en_BD_1.default;
/**
* Instantiating the passed user currency local option in configuration
* object
* @returns {class}
*/
case 'en-GH':
return en_GH_1.default;
case 'en-IN':
return en_IN_1.default;
case 'en-MM':
return en_MM_1.default;
case 'en-MU':
/**
*
* @param number - user input number to be converted
* @param options - configuration set by user
* @returns {string} - converted number to words string
*/
return en_MU_1.default;
/**
* Instantiating the passed user currency local option in configuration
* object
* @returns {class}
*/
case 'en-NG':
return en_NG_1.default;
case 'en-US':
return en_US_1.default;
case 'en-GB':
return en_GB_1.default;
case 'fa-IR':
/**
*
* @param number - user input number to be converted
* @param options - configuration set by user
/**
*
* @param number
* @returns {Array<string>} converted words as array of strings
*/
* @returns {string} - converted number to words string
*/
return fa_IR_1.default;
/**
* Instantiating the passed user currency local option in configuration
* object
* @returns {class}
*/
case 'fr-FR':
return fr_FR_1.default;
case 'gu-IN':
return gu_IN_1.default;
case 'hi-IN':
return hi_IN_1.default;
case 'mr-IN':
/**
*
* @param number - user input number to be converted
* @param options - configuration set by user
/**
*
* @param number
* @returns {Array<string>} converted words as array of strings
*/
* @returns {string} - converted number to words string
*/
return mr_IN_1.default;
/**
* Instantiating the passed user currency local option in configuration
* object
* @returns {class}
*/
case 'tr-TR':
return tr_TR_1.default;
case 'nl-SR':
return nl_SR_1.default;
}
/* eslint-enable @typescript-eslint/no-var-requires */
throw new Error(`Unknown Locale "${this.options.localeCode}"`);
/**
*
* @param number - user input number to be converted
* @param options - configuration set by user
/**
*
* @param number
* @returns {Array<string>} converted words as array of strings
*/
* @returns {string} - converted number to words string
*/
}
/**
* Create instance of the locale class.
*
* @returns {class}
*/
getLocale() {
if (this.locale === undefined) {
const LocaleClass = this.getLocaleClass();
this.locale = new LocaleClass();
}
return this.locale;
}
/**
*
* @param number - The number to be converted into the words.
* @param options - Converter Options object.
* @returns {string} - converted number to words
*/
convert(number, options = {}) {
options = Object.assign({}, this.options.converterOptions, options);
//check type of the user input and replace any commas
// and convert the input to number if string
number = this.clean(number);
if (!this.isValidNumber(number)) {
throw new Error(`Invalid Number "${number}"`);
}
Expand All @@ -96,6 +173,11 @@ class ToWords {
}
return words.join(' ');
}
/**
*
* @param number
* @returns {Array<string>} converted words as array of strings
*/
convertNumber(number) {
var _a, _b, _c;
const locale = this.getLocale();
Expand Down Expand Up @@ -242,5 +324,9 @@ class ToWords {
isNumberZero(number) {
return number >= 0 && number < 1;
}
// replace the commas and convert to number
clean(value) {
return typeof value === 'string' ? Number(value.toString().replace(/,/g, '')) : value;
}
}
exports.ToWords = ToWords;
Loading