UNPKG

5.59 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 return x && x.constructor.prototype.isBigNumber === true || false;
19}
20export function isComplex(x) {
21 return x && typeof x === 'object' && Object.getPrototypeOf(x).isComplex === true || false;
22}
23export function isFraction(x) {
24 return x && typeof x === 'object' && Object.getPrototypeOf(x).isFraction === true || false;
25}
26export function isUnit(x) {
27 return x && x.constructor.prototype.isUnit === true || false;
28}
29export function isString(x) {
30 return typeof x === 'string';
31}
32export var isArray = Array.isArray;
33export function isMatrix(x) {
34 return x && x.constructor.prototype.isMatrix === true || false;
35}
36/**
37 * Test whether a value is a collection: an Array or Matrix
38 * @param {*} x
39 * @returns {boolean} isCollection
40 */
41
42export function isCollection(x) {
43 return Array.isArray(x) || isMatrix(x);
44}
45export function isDenseMatrix(x) {
46 return x && x.isDenseMatrix && x.constructor.prototype.isMatrix === true || false;
47}
48export function isSparseMatrix(x) {
49 return x && x.isSparseMatrix && x.constructor.prototype.isMatrix === true || false;
50}
51export function isRange(x) {
52 return x && x.constructor.prototype.isRange === true || false;
53}
54export function isIndex(x) {
55 return x && x.constructor.prototype.isIndex === true || false;
56}
57export function isBoolean(x) {
58 return typeof x === 'boolean';
59}
60export function isResultSet(x) {
61 return x && x.constructor.prototype.isResultSet === true || false;
62}
63export function isHelp(x) {
64 return x && x.constructor.prototype.isHelp === true || false;
65}
66export function isFunction(x) {
67 return typeof x === 'function';
68}
69export function isDate(x) {
70 return x instanceof Date;
71}
72export function isRegExp(x) {
73 return x instanceof RegExp;
74}
75export function isObject(x) {
76 return !!(x && typeof x === 'object' && x.constructor === Object && !isComplex(x) && !isFraction(x));
77}
78export function isNull(x) {
79 return x === null;
80}
81export function isUndefined(x) {
82 return x === undefined;
83}
84export function isAccessorNode(x) {
85 return x && x.isAccessorNode === true && x.constructor.prototype.isNode === true || false;
86}
87export function isArrayNode(x) {
88 return x && x.isArrayNode === true && x.constructor.prototype.isNode === true || false;
89}
90export function isAssignmentNode(x) {
91 return x && x.isAssignmentNode === true && x.constructor.prototype.isNode === true || false;
92}
93export function isBlockNode(x) {
94 return x && x.isBlockNode === true && x.constructor.prototype.isNode === true || false;
95}
96export function isConditionalNode(x) {
97 return x && x.isConditionalNode === true && x.constructor.prototype.isNode === true || false;
98}
99export function isConstantNode(x) {
100 return x && x.isConstantNode === true && x.constructor.prototype.isNode === true || false;
101}
102export function isFunctionAssignmentNode(x) {
103 return x && x.isFunctionAssignmentNode === true && x.constructor.prototype.isNode === true || false;
104}
105export function isFunctionNode(x) {
106 return x && x.isFunctionNode === true && x.constructor.prototype.isNode === true || false;
107}
108export function isIndexNode(x) {
109 return x && x.isIndexNode === true && x.constructor.prototype.isNode === true || false;
110}
111export function isNode(x) {
112 return x && x.isNode === true && x.constructor.prototype.isNode === true || false;
113}
114export function isObjectNode(x) {
115 return x && x.isObjectNode === true && x.constructor.prototype.isNode === true || false;
116}
117export function isOperatorNode(x) {
118 return x && x.isOperatorNode === true && x.constructor.prototype.isNode === true || false;
119}
120export function isParenthesisNode(x) {
121 return x && x.isParenthesisNode === true && x.constructor.prototype.isNode === true || false;
122}
123export function isRangeNode(x) {
124 return x && x.isRangeNode === true && x.constructor.prototype.isNode === true || false;
125}
126export function isSymbolNode(x) {
127 return x && x.isSymbolNode === true && x.constructor.prototype.isNode === true || false;
128}
129export function isChain(x) {
130 return x && x.constructor.prototype.isChain === true || false;
131}
132export function typeOf(x) {
133 var t = typeof x;
134
135 if (t === 'object') {
136 // JavaScript types
137 if (x === null) return 'null';
138 if (Array.isArray(x)) return 'Array';
139 if (x instanceof Date) return 'Date';
140 if (x instanceof RegExp) return 'RegExp'; // math.js types
141
142 if (isBigNumber(x)) return 'BigNumber';
143 if (isComplex(x)) return 'Complex';
144 if (isFraction(x)) return 'Fraction';
145 if (isMatrix(x)) return 'Matrix';
146 if (isUnit(x)) return 'Unit';
147 if (isIndex(x)) return 'Index';
148 if (isRange(x)) return 'Range';
149 if (isResultSet(x)) return 'ResultSet';
150 if (isNode(x)) return x.type;
151 if (isChain(x)) return 'Chain';
152 if (isHelp(x)) return 'Help';
153 return 'Object';
154 }
155
156 if (t === 'function') return 'Function';
157 return t; // can be 'string', 'number', 'boolean', ...
158}
\No newline at end of file