UNPKG

@formant/ava

Version:

A framework for automated visual analytics.

36 lines (35 loc) 1.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * data format. * dataFormat(0.8849) = 0.88 * dataFormat(12, 1) = 12 * dataFormat(1234, 1) = 1.2k * dataFormat(123.456, 2) = 123.46 * @param value value to be formatted * @param digits the number of digits to keep after the decimal point, 2 by default * @returns formatted value string */ function dataFormat(value, digits) { if (digits === void 0) { digits = 2; } if (typeof value === 'string') return value; var formatMap = [ { value: 1, symbol: '' }, { value: 1e3, symbol: 'k' }, { value: 1e6, symbol: 'M' }, { value: 1e9, symbol: 'G' }, { value: 1e12, symbol: 'T' }, { value: 1e15, symbol: 'P' }, { value: 1e18, symbol: 'E' }, ]; var rx = /\.0+$|(\.[0-9]*[1-9])0+$/; var item = formatMap .slice() .reverse() .find(function (item) { return value >= item.value; }); return item ? (value / item.value).toFixed(digits).replace(rx, '$1') + item.symbol : value.toFixed(digits); } exports.default = dataFormat;