format.js (6340B)
1 "use strict"; 2 3 Object.defineProperty(exports, "__esModule", { 4 value: true 5 }); 6 exports.createFormat = void 0; 7 8 var _string = require("../../utils/string.js"); 9 10 var _factory = require("../../utils/factory.js"); 11 12 var name = 'format'; 13 var dependencies = ['typed']; 14 var createFormat = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) { 15 var typed = _ref.typed; 16 17 /** 18 * Format a value of any type into a string. 19 * 20 * Syntax: 21 * 22 * math.format(value) 23 * math.format(value, options) 24 * math.format(value, precision) 25 * math.format(value, callback) 26 * 27 * Where: 28 * 29 * - `value: *` 30 * The value to be formatted 31 * - `options: Object` 32 * An object with formatting options. Available options: 33 * - `notation: string` 34 * Number notation. Choose from: 35 * - 'fixed' 36 * Always use regular number notation. 37 * For example '123.40' and '14000000' 38 * - 'exponential' 39 * Always use exponential notation. 40 * For example '1.234e+2' and '1.4e+7' 41 * - 'engineering' 42 * Always use engineering notation: always have exponential notation, 43 * and select the exponent to be a multiple of 3. 44 * For example '123.4e+0' and '14.0e+6' 45 * - 'auto' (default) 46 * Regular number notation for numbers having an absolute value between 47 * `lower` and `upper` bounds, and uses exponential notation elsewhere. 48 * Lower bound is included, upper bound is excluded. 49 * For example '123.4' and '1.4e7'. 50 * - 'bin', 'oct, or 'hex' 51 * Format the number using binary, octal, or hexadecimal notation. 52 * For example '0b1101' and '0x10fe'. 53 * - `wordSize: number` 54 * The word size in bits to use for formatting in binary, octal, or 55 * hexadecimal notation. To be used only with 'bin', 'oct', or 'hex' 56 * values for 'notation' option. When this option is defined the value 57 * is formatted as a signed twos complement integer of the given word 58 * size and the size suffix is appended to the output. 59 * For example format(-1, {notation: 'hex', wordSize: 8}) === '0xffi8'. 60 * Default value is undefined. 61 * - `precision: number` 62 * Limit the number of digits of the formatted value. 63 * For regular numbers, must be a number between 0 and 16. 64 * For bignumbers, the maximum depends on the configured precision, 65 * see function `config()`. 66 * In case of notations 'exponential', 'engineering', and 'auto', `precision` 67 * defines the total number of significant digits returned. 68 * In case of notation 'fixed', `precision` defines the number of 69 * significant digits after the decimal point. 70 * `precision` is undefined by default. 71 * - `lowerExp: number` 72 * Exponent determining the lower boundary for formatting a value with 73 * an exponent when `notation='auto`. Default value is `-3`. 74 * - `upperExp: number` 75 * Exponent determining the upper boundary for formatting a value with 76 * an exponent when `notation='auto`. Default value is `5`. 77 * - `fraction: string`. Available values: 'ratio' (default) or 'decimal'. 78 * For example `format(fraction(1, 3))` will output '1/3' when 'ratio' is 79 * configured, and will output `0.(3)` when 'decimal' is configured. 80 * - `truncate: number`. Specifies the maximum allowed length of the 81 * returned string. If it would have been longer, the excess characters 82 * are deleted and replaced with `'...'`. 83 * - `callback: function` 84 * A custom formatting function, invoked for all numeric elements in `value`, 85 * for example all elements of a matrix, or the real and imaginary 86 * parts of a complex number. This callback can be used to override the 87 * built-in numeric notation with any type of formatting. Function `callback` 88 * is called with `value` as parameter and must return a string. 89 * 90 * When `value` is an Object: 91 * 92 * - When the object contains a property `format` being a function, this function 93 * is invoked as `value.format(options)` and the result is returned. 94 * - When the object has its own `toString` method, this method is invoked 95 * and the result is returned. 96 * - In other cases the function will loop over all object properties and 97 * return JSON object notation like '{"a": 2, "b": 3}'. 98 * 99 * When value is a function: 100 * 101 * - When the function has a property `syntax`, it returns this 102 * syntax description. 103 * - In other cases, a string `'function'` is returned. 104 * 105 * Examples: 106 * 107 * math.format(6.4) // returns '6.4' 108 * math.format(1240000) // returns '1.24e6' 109 * math.format(1/3) // returns '0.3333333333333333' 110 * math.format(1/3, 3) // returns '0.333' 111 * math.format(21385, 2) // returns '21000' 112 * math.format(12e8, {notation: 'fixed'}) // returns '1200000000' 113 * math.format(2.3, {notation: 'fixed', precision: 4}) // returns '2.3000' 114 * math.format(52.8, {notation: 'exponential'}) // returns '5.28e+1' 115 * math.format(12400,{notation: 'engineering'}) // returns '12.400e+3' 116 * math.format(2000, {lowerExp: -2, upperExp: 2}) // returns '2e+3' 117 * 118 * function formatCurrency(value) { 119 * // return currency notation with two digits: 120 * return '$' + value.toFixed(2) 121 * 122 * // you could also use math.format inside the callback: 123 * // return '$' + math.format(value, {notation: 'fixed', precision: 2}) 124 * } 125 * math.format([2.1, 3, 0.016], formatCurrency) // returns '[$2.10, $3.00, $0.02]' 126 * 127 * See also: 128 * 129 * print 130 * 131 * @param {*} value Value to be stringified 132 * @param {Object | Function | number} [options] Formatting options 133 * @return {string} The formatted value 134 */ 135 return typed(name, { 136 any: _string.format, 137 'any, Object | function | number': _string.format 138 }); 139 }); 140 exports.createFormat = createFormat;