UNPKG

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