UNPKG

1.41 kBJavaScriptView Raw
1import { factory } from '../utils/factory'
2import { deepMap } from '../utils/collection'
3import { format } from '../utils/number'
4
5const name = 'string'
6const dependencies = ['typed']
7
8export const createString = /* #__PURE__ */ factory(name, dependencies, ({ typed }) => {
9 /**
10 * Create a string or convert any object into a string.
11 * Elements of Arrays and Matrices are processed element wise.
12 *
13 * Syntax:
14 *
15 * math.string(value)
16 *
17 * Examples:
18 *
19 * math.string(4.2) // returns string '4.2'
20 * math.string(math.complex(3, 2) // returns string '3 + 2i'
21 *
22 * const u = math.unit(5, 'km')
23 * math.string(u.to('m')) // returns string '5000 m'
24 *
25 * math.string([true, false]) // returns ['true', 'false']
26 *
27 * See also:
28 *
29 * bignumber, boolean, complex, index, matrix, number, unit
30 *
31 * @param {* | Array | Matrix | null} [value] A value to convert to a string
32 * @return {string | Array | Matrix} The created string
33 */
34 return typed(name, {
35 '': function () {
36 return ''
37 },
38
39 number: format,
40
41 null: function (x) {
42 return 'null'
43 },
44
45 boolean: function (x) {
46 return x + ''
47 },
48
49 string: function (x) {
50 return x
51 },
52
53 'Array | Matrix': function (x) {
54 return deepMap(x, this)
55 },
56
57 any: function (x) {
58 return String(x)
59 }
60 })
61})