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