UNPKG

transcend-charts

Version:

Transcend is a charting and graph library for NUVI

131 lines (115 loc) 3.88 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var isNum = /\-?([0-9]*\.)?[0-9]+/; var nonDigits = /[^0-9\.\-]/; exports.default = { makeNumber: function makeNumber(inputString, allowBlank, precision) { var precisionSelected = true; if (precision == null) { precision = 1; precisionSelected = false; } // remove any non-digit, non-decimal point characters if (typeof inputString == 'string') { inputString = inputString.replace(nonDigits, ""); // if it's still not a number, there must be multiple decimal places if (!isNum.test(inputString)) { // find the index of the second decimal point and throw everything away after it var decimalIndex = inputString.indexOf('.'); var decimalIndex2 = inputString.indexOf('.', decimalIndex + 1); inputString = inputString.substring(0, decimalIndex2); } } if (!allowBlank && inputString == "") { if (precision == 0) { inputString = "0"; } else { inputString = "0."; for (var i = 0; i < precision; i++) { inputString += "0"; } } } if (precisionSelected) { inputString = Number(inputString).toFixed(precision); } return Number(inputString); }, formatNumber: function formatNumber(num, precision, abbreviate) { if (num == 0) { return '0'; } num = this.makeNumber(String(num), false, precision); var str = String(num); if (abbreviate) { // add order of magnitude abbreviations var magnitudes = [{ magnitude: 1000000000000, label: 'T' }, { magnitude: 1000000000, label: 'B' }, { magnitude: 1000000, label: 'M' }, { magnitude: 1000, label: 'K' }]; for (var i = 0; i < magnitudes.length; i++) { if (Math.abs(num) >= magnitudes[i].magnitude) { str = (num / magnitudes[i].magnitude).toFixed(precision); if (str.indexOf('.0') != -1) { str = str.substring(0, str.indexOf('.0')); } str += magnitudes[i].label; break; } } } else { // add commas var pos = str.indexOf('.'); var prefix = str; var suffix = ''; if (pos >= 0) { prefix = str.substring(0, pos); suffix = str.substring(pos + 1); if (suffix != '') { suffix = '.' + suffix; } } for (var i = prefix.length - 3; i > 0; i -= 3) { prefix = prefix.substring(0, i) + ',' + prefix.substring(i); } str = prefix + suffix; } return str; }, formatNumberAsPct: function formatNumberAsPct(num, precision, abbreviate) { var newNumber = this.formatNumber(num * 100, precision, abbreviate); if (!newNumber.length) { newNumber = '0'; } return newNumber + '%'; }, /** * Accepts a numeric value which may have a prefix (like $) and/or a suffix (like %), * separates those three parts and returns them **/ separateNumberUnits: function separateNumberUnits(mixedValue) { var parts = {}; if (typeof mixedValue === 'number') { parts.value = mixedValue; parts.prefix = ''; parts.suffix = ''; } else if (typeof mixedValue === 'string') { parts.prefix = mixedValue.replace(/^([^0-9\.]*)[0-9\.]+(.*)$/, '$1'); parts.suffix = mixedValue.replace(/^([^0-9\.]*)[0-9\.]+([^0-9\.]*)$/, '$2'); parts.value = this.makeNumber(mixedValue); } return parts; }, max: function max(array, compareFunc) { var currentMaxItem; array.forEach(function (item) { if (currentMaxItem === undefined) { currentMaxItem = item; } else if (compareFunc && compareFunc(item, currentMaxItem) > 0) { currentMaxItem = item; } else if (item > currentMaxItem) { currentMaxItem = item; } }); return currentMaxItem; } };