function millify(value, options) {
    var _a;
    // Override default options with options supplied by user.
    const opts = options
        ? Object.assign(Object.assign({}, options_1.defaultOptions), options) : options_1.defaultOptions;
    if (!Array.isArray(opts.units) || !opts.units.length) {
        throw new Error("Option `units` must be a non-empty array");
    }
    // Validate value for type and length.
    let val = utils_1.parseValue(value);
    // Add a minus sign (-) prefix if it's a negative number.
    const prefix = val < 0 ? "-" : "";
    // Work only with positive values for simplicity's sake.
    val = Math.abs(val);
    // Keep dividing the input value by the digit grouping base
    // until the decimal and the unit index is deciphered.
    let unitIndex = 0;
    for (const result of divider(val)) {
        val = result;
        unitIndex += 1;
    }
    // Return the original number if the number is too large to have
    // a corresponding unit. Returning anything else is ambiguous.
    const unitIndexOutOfRange = unitIndex >= opts.units.length;
    if (unitIndexOutOfRange) {
        return value.toString();
    }
    // Round decimal up to desired precision.
    let rounded = utils_1.roundTo(val, opts.precision);
    // Fixes an edge case bug that outputs certain numbers as 1000K instead of 1M.
    // The rounded value needs another iteration in the divider cycle.
    for (const result of divider(rounded)) {
        rounded = result;
        unitIndex += 1;
    }
    // Calculate the unit suffix and make it lowercase (if needed).
    const unit = (_a = opts.units[unitIndex]) !== null && _a !== void 0 ? _a : "";
    const suffix = opts.lowercase ? unit.toLowerCase() : unit;
    // Add a space between number and abbreviation.
    const space = opts.space ? " " : "";
    // Replace decimal mark if desired.
    const formatted = rounded
        .toString()
        .replace(options_1.defaultOptions.decimalSeparator, opts.decimalSeparator);
    return `${prefix}${formatted}${space}${suffix}`;
}
/**
 * millify converts long numbers to human-readable strings.
 */
millify converts long numbers to human-readable strings.
millify converts long numbers to human - readable strings .
function millify(value, options) {
    var _a;
    const opts = options
        ? Object.assign(Object.assign({}, options_1.defaultOptions), options) : options_1.defaultOptions;
    if (!Array.isArray(opts.units) || !opts.units.length) {
        throw new Error("Option `units` must be a non-empty array");
    }
    let val = utils_1.parseValue(value);
    const prefix = val < 0 ? "-" : "";
    val = Math.abs(val);
    let unitIndex = 0;
    for (const result of divider(val)) {
        val = result;
        unitIndex += 1;
    }
    const unitIndexOutOfRange = unitIndex >= opts.units.length;
    if (unitIndexOutOfRange) {
        return value.toString();
    }
    let rounded = utils_1.roundTo(val, opts.precision);
    for (const result of divider(rounded)) {
        rounded = result;
        unitIndex += 1;
    }
    const unit = (_a = opts.units[unitIndex]) !== null && _a !== void 0 ? _a : "";
    const suffix = opts.lowercase ? unit.toLowerCase() : unit;
    const space = opts.space ? " " : "";
    const formatted = rounded
        .toString()
        .replace(options_1.defaultOptions.decimalSeparator, opts.decimalSeparator);
    return `${prefix}${formatted}${space}${suffix}`;
}
function millify ( value , options ) { var _a ; const opts = options ? Object . assign ( Object . assign ( { } , options_1 . defaultOptions ) , options ) : options_1 . defaultOptions ; if ( ! Array . isArray ( opts . units ) || ! opts . units . length ) { throw new Error ( "Option `units` must be a non-empty array" ) ; } let val = utils_1 . parseValue ( value ) ; const prefix = val < 0 ? "-" : "" ; val = Math . abs ( val ) ; let unitIndex = 0 ; for ( const result of divider ( val ) ) { val = result ; unitIndex += 1 ; } const unitIndexOutOfRange = unitIndex >= opts . units . length ; if ( unitIndexOutOfRange ) { return value . toString ( ) ; } let rounded = utils_1 . roundTo ( val , opts . precision ) ; for ( const result of divider ( rounded ) ) { rounded = result ; unitIndex += 1 ; } const unit = ( _a = opts . units [ unitIndex ] ) !== null && _a !== void 0 ? _a : "" ; const suffix = opts . lowercase ? unit . toLowerCase ( ) : unit ; const space = opts . space ? " " : "" ; const formatted = rounded . toString ( ) . replace ( options_1 . defaultOptions . decimalSeparator , opts . decimalSeparator ) ; return ` ${ prefix } ${ formatted } ${ space } ${ suffix } ` ; }
millify converts long numbers to human-readable strings.
millify converts long numbers to human - readable strings .
// Override default options with options supplied by user. // Validate value for type and length. // Add a minus sign (-) prefix if it's a negative number. // Work only with positive values for simplicity's sake. // Keep dividing the input value by the digit grouping base // until the decimal and the unit index is deciphered. // Return the original number if the number is too large to have // a corresponding unit. Returning anything else is ambiguous. // Round decimal up to desired precision. // Fixes an edge case bug that outputs certain numbers as 1000K instead of 1M. // The rounded value needs another iteration in the divider cycle. // Calculate the unit suffix and make it lowercase (if needed). // Add a space between number and abbreviation. // Replace decimal mark if desired.