All files / src index.ts

84.24% Statements 171/203
65.38% Branches 51/78
100% Functions 7/7
84.24% Lines 171/203

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 2031x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x 1x 1x 243x 243x 80x 243x 16x 243x 243x 16x 16x 16x 1x 1x 163x 57x 163x 163x 163x 163x 57x 57x 57x 163x 3x   3x 3x       54x 54x 54x 163x             1x 1x 106x 69x 69x 69x 106x                     69x 69x 69x 69x 69x 69x 106x 106x 106x 106x 106x 69x 69x 69x 69x 106x 106x 69x 106x 106x 106x 106x 1x 1x 249x 249x 249x 249x 249x 243x 243x 243x 249x 249x 249x 37x 37x 37x 37x 37x 37x 37x 37x 249x 24x 24x 249x 12x 12x 13x 1x 1x 1x 37x 37x 37x 37x 37x 249x 36x 36x 36x 5x 5x 36x 36x 36x 1x 1x 1x 1x 1x 14x 14x 14x 14x 14x 14x 14x 14x 1x 1x 14x 14x 14x 14x     14x     14x     14x 14x 14x 14x     14x     14x     14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 1x
import {ITranslation} from "./interfaces/ITranslation";
 
import {English} from "./i18n/en";
import {Bulgarian} from "./i18n/bg";
import {Portuguese} from "./i18n/pt";
import {SpanishLarga} from "./i18n/es-larga";
import {SpanishCorto} from "./i18n/es-corto";
import {IOptions} from "./interfaces/IOptions";
import {DEFAULT_OPTIONS, ERROR} from "./constants";
 
export enum Language {
    EN = "en",
    BG = "bg",
    PT = "pt",
    ES = "es",
    ESLarga = "es-larga",
    ESCorto = "es-corto",
}
 
export class Wordify {
    protected number = "";
    protected firstIteration = true;
    protected lang: ITranslation = English;
    protected options: IOptions = DEFAULT_OPTIONS;
 
    protected hasInUnits(n: string): string {
        const response = this.lang.units[n]
        if(!response) return ''
 
        if (typeof response === 'string') return response;
 
        if (!this.options.gender)
            throw new Error(ERROR.INVALID_GENDER)
 
        return response[this.options.gender]!;
    }
 
    protected isLessThan100(n: string): string {
        if (Number(n) >= 100) return ""
 
        const separator = this.lang.unitSeparator ?? this.lang.separator ?? " ";
        const tenths = this.lang.tenths[n[0].padEnd(2, '0')];
 
        if (!tenths) throw new Error(ERROR.MISSING_PROPERTY)
 
        const rest = this.convert(n.substr(1));
 
        if (!rest) {
            if (typeof tenths === 'string') return tenths

            if (this.options.gender && tenths[this.options.gender])
                return tenths[this.options.gender]!

            throw new Error(ERROR.MISSING_GENDER)
        }
 
        if (typeof tenths === 'string') {
            return tenths + separator + rest
        } else {
            if (this.options.gender)
                return tenths[this.options.gender]!
        }

        return ""
    }
 
    protected isLessThan1000(n: string): string {
        if (Number(n) >= 1000) return ""
 
        let hundreds = this.lang.hundreds[n[0].padEnd(3, '0')];
 
        if (!hundreds) {
            let unity = this.lang.units[n[0]];
            const isBiggerThanOne = Number(n[0]) > 1;
            unity = typeof unity === 'string' ? unity : unity[this.options.gender]!

            const findElement = this.lang.other.find(o => o.exponent === 2)!;
            const result = findElement.all ?? (isBiggerThanOne ? findElement.plural : findElement.singular);
            const hundred = typeof result === 'string' ? result : result![this.options.gender];

            hundreds = `${unity} ${hundred}`
        }
        const rest = n.substr(1);
        const overflow = Number(rest);
 
        const result = this.convert(rest)
 
        let separator = " ";
        const definedSeparator = this.lang.orderSeparator ?? this.lang.separator;
 
        let wordsSoFar: string[];
 
        if (definedSeparator && !this.lang.ignoreHundredsSeparator)
            wordsSoFar = result.replace(new RegExp(definedSeparator, 'i'), "").split(" ")
        else
            wordsSoFar = result.split(" ")
 
        if (wordsSoFar.length === 1 && !!wordsSoFar[0])
            separator = this.lang.orderSeparator ?? this.lang.separator ?? " "
 
        if (overflow > 0)
            return (typeof hundreds === 'string' ? hundreds : hundreds[this.options.gender]!) + separator + result
        return (typeof hundreds === 'string' ? hundreds : hundreds[this.options.gender]!)
    }
 
    protected convert(n: string): string {
        const number = Number(n);
        const length = n.length;
        n = number.toString();
 
        if (!this.firstIteration && n === '0') return '';
        this.firstIteration = false
 
        let res = '';
        if (res = this.hasInUnits(n)) return res
        if (res = this.isLessThan100(n)) return res
        if (res = this.isLessThan1000(n)) return res
 
        const sorted = this.lang.other.sort((a, b) => a.exponent - b.exponent)
        const orderAbove = sorted.findIndex(o => o.exponent >= length)
        const order = sorted[orderAbove - 1];
 
        const multiplier = n.substr(0, n.length - order.exponent);
        let result = '';
 
        if (order.all) {
            if (typeof order.all === 'string') result = order.all
            else result = order.all[this.options.gender]!
        } else if (Number(multiplier) > 1) {
            if (typeof order.plural === 'string') result = order.plural
            else if (order.plural) result = order.plural[this.options.gender]!
        } else {
            if (typeof order.singular === 'string') result = order.singular
            else if (order.singular) result = order.singular[this.options.gender]!
        }
 
        let separator = " "
        const other = n.substr(multiplier.length);
        const overflow = Number(other);
 
        if (overflow > 0) {
            const rest = this.convert(other)
 
            if ((overflow < Math.pow(10, order.exponent) && overflow % Math.pow(10, order.exponent - 1) === 0) || rest.split(" ").length === 1) {
                separator = this.lang.orderSeparator ?? this.lang.separator ?? " "
            }
 
            return `${this.convert(multiplier)} ${result}${separator}${this.convert(other)}`
        }
 
        return `${this.convert(multiplier)}${separator}${result}`
    }
 
    public static from(number: string | number): Wordify {
        if (Number.isNaN(number))
            throw new Error('Input is not a number')
 
        const instance = new Wordify()
        instance.number = number.toString().trim()
 
        return instance
    }
 
    public toWords(language: Language = Language.EN, options?: IOptions): string {
        if (options) this.options = options
 
        switch (language) {
            case Language.BG:
                this.lang = Bulgarian;
                break;
            case Language.EN:
                this.lang = English;
                break;
            case Language.PT:
                this.lang = Portuguese;
                break;
            case Language.ESLarga:
                this.lang = SpanishLarga;
                break;
            case Language.ESCorto:
                this.lang = SpanishCorto;
                break;
            case Language.ES:
                this.lang = SpanishCorto;
                break;
            default:
                this.lang = English
                break;
        }
 
        let value = this.convert(this.number)
 
        if (this.lang.exceptions && this.lang.exceptions.length > 0) {
            this.lang.exceptions
                .filter(e => e.type === 'post')
                .forEach(e => {
                    value = e.func(value, Number(this.number))
                })
        }
 
        return value
    }
}