UNPKG

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