is.js (5902B)
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. 14 export function isNumber(x) { 15 return typeof x === 'number'; 16 } 17 export 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 } 32 export function isComplex(x) { 33 return x && typeof x === 'object' && Object.getPrototypeOf(x).isComplex === true || false; 34 } 35 export function isFraction(x) { 36 return x && typeof x === 'object' && Object.getPrototypeOf(x).isFraction === true || false; 37 } 38 export function isUnit(x) { 39 return x && x.constructor.prototype.isUnit === true || false; 40 } 41 export function isString(x) { 42 return typeof x === 'string'; 43 } 44 export var isArray = Array.isArray; 45 export 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 54 export function isCollection(x) { 55 return Array.isArray(x) || isMatrix(x); 56 } 57 export function isDenseMatrix(x) { 58 return x && x.isDenseMatrix && x.constructor.prototype.isMatrix === true || false; 59 } 60 export function isSparseMatrix(x) { 61 return x && x.isSparseMatrix && x.constructor.prototype.isMatrix === true || false; 62 } 63 export function isRange(x) { 64 return x && x.constructor.prototype.isRange === true || false; 65 } 66 export function isIndex(x) { 67 return x && x.constructor.prototype.isIndex === true || false; 68 } 69 export function isBoolean(x) { 70 return typeof x === 'boolean'; 71 } 72 export function isResultSet(x) { 73 return x && x.constructor.prototype.isResultSet === true || false; 74 } 75 export function isHelp(x) { 76 return x && x.constructor.prototype.isHelp === true || false; 77 } 78 export function isFunction(x) { 79 return typeof x === 'function'; 80 } 81 export function isDate(x) { 82 return x instanceof Date; 83 } 84 export function isRegExp(x) { 85 return x instanceof RegExp; 86 } 87 export function isObject(x) { 88 return !!(x && typeof x === 'object' && x.constructor === Object && !isComplex(x) && !isFraction(x)); 89 } 90 export function isNull(x) { 91 return x === null; 92 } 93 export function isUndefined(x) { 94 return x === undefined; 95 } 96 export function isAccessorNode(x) { 97 return x && x.isAccessorNode === true && x.constructor.prototype.isNode === true || false; 98 } 99 export function isArrayNode(x) { 100 return x && x.isArrayNode === true && x.constructor.prototype.isNode === true || false; 101 } 102 export function isAssignmentNode(x) { 103 return x && x.isAssignmentNode === true && x.constructor.prototype.isNode === true || false; 104 } 105 export function isBlockNode(x) { 106 return x && x.isBlockNode === true && x.constructor.prototype.isNode === true || false; 107 } 108 export function isConditionalNode(x) { 109 return x && x.isConditionalNode === true && x.constructor.prototype.isNode === true || false; 110 } 111 export function isConstantNode(x) { 112 return x && x.isConstantNode === true && x.constructor.prototype.isNode === true || false; 113 } 114 export function isFunctionAssignmentNode(x) { 115 return x && x.isFunctionAssignmentNode === true && x.constructor.prototype.isNode === true || false; 116 } 117 export function isFunctionNode(x) { 118 return x && x.isFunctionNode === true && x.constructor.prototype.isNode === true || false; 119 } 120 export function isIndexNode(x) { 121 return x && x.isIndexNode === true && x.constructor.prototype.isNode === true || false; 122 } 123 export function isNode(x) { 124 return x && x.isNode === true && x.constructor.prototype.isNode === true || false; 125 } 126 export function isObjectNode(x) { 127 return x && x.isObjectNode === true && x.constructor.prototype.isNode === true || false; 128 } 129 export function isOperatorNode(x) { 130 return x && x.isOperatorNode === true && x.constructor.prototype.isNode === true || false; 131 } 132 export function isParenthesisNode(x) { 133 return x && x.isParenthesisNode === true && x.constructor.prototype.isNode === true || false; 134 } 135 export function isRangeNode(x) { 136 return x && x.isRangeNode === true && x.constructor.prototype.isNode === true || false; 137 } 138 export function isSymbolNode(x) { 139 return x && x.isSymbolNode === true && x.constructor.prototype.isNode === true || false; 140 } 141 export function isChain(x) { 142 return x && x.constructor.prototype.isChain === true || false; 143 } 144 export 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 }