1 | import { factory } from '../utils/factory';
|
2 | import { deepMap } from '../utils/collection';
|
3 | var name = 'boolean';
|
4 | var dependencies = ['typed'];
|
5 | export var createBoolean = /* #__PURE__ */factory(name, dependencies, function (_ref) {
|
6 | var typed = _ref.typed;
|
7 |
|
8 | /**
|
9 | * Create a boolean or convert a string or number to a boolean.
|
10 | * In case of a number, `true` is returned for non-zero numbers, and `false` in
|
11 | * case of zero.
|
12 | * Strings can be `'true'` or `'false'`, or can contain a number.
|
13 | * When value is a matrix, all elements will be converted to boolean.
|
14 | *
|
15 | * Syntax:
|
16 | *
|
17 | * math.boolean(x)
|
18 | *
|
19 | * Examples:
|
20 | *
|
21 | * math.boolean(0) // returns false
|
22 | * math.boolean(1) // returns true
|
23 | * math.boolean(-3) // returns true
|
24 | * math.boolean('true') // returns true
|
25 | * math.boolean('false') // returns false
|
26 | * math.boolean([1, 0, 1, 1]) // returns [true, false, true, true]
|
27 | *
|
28 | * See also:
|
29 | *
|
30 | * bignumber, complex, index, matrix, string, unit
|
31 | *
|
32 | * @param {string | number | boolean | Array | Matrix | null} value A value of any type
|
33 | * @return {boolean | Array | Matrix} The boolean value
|
34 | */
|
35 | return typed(name, {
|
36 | '': function _() {
|
37 | return false;
|
38 | },
|
39 | "boolean": function boolean(x) {
|
40 | return x;
|
41 | },
|
42 | number: function number(x) {
|
43 | return !!x;
|
44 | },
|
45 | "null": function _null(x) {
|
46 | return false;
|
47 | },
|
48 | BigNumber: function BigNumber(x) {
|
49 | return !x.isZero();
|
50 | },
|
51 | string: function string(x) {
|
52 | // try case insensitive
|
53 | var lcase = x.toLowerCase();
|
54 |
|
55 | if (lcase === 'true') {
|
56 | return true;
|
57 | } else if (lcase === 'false') {
|
58 | return false;
|
59 | } // test whether value is a valid number
|
60 |
|
61 |
|
62 | var num = Number(x);
|
63 |
|
64 | if (x !== '' && !isNaN(num)) {
|
65 | return !!num;
|
66 | }
|
67 |
|
68 | throw new Error('Cannot convert "' + x + '" to a boolean');
|
69 | },
|
70 | 'Array | Matrix': function ArrayMatrix(x) {
|
71 | return deepMap(x, this);
|
72 | }
|
73 | });
|
74 | }); |
\ | No newline at end of file |