UNPKG

1.5 kBJavaScriptView Raw
1'use strict'
2
3const deepMap = require('./../utils/collection/deepMap')
4const number = require('../utils/number')
5
6function factory (type, config, load, typed) {
7 /**
8 * Create a string or convert any object into a string.
9 * Elements of Arrays and Matrices are processed element wise.
10 *
11 * Syntax:
12 *
13 * math.string(value)
14 *
15 * Examples:
16 *
17 * math.string(4.2) // returns string '4.2'
18 * math.string(math.complex(3, 2) // returns string '3 + 2i'
19 *
20 * const u = math.unit(5, 'km')
21 * math.string(u.to('m')) // returns string '5000 m'
22 *
23 * math.string([true, false]) // returns ['true', 'false']
24 *
25 * See also:
26 *
27 * bignumber, boolean, complex, index, matrix, number, unit
28 *
29 * @param {* | Array | Matrix | null} [value] A value to convert to a string
30 * @return {string | Array | Matrix} The created string
31 */
32 const string = typed('string', {
33 '': function () {
34 return ''
35 },
36
37 'number': number.format,
38
39 'null': function (x) {
40 return 'null'
41 },
42
43 'boolean': function (x) {
44 return x + ''
45 },
46
47 'string': function (x) {
48 return x
49 },
50
51 'Array | Matrix': function (x) {
52 return deepMap(x, string)
53 },
54
55 'any': function (x) {
56 return String(x)
57 }
58 })
59
60 string.toTex = {
61 0: '\\mathtt{""}',
62 1: `\\mathrm{string}\\left(\${args[0]}\\right)`
63 }
64
65 return string
66}
67
68exports.name = 'string'
69exports.factory = factory