is.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.isAccessorNode = isAccessorNode;
  7. exports.isArray = void 0;
  8. exports.isArrayNode = isArrayNode;
  9. exports.isAssignmentNode = isAssignmentNode;
  10. exports.isBigNumber = isBigNumber;
  11. exports.isBlockNode = isBlockNode;
  12. exports.isBoolean = isBoolean;
  13. exports.isChain = isChain;
  14. exports.isCollection = isCollection;
  15. exports.isComplex = isComplex;
  16. exports.isConditionalNode = isConditionalNode;
  17. exports.isConstantNode = isConstantNode;
  18. exports.isDate = isDate;
  19. exports.isDenseMatrix = isDenseMatrix;
  20. exports.isFraction = isFraction;
  21. exports.isFunction = isFunction;
  22. exports.isFunctionAssignmentNode = isFunctionAssignmentNode;
  23. exports.isFunctionNode = isFunctionNode;
  24. exports.isHelp = isHelp;
  25. exports.isIndex = isIndex;
  26. exports.isIndexNode = isIndexNode;
  27. exports.isMatrix = isMatrix;
  28. exports.isNode = isNode;
  29. exports.isNull = isNull;
  30. exports.isNumber = isNumber;
  31. exports.isObject = isObject;
  32. exports.isObjectNode = isObjectNode;
  33. exports.isOperatorNode = isOperatorNode;
  34. exports.isParenthesisNode = isParenthesisNode;
  35. exports.isRange = isRange;
  36. exports.isRangeNode = isRangeNode;
  37. exports.isRegExp = isRegExp;
  38. exports.isRelationalNode = isRelationalNode;
  39. exports.isResultSet = isResultSet;
  40. exports.isSparseMatrix = isSparseMatrix;
  41. exports.isString = isString;
  42. exports.isSymbolNode = isSymbolNode;
  43. exports.isUndefined = isUndefined;
  44. exports.isUnit = isUnit;
  45. exports.rule2Node = rule2Node;
  46. exports.typeOf = typeOf;
  47. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  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.
  61. function isNumber(x) {
  62. return typeof x === 'number';
  63. }
  64. function isBigNumber(x) {
  65. if (!x || (0, _typeof2["default"])(x) !== 'object' || typeof x.constructor !== 'function') {
  66. return false;
  67. }
  68. if (x.isBigNumber === true && (0, _typeof2["default"])(x.constructor.prototype) === 'object' && x.constructor.prototype.isBigNumber === true) {
  69. return true;
  70. }
  71. if (typeof x.constructor.isDecimal === 'function' && x.constructor.isDecimal(x) === true) {
  72. return true;
  73. }
  74. return false;
  75. }
  76. function isComplex(x) {
  77. return x && (0, _typeof2["default"])(x) === 'object' && Object.getPrototypeOf(x).isComplex === true || false;
  78. }
  79. function isFraction(x) {
  80. return x && (0, _typeof2["default"])(x) === 'object' && Object.getPrototypeOf(x).isFraction === true || false;
  81. }
  82. function isUnit(x) {
  83. return x && x.constructor.prototype.isUnit === true || false;
  84. }
  85. function isString(x) {
  86. return typeof x === 'string';
  87. }
  88. var isArray = Array.isArray;
  89. exports.isArray = isArray;
  90. function isMatrix(x) {
  91. return x && x.constructor.prototype.isMatrix === true || false;
  92. }
  93. /**
  94. * Test whether a value is a collection: an Array or Matrix
  95. * @param {*} x
  96. * @returns {boolean} isCollection
  97. */
  98. function isCollection(x) {
  99. return Array.isArray(x) || isMatrix(x);
  100. }
  101. function isDenseMatrix(x) {
  102. return x && x.isDenseMatrix && x.constructor.prototype.isMatrix === true || false;
  103. }
  104. function isSparseMatrix(x) {
  105. return x && x.isSparseMatrix && x.constructor.prototype.isMatrix === true || false;
  106. }
  107. function isRange(x) {
  108. return x && x.constructor.prototype.isRange === true || false;
  109. }
  110. function isIndex(x) {
  111. return x && x.constructor.prototype.isIndex === true || false;
  112. }
  113. function isBoolean(x) {
  114. return typeof x === 'boolean';
  115. }
  116. function isResultSet(x) {
  117. return x && x.constructor.prototype.isResultSet === true || false;
  118. }
  119. function isHelp(x) {
  120. return x && x.constructor.prototype.isHelp === true || false;
  121. }
  122. function isFunction(x) {
  123. return typeof x === 'function';
  124. }
  125. function isDate(x) {
  126. return x instanceof Date;
  127. }
  128. function isRegExp(x) {
  129. return x instanceof RegExp;
  130. }
  131. function isObject(x) {
  132. return !!(x && (0, _typeof2["default"])(x) === 'object' && x.constructor === Object && !isComplex(x) && !isFraction(x));
  133. }
  134. function isNull(x) {
  135. return x === null;
  136. }
  137. function isUndefined(x) {
  138. return x === undefined;
  139. }
  140. function isAccessorNode(x) {
  141. return x && x.isAccessorNode === true && x.constructor.prototype.isNode === true || false;
  142. }
  143. function isArrayNode(x) {
  144. return x && x.isArrayNode === true && x.constructor.prototype.isNode === true || false;
  145. }
  146. function isAssignmentNode(x) {
  147. return x && x.isAssignmentNode === true && x.constructor.prototype.isNode === true || false;
  148. }
  149. function isBlockNode(x) {
  150. return x && x.isBlockNode === true && x.constructor.prototype.isNode === true || false;
  151. }
  152. function isConditionalNode(x) {
  153. return x && x.isConditionalNode === true && x.constructor.prototype.isNode === true || false;
  154. }
  155. function isConstantNode(x) {
  156. return x && x.isConstantNode === true && x.constructor.prototype.isNode === true || false;
  157. }
  158. /* Very specialized: returns true for those nodes which in the numerator of
  159. a fraction means that the division in that fraction has precedence over implicit
  160. multiplication, e.g. -2/3 x parses as (-2/3) x and 3/4 x parses as (3/4) x but
  161. 6!/8 x parses as 6! / (8x). It is located here because it is shared between
  162. parse.js and OperatorNode.js (for parsing and printing, respectively).
  163. This should *not* be exported from mathjs, unlike most of the tests here.
  164. Its name does not start with 'is' to prevent utils/snapshot.js from thinking
  165. it should be exported.
  166. */
  167. function rule2Node(node) {
  168. return isConstantNode(node) || isOperatorNode(node) && node.args.length === 1 && isConstantNode(node.args[0]) && '-+~'.includes(node.op);
  169. }
  170. function isFunctionAssignmentNode(x) {
  171. return x && x.isFunctionAssignmentNode === true && x.constructor.prototype.isNode === true || false;
  172. }
  173. function isFunctionNode(x) {
  174. return x && x.isFunctionNode === true && x.constructor.prototype.isNode === true || false;
  175. }
  176. function isIndexNode(x) {
  177. return x && x.isIndexNode === true && x.constructor.prototype.isNode === true || false;
  178. }
  179. function isNode(x) {
  180. return x && x.isNode === true && x.constructor.prototype.isNode === true || false;
  181. }
  182. function isObjectNode(x) {
  183. return x && x.isObjectNode === true && x.constructor.prototype.isNode === true || false;
  184. }
  185. function isOperatorNode(x) {
  186. return x && x.isOperatorNode === true && x.constructor.prototype.isNode === true || false;
  187. }
  188. function isParenthesisNode(x) {
  189. return x && x.isParenthesisNode === true && x.constructor.prototype.isNode === true || false;
  190. }
  191. function isRangeNode(x) {
  192. return x && x.isRangeNode === true && x.constructor.prototype.isNode === true || false;
  193. }
  194. function isRelationalNode(x) {
  195. return x && x.isRelationalNode === true && x.constructor.prototype.isNode === true || false;
  196. }
  197. function isSymbolNode(x) {
  198. return x && x.isSymbolNode === true && x.constructor.prototype.isNode === true || false;
  199. }
  200. function isChain(x) {
  201. return x && x.constructor.prototype.isChain === true || false;
  202. }
  203. function typeOf(x) {
  204. var t = (0, _typeof2["default"])(x);
  205. if (t === 'object') {
  206. if (x === null) return 'null';
  207. if (isBigNumber(x)) return 'BigNumber'; // Special: weird mashup with Decimal
  208. if (x.constructor && x.constructor.name) return x.constructor.name;
  209. return 'Object'; // just in case
  210. }
  211. return t; // can be 'string', 'number', 'boolean', 'function', 'bigint', ...
  212. }