UNPKG

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