UNPKG

svelte-ux

Version:

- Increment version in `package.json` and commit as `Version bump to x.y.z` - `npm run publish`

85 lines (84 loc) 2.68 kB
import { format as d3Format } from 'd3-format'; // See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat export function formatNumber(number, options = {}) { if (number == null) { return ''; } const formatter = Intl.NumberFormat(undefined, { ...(options.currency != null && { style: 'currency', }), ...{ minimumFractionDigits: options.fractionDigits != null ? options.fractionDigits : 2, maximumFractionDigits: options.fractionDigits != null ? options.fractionDigits : 2, }, ...options, }); const value = formatter.format(number); return value; } export function formatNumberAsStyle(value, style = 'decimal', precision = 2, // Used for decimals, defaults to 2 significantDigits // Used for summary, ie, 1,001.34 with significantDigits=1 will be 1K ) { if (value == null) { return ''; } if (style === 'none') { return `${value}`; } var formula = ''; if (style === 'currency') { formula += '$'; } // All numbers are formatted with commas formula += ','; // TODO: Format `G` as `B`, etc. See: https://github.com/d3/d3/issues/2241 and https://github.com/d3/d3-format/pull/96 if (style === 'percent') { formula += `.${precision}%`; } else if (style === 'percentRound') { formula += `.0%`; } else if (style === 'integer') { formula += `d`; } else if (style === 'metric') { formula += '~s'; } else if (significantDigits === 0) { formula += '~s'; } else if (significantDigits) { formula += `.${significantDigits}s`; } else { formula += `.${precision}f`; } // console.log({ value, formula, result: d3Format(formula)(value) }); return d3Format(formula)(value); } /** * Clamps value within min and max */ export function clamp(value, min, max) { return value < min ? min : value > max ? max : value; } /** * Return the number of decimal positions (ex. 123.45 => 2, 123 => 0) */ export function decimalCount(value) { var _a, _b; return (_b = (_a = value === null || value === void 0 ? void 0 : value.toString().split('.')[1]) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0; } /** * Round to the number of decimals (ex. round(123.45, 1) => 123.5) */ export function round(value, decimals) { return Number(value.toFixed(decimals)); } /** * Get random number between min and max (inclusive) */ export function randomInteger(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }