UNPKG

@barchart/common-js

Version:
62 lines (60 loc) 2.05 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var formatter_exports = {}; __export(formatter_exports, { numberToString: () => numberToString }); module.exports = __toCommonJS(formatter_exports); function numberToString(value, digits, thousandsSeparator, useParenthesis) { if (value === void 0 || value === null || Number.isNaN(value)) { return ""; } const applyParenthesis = value < 0 && useParenthesis === true; if (applyParenthesis) { value = 0 - value; } let returnRef = value.toFixed(digits); if (thousandsSeparator && !(value > -1e3 && value < 1e3)) { const length = returnRef.length; const negative = value < 0; let found = digits === 0; let counter = 0; const buffer = []; for (let i = length - 1; !(i < 0); i--) { if (counter === 3 && !(negative && i === 0)) { buffer.unshift(thousandsSeparator); counter = 0; } const character = returnRef.charAt(i); buffer.unshift(character); if (found) { counter = counter + 1; } else if (character === ".") { found = true; } } if (applyParenthesis) { buffer.unshift("("); buffer.push(")"); } returnRef = buffer.join(""); } else if (applyParenthesis) { returnRef = "(" + returnRef + ")"; } return returnRef; }