is.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. if (x.isBigNumber === true && typeof x.constructor.prototype === 'object' && x.constructor.prototype.isBigNumber === true) {
  22. return true;
  23. }
  24. if (typeof x.constructor.isDecimal === 'function' && x.constructor.isDecimal(x) === true) {
  25. return true;
  26. }
  27. return false;
  28. }
  29. export function isComplex(x) {
  30. return x && typeof x === 'object' && Object.getPrototypeOf(x).isComplex === true || false;
  31. }
  32. export function isFraction(x) {
  33. return x && typeof x === 'object' && Object.getPrototypeOf(x).isFraction === true || false;
  34. }
  35. export function isUnit(x) {
  36. return x && x.constructor.prototype.isUnit === true || false;
  37. }
  38. export function isString(x) {
  39. return typeof x === 'string';
  40. }
  41. export var isArray = Array.isArray;
  42. export function isMatrix(x) {
  43. return x && x.constructor.prototype.isMatrix === true || false;
  44. }
  45. /**
  46. * Test whether a value is a collection: an Array or Matrix
  47. * @param {*} x
  48. * @returns {boolean} isCollection
  49. */
  50. export function isCollection(x) {
  51. return Array.isArray(x) || isMatrix(x);
  52. }
  53. export function isDenseMatrix(x) {
  54. return x && x.isDenseMatrix && x.constructor.prototype.isMatrix === true || false;
  55. }
  56. export function isSparseMatrix(x) {
  57. return x && x.isSparseMatrix && x.constructor.prototype.isMatrix === true || false;
  58. }
  59. export function isRange(x) {
  60. return x && x.constructor.prototype.isRange === true || false;
  61. }
  62. export function isIndex(x) {
  63. return x && x.constructor.prototype.isIndex === true || false;
  64. }
  65. export function isBoolean(x) {
  66. return typeof x === 'boolean';
  67. }
  68. export function isResultSet(x) {
  69. return x && x.constructor.prototype.isResultSet === true || false;
  70. }
  71. export function isHelp(x) {
  72. return x && x.constructor.prototype.isHelp === true || false;
  73. }
  74. export function isFunction(x) {
  75. return typeof x === 'function';
  76. }
  77. export function isDate(x) {
  78. return x instanceof Date;
  79. }
  80. export function isRegExp(x) {
  81. return x instanceof RegExp;
  82. }
  83. export function isObject(x) {
  84. return !!(x && typeof x === 'object' && x.constructor === Object && !isComplex(x) && !isFraction(x));
  85. }
  86. export function isNull(x) {
  87. return x === null;
  88. }
  89. export function isUndefined(x) {
  90. return x === undefined;
  91. }
  92. export function isAccessorNode(x) {
  93. return x && x.isAccessorNode === true && x.constructor.prototype.isNode === true || false;
  94. }
  95. export function isArrayNode(x) {
  96. return x && x.isArrayNode === true && x.constructor.prototype.isNode === true || false;
  97. }
  98. export function isAssignmentNode(x) {
  99. return x && x.isAssignmentNode === true && x.constructor.prototype.isNode === true || false;
  100. }
  101. export function isBlockNode(x) {
  102. return x && x.isBlockNode === true && x.constructor.prototype.isNode === true || false;
  103. }
  104. export function isConditionalNode(x) {
  105. return x && x.isConditionalNode === true && x.constructor.prototype.isNode === true || false;
  106. }
  107. export function isConstantNode(x) {
  108. return x && x.isConstantNode === true && x.constructor.prototype.isNode === true || false;
  109. }
  110. /* Very specialized: returns true for those nodes which in the numerator of
  111. a fraction means that the division in that fraction has precedence over implicit
  112. multiplication, e.g. -2/3 x parses as (-2/3) x and 3/4 x parses as (3/4) x but
  113. 6!/8 x parses as 6! / (8x). It is located here because it is shared between
  114. parse.js and OperatorNode.js (for parsing and printing, respectively).
  115. This should *not* be exported from mathjs, unlike most of the tests here.
  116. Its name does not start with 'is' to prevent utils/snapshot.js from thinking
  117. it should be exported.
  118. */
  119. export function rule2Node(node) {
  120. return isConstantNode(node) || isOperatorNode(node) && node.args.length === 1 && isConstantNode(node.args[0]) && '-+~'.includes(node.op);
  121. }
  122. export function isFunctionAssignmentNode(x) {
  123. return x && x.isFunctionAssignmentNode === true && x.constructor.prototype.isNode === true || false;
  124. }
  125. export function isFunctionNode(x) {
  126. return x && x.isFunctionNode === true && x.constructor.prototype.isNode === true || false;
  127. }
  128. export function isIndexNode(x) {
  129. return x && x.isIndexNode === true && x.constructor.prototype.isNode === true || false;
  130. }
  131. export function isNode(x) {
  132. return x && x.isNode === true && x.constructor.prototype.isNode === true || false;
  133. }
  134. export function isObjectNode(x) {
  135. return x && x.isObjectNode === true && x.constructor.prototype.isNode === true || false;
  136. }
  137. export function isOperatorNode(x) {
  138. return x && x.isOperatorNode === true && x.constructor.prototype.isNode === true || false;
  139. }
  140. export function isParenthesisNode(x) {
  141. return x && x.isParenthesisNode === true && x.constructor.prototype.isNode === true || false;
  142. }
  143. export function isRangeNode(x) {
  144. return x && x.isRangeNode === true && x.constructor.prototype.isNode === true || false;
  145. }
  146. export function isRelationalNode(x) {
  147. return x && x.isRelationalNode === true && x.constructor.prototype.isNode === true || false;
  148. }
  149. export function isSymbolNode(x) {
  150. return x && x.isSymbolNode === true && x.constructor.prototype.isNode === true || false;
  151. }
  152. export function isChain(x) {
  153. return x && x.constructor.prototype.isChain === true || false;
  154. }
  155. export function typeOf(x) {
  156. var t = typeof x;
  157. if (t === 'object') {
  158. if (x === null) return 'null';
  159. if (isBigNumber(x)) return 'BigNumber'; // Special: weird mashup with Decimal
  160. if (x.constructor && x.constructor.name) return x.constructor.name;
  161. return 'Object'; // just in case
  162. }
  163. return t; // can be 'string', 'number', 'boolean', 'function', 'bigint', ...
  164. }