UNPKG

5.13 kBJavaScriptView Raw
1import { format as formatString } from '../../utils/string'
2import { factory } from '../../utils/factory'
3
4const name = 'format'
5const dependencies = ['typed']
6
7export const createFormat = /* #__PURE__ */ factory(name, dependencies, ({ typed }) => {
8 /**
9 * Format a value of any type into a string.
10 *
11 * Syntax:
12 *
13 * math.format(value)
14 * math.format(value, options)
15 * math.format(value, precision)
16 * math.format(value, callback)
17 *
18 * Where:
19 *
20 * - `value: *`
21 * The value to be formatted
22 * - `options: Object`
23 * An object with formatting options. Available options:
24 * - `notation: string`
25 * Number notation. Choose from:
26 * - 'fixed'
27 * Always use regular number notation.
28 * For example '123.40' and '14000000'
29 * - 'exponential'
30 * Always use exponential notation.
31 * For example '1.234e+2' and '1.4e+7'
32 * - 'engineering'
33 * Always use engineering notation: always have exponential notation,
34 * and select the exponent to be a multiple of 3.
35 * For example '123.4e+0' and '14.0e+6'
36 * - 'auto' (default)
37 * Regular number notation for numbers having an absolute value between
38 * `lower` and `upper` bounds, and uses exponential notation elsewhere.
39 * Lower bound is included, upper bound is excluded.
40 * For example '123.4' and '1.4e7'.
41 * - `precision: number`
42 * A number between 0 and 16 to round the digits of the number. In case
43 * of notations 'exponential', 'engineering', and 'auto', `precision`
44 * defines the total number of significant digits returned.
45 * In case of notation 'fixed', `precision` defines the number of
46 * significant digits after the decimal point.
47 * `precision` is undefined by default.
48 * - `lowerExp: number`
49 * Exponent determining the lower boundary for formatting a value with
50 * an exponent when `notation='auto`. Default value is `-3`.
51 * - `upperExp: number`
52 * Exponent determining the upper boundary for formatting a value with
53 * an exponent when `notation='auto`. Default value is `5`.
54 * - `fraction: string`. Available values: 'ratio' (default) or 'decimal'.
55 * For example `format(fraction(1, 3))` will output '1/3' when 'ratio' is
56 * configured, and will output `0.(3)` when 'decimal' is configured.
57 * - `callback: function`
58 * A custom formatting function, invoked for all numeric elements in `value`,
59 * for example all elements of a matrix, or the real and imaginary
60 * parts of a complex number. This callback can be used to override the
61 * built-in numeric notation with any type of formatting. Function `callback`
62 * is called with `value` as parameter and must return a string.
63 *
64 * When `value` is an Object:
65 *
66 * - When the object contains a property `format` being a function, this function
67 * is invoked as `value.format(options)` and the result is returned.
68 * - When the object has its own `toString` method, this method is invoked
69 * and the result is returned.
70 * - In other cases the function will loop over all object properties and
71 * return JSON object notation like '{"a": 2, "b": 3}'.
72 *
73 * When value is a function:
74 *
75 * - When the function has a property `syntax`, it returns this
76 * syntax description.
77 * - In other cases, a string `'function'` is returned.
78 *
79 * Examples:
80 *
81 * math.format(6.4) // returns '6.4'
82 * math.format(1240000) // returns '1.24e6'
83 * math.format(1/3) // returns '0.3333333333333333'
84 * math.format(1/3, 3) // returns '0.333'
85 * math.format(21385, 2) // returns '21000'
86 * math.format(12e8, {notation: 'fixed'}) // returns '1200000000'
87 * math.format(2.3, {notation: 'fixed', precision: 4}) // returns '2.3000'
88 * math.format(52.8, {notation: 'exponential'}) // returns '5.28e+1'
89 * math.format(12400,{notation: 'engineering'}) // returns '12.400e+3'
90 * math.format(2000, {lowerExp: -2, upperExp: 2}) // returns '2e+3'
91 *
92 * function formatCurrency(value) {
93 * // return currency notation with two digits:
94 * return '$' + value.toFixed(2)
95 *
96 * // you could also use math.format inside the callback:
97 * // return '$' + math.format(value, {notation: 'fixed', precision: 2})
98 * }
99 * math.format([2.1, 3, 0.016], formatCurrency} // returns '[$2.10, $3.00, $0.02]'
100 *
101 * See also:
102 *
103 * print
104 *
105 * @param {*} value Value to be stringified
106 * @param {Object | Function | number} [options] Formatting options
107 * @return {string} The formatted value
108 */
109 return typed(name, {
110 any: formatString,
111 'any, Object | function | number': formatString
112 })
113})