UNPKG

5.98 kBJavaScriptView Raw
1function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
2
3// type checks for all known types
4//
5// note that:
6//
7// - check by duck-typing on a property like `isUnit`, instead of checking instanceof.
8// instanceof cannot be used because that would not allow to pass data from
9// one instance of math.js to another since each has it's own instance of Unit.
10// - check the `isUnit` property via the constructor, so there will be no
11// matches for "fake" instances like plain objects with a property `isUnit`.
12// That is important for security reasons.
13// - It must not be possible to override the type checks used internally,
14// for security reasons, so these functions are not exposed in the expression
15// parser.
16export function isNumber(x) {
17 return typeof x === 'number';
18}
19export function isBigNumber(x) {
20 return x && x.constructor.prototype.isBigNumber === true || false;
21}
22export function isComplex(x) {
23 return x && _typeof(x) === 'object' && Object.getPrototypeOf(x).isComplex === true || false;
24}
25export function isFraction(x) {
26 return x && _typeof(x) === 'object' && Object.getPrototypeOf(x).isFraction === true || false;
27}
28export function isUnit(x) {
29 return x && x.constructor.prototype.isUnit === true || false;
30}
31export function isString(x) {
32 return typeof x === 'string';
33}
34export var isArray = Array.isArray;
35export function isMatrix(x) {
36 return x && x.constructor.prototype.isMatrix === true || false;
37}
38/**
39 * Test whether a value is a collection: an Array or Matrix
40 * @param {*} x
41 * @returns {boolean} isCollection
42 */
43
44export function isCollection(x) {
45 return Array.isArray(x) || isMatrix(x);
46}
47export function isDenseMatrix(x) {
48 return x && x.isDenseMatrix && x.constructor.prototype.isMatrix === true || false;
49}
50export function isSparseMatrix(x) {
51 return x && x.isSparseMatrix && x.constructor.prototype.isMatrix === true || false;
52}
53export function isRange(x) {
54 return x && x.constructor.prototype.isRange === true || false;
55}
56export function isIndex(x) {
57 return x && x.constructor.prototype.isIndex === true || false;
58}
59export function isBoolean(x) {
60 return typeof x === 'boolean';
61}
62export function isResultSet(x) {
63 return x && x.constructor.prototype.isResultSet === true || false;
64}
65export function isHelp(x) {
66 return x && x.constructor.prototype.isHelp === true || false;
67}
68export function isFunction(x) {
69 return typeof x === 'function';
70}
71export function isDate(x) {
72 return x instanceof Date;
73}
74export function isRegExp(x) {
75 return x instanceof RegExp;
76}
77export function isObject(x) {
78 return !!(x && _typeof(x) === 'object' && x.constructor === Object && !isComplex(x) && !isFraction(x));
79}
80export function isNull(x) {
81 return x === null;
82}
83export function isUndefined(x) {
84 return x === undefined;
85}
86export function isAccessorNode(x) {
87 return x && x.isAccessorNode === true && x.constructor.prototype.isNode === true || false;
88}
89export function isArrayNode(x) {
90 return x && x.isArrayNode === true && x.constructor.prototype.isNode === true || false;
91}
92export function isAssignmentNode(x) {
93 return x && x.isAssignmentNode === true && x.constructor.prototype.isNode === true || false;
94}
95export function isBlockNode(x) {
96 return x && x.isBlockNode === true && x.constructor.prototype.isNode === true || false;
97}
98export function isConditionalNode(x) {
99 return x && x.isConditionalNode === true && x.constructor.prototype.isNode === true || false;
100}
101export function isConstantNode(x) {
102 return x && x.isConstantNode === true && x.constructor.prototype.isNode === true || false;
103}
104export function isFunctionAssignmentNode(x) {
105 return x && x.isFunctionAssignmentNode === true && x.constructor.prototype.isNode === true || false;
106}
107export function isFunctionNode(x) {
108 return x && x.isFunctionNode === true && x.constructor.prototype.isNode === true || false;
109}
110export function isIndexNode(x) {
111 return x && x.isIndexNode === true && x.constructor.prototype.isNode === true || false;
112}
113export function isNode(x) {
114 return x && x.isNode === true && x.constructor.prototype.isNode === true || false;
115}
116export function isObjectNode(x) {
117 return x && x.isObjectNode === true && x.constructor.prototype.isNode === true || false;
118}
119export function isOperatorNode(x) {
120 return x && x.isOperatorNode === true && x.constructor.prototype.isNode === true || false;
121}
122export function isParenthesisNode(x) {
123 return x && x.isParenthesisNode === true && x.constructor.prototype.isNode === true || false;
124}
125export function isRangeNode(x) {
126 return x && x.isRangeNode === true && x.constructor.prototype.isNode === true || false;
127}
128export function isSymbolNode(x) {
129 return x && x.isSymbolNode === true && x.constructor.prototype.isNode === true || false;
130}
131export function isChain(x) {
132 return x && x.constructor.prototype.isChain === true || false;
133}
134export function typeOf(x) {
135 var t = _typeof(x);
136
137 if (t === 'object') {
138 // JavaScript types
139 if (x === null) return 'null';
140 if (Array.isArray(x)) return 'Array';
141 if (x instanceof Date) return 'Date';
142 if (x instanceof RegExp) return 'RegExp'; // math.js types
143
144 if (isBigNumber(x)) return 'BigNumber';
145 if (isComplex(x)) return 'Complex';
146 if (isFraction(x)) return 'Fraction';
147 if (isMatrix(x)) return 'Matrix';
148 if (isUnit(x)) return 'Unit';
149 if (isIndex(x)) return 'Index';
150 if (isRange(x)) return 'Range';
151 if (isResultSet(x)) return 'ResultSet';
152 if (isNode(x)) return x.type;
153 if (isChain(x)) return 'Chain';
154 if (isHelp(x)) return 'Help';
155 return 'Object';
156 }
157
158 if (t === 'function') return 'Function';
159 return t; // can be 'string', 'number', 'boolean', ...
160}
\No newline at end of file