UNPKG

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