UNPKG

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