UNPKG

7.18 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.isNumber = isNumber;
7exports.isBigNumber = isBigNumber;
8exports.isComplex = isComplex;
9exports.isFraction = isFraction;
10exports.isUnit = isUnit;
11exports.isString = isString;
12exports.isMatrix = isMatrix;
13exports.isCollection = isCollection;
14exports.isDenseMatrix = isDenseMatrix;
15exports.isSparseMatrix = isSparseMatrix;
16exports.isRange = isRange;
17exports.isIndex = isIndex;
18exports.isBoolean = isBoolean;
19exports.isResultSet = isResultSet;
20exports.isHelp = isHelp;
21exports.isFunction = isFunction;
22exports.isDate = isDate;
23exports.isRegExp = isRegExp;
24exports.isObject = isObject;
25exports.isNull = isNull;
26exports.isUndefined = isUndefined;
27exports.isAccessorNode = isAccessorNode;
28exports.isArrayNode = isArrayNode;
29exports.isAssignmentNode = isAssignmentNode;
30exports.isBlockNode = isBlockNode;
31exports.isConditionalNode = isConditionalNode;
32exports.isConstantNode = isConstantNode;
33exports.isFunctionAssignmentNode = isFunctionAssignmentNode;
34exports.isFunctionNode = isFunctionNode;
35exports.isIndexNode = isIndexNode;
36exports.isNode = isNode;
37exports.isObjectNode = isObjectNode;
38exports.isOperatorNode = isOperatorNode;
39exports.isParenthesisNode = isParenthesisNode;
40exports.isRangeNode = isRangeNode;
41exports.isSymbolNode = isSymbolNode;
42exports.isChain = isChain;
43exports.typeOf = typeOf;
44exports.isArray = void 0;
45
46function _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); }
47
48// type checks for all known types
49//
50// note that:
51//
52// - check by duck-typing on a property like `isUnit`, instead of checking instanceof.
53// instanceof cannot be used because that would not allow to pass data from
54// one instance of math.js to another since each has it's own instance of Unit.
55// - check the `isUnit` property via the constructor, so there will be no
56// matches for "fake" instances like plain objects with a property `isUnit`.
57// That is important for security reasons.
58// - It must not be possible to override the type checks used internally,
59// for security reasons, so these functions are not exposed in the expression
60// parser.
61function isNumber(x) {
62 return typeof x === 'number';
63}
64
65function isBigNumber(x) {
66 return x && x.constructor.prototype.isBigNumber === true || false;
67}
68
69function isComplex(x) {
70 return x && _typeof(x) === 'object' && Object.getPrototypeOf(x).isComplex === true || false;
71}
72
73function isFraction(x) {
74 return x && _typeof(x) === 'object' && Object.getPrototypeOf(x).isFraction === true || false;
75}
76
77function isUnit(x) {
78 return x && x.constructor.prototype.isUnit === true || false;
79}
80
81function isString(x) {
82 return typeof x === 'string';
83}
84
85var isArray = Array.isArray;
86exports.isArray = isArray;
87
88function isMatrix(x) {
89 return x && x.constructor.prototype.isMatrix === true || false;
90}
91/**
92 * Test whether a value is a collection: an Array or Matrix
93 * @param {*} x
94 * @returns {boolean} isCollection
95 */
96
97
98function isCollection(x) {
99 return Array.isArray(x) || isMatrix(x);
100}
101
102function isDenseMatrix(x) {
103 return x && x.isDenseMatrix && x.constructor.prototype.isMatrix === true || false;
104}
105
106function isSparseMatrix(x) {
107 return x && x.isSparseMatrix && x.constructor.prototype.isMatrix === true || false;
108}
109
110function isRange(x) {
111 return x && x.constructor.prototype.isRange === true || false;
112}
113
114function isIndex(x) {
115 return x && x.constructor.prototype.isIndex === true || false;
116}
117
118function isBoolean(x) {
119 return typeof x === 'boolean';
120}
121
122function isResultSet(x) {
123 return x && x.constructor.prototype.isResultSet === true || false;
124}
125
126function isHelp(x) {
127 return x && x.constructor.prototype.isHelp === true || false;
128}
129
130function isFunction(x) {
131 return typeof x === 'function';
132}
133
134function isDate(x) {
135 return x instanceof Date;
136}
137
138function isRegExp(x) {
139 return x instanceof RegExp;
140}
141
142function isObject(x) {
143 return !!(x && _typeof(x) === 'object' && x.constructor === Object && !isComplex(x) && !isFraction(x));
144}
145
146function isNull(x) {
147 return x === null;
148}
149
150function isUndefined(x) {
151 return x === undefined;
152}
153
154function isAccessorNode(x) {
155 return x && x.isAccessorNode === true && x.constructor.prototype.isNode === true || false;
156}
157
158function isArrayNode(x) {
159 return x && x.isArrayNode === true && x.constructor.prototype.isNode === true || false;
160}
161
162function isAssignmentNode(x) {
163 return x && x.isAssignmentNode === true && x.constructor.prototype.isNode === true || false;
164}
165
166function isBlockNode(x) {
167 return x && x.isBlockNode === true && x.constructor.prototype.isNode === true || false;
168}
169
170function isConditionalNode(x) {
171 return x && x.isConditionalNode === true && x.constructor.prototype.isNode === true || false;
172}
173
174function isConstantNode(x) {
175 return x && x.isConstantNode === true && x.constructor.prototype.isNode === true || false;
176}
177
178function isFunctionAssignmentNode(x) {
179 return x && x.isFunctionAssignmentNode === true && x.constructor.prototype.isNode === true || false;
180}
181
182function isFunctionNode(x) {
183 return x && x.isFunctionNode === true && x.constructor.prototype.isNode === true || false;
184}
185
186function isIndexNode(x) {
187 return x && x.isIndexNode === true && x.constructor.prototype.isNode === true || false;
188}
189
190function isNode(x) {
191 return x && x.isNode === true && x.constructor.prototype.isNode === true || false;
192}
193
194function isObjectNode(x) {
195 return x && x.isObjectNode === true && x.constructor.prototype.isNode === true || false;
196}
197
198function isOperatorNode(x) {
199 return x && x.isOperatorNode === true && x.constructor.prototype.isNode === true || false;
200}
201
202function isParenthesisNode(x) {
203 return x && x.isParenthesisNode === true && x.constructor.prototype.isNode === true || false;
204}
205
206function isRangeNode(x) {
207 return x && x.isRangeNode === true && x.constructor.prototype.isNode === true || false;
208}
209
210function isSymbolNode(x) {
211 return x && x.isSymbolNode === true && x.constructor.prototype.isNode === true || false;
212}
213
214function isChain(x) {
215 return x && x.constructor.prototype.isChain === true || false;
216}
217
218function typeOf(x) {
219 var t = _typeof(x);
220
221 if (t === 'object') {
222 // JavaScript types
223 if (x === null) return 'null';
224 if (Array.isArray(x)) return 'Array';
225 if (x instanceof Date) return 'Date';
226 if (x instanceof RegExp) return 'RegExp'; // math.js types
227
228 if (isBigNumber(x)) return 'BigNumber';
229 if (isComplex(x)) return 'Complex';
230 if (isFraction(x)) return 'Fraction';
231 if (isMatrix(x)) return 'Matrix';
232 if (isUnit(x)) return 'Unit';
233 if (isIndex(x)) return 'Index';
234 if (isRange(x)) return 'Range';
235 if (isResultSet(x)) return 'ResultSet';
236 if (isNode(x)) return x.type;
237 if (isChain(x)) return 'Chain';
238 if (isHelp(x)) return 'Help';
239 return 'Object';
240 }
241
242 if (t === 'function') return 'Function';
243 return t; // can be 'string', 'number', 'boolean', ...
244}
\No newline at end of file