simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

is.js (7370B)


      1 "use strict";
      2 
      3 var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
      4 
      5 Object.defineProperty(exports, "__esModule", {
      6   value: true
      7 });
      8 exports.isAccessorNode = isAccessorNode;
      9 exports.isArray = void 0;
     10 exports.isArrayNode = isArrayNode;
     11 exports.isAssignmentNode = isAssignmentNode;
     12 exports.isBigNumber = isBigNumber;
     13 exports.isBlockNode = isBlockNode;
     14 exports.isBoolean = isBoolean;
     15 exports.isChain = isChain;
     16 exports.isCollection = isCollection;
     17 exports.isComplex = isComplex;
     18 exports.isConditionalNode = isConditionalNode;
     19 exports.isConstantNode = isConstantNode;
     20 exports.isDate = isDate;
     21 exports.isDenseMatrix = isDenseMatrix;
     22 exports.isFraction = isFraction;
     23 exports.isFunction = isFunction;
     24 exports.isFunctionAssignmentNode = isFunctionAssignmentNode;
     25 exports.isFunctionNode = isFunctionNode;
     26 exports.isHelp = isHelp;
     27 exports.isIndex = isIndex;
     28 exports.isIndexNode = isIndexNode;
     29 exports.isMatrix = isMatrix;
     30 exports.isNode = isNode;
     31 exports.isNull = isNull;
     32 exports.isNumber = isNumber;
     33 exports.isObject = isObject;
     34 exports.isObjectNode = isObjectNode;
     35 exports.isOperatorNode = isOperatorNode;
     36 exports.isParenthesisNode = isParenthesisNode;
     37 exports.isRange = isRange;
     38 exports.isRangeNode = isRangeNode;
     39 exports.isRegExp = isRegExp;
     40 exports.isResultSet = isResultSet;
     41 exports.isSparseMatrix = isSparseMatrix;
     42 exports.isString = isString;
     43 exports.isSymbolNode = isSymbolNode;
     44 exports.isUndefined = isUndefined;
     45 exports.isUnit = isUnit;
     46 exports.typeOf = typeOf;
     47 
     48 var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
     49 
     50 // type checks for all known types
     51 //
     52 // note that:
     53 //
     54 // - check by duck-typing on a property like `isUnit`, instead of checking instanceof.
     55 //   instanceof cannot be used because that would not allow to pass data from
     56 //   one instance of math.js to another since each has it's own instance of Unit.
     57 // - check the `isUnit` property via the constructor, so there will be no
     58 //   matches for "fake" instances like plain objects with a property `isUnit`.
     59 //   That is important for security reasons.
     60 // - It must not be possible to override the type checks used internally,
     61 //   for security reasons, so these functions are not exposed in the expression
     62 //   parser.
     63 function isNumber(x) {
     64   return typeof x === 'number';
     65 }
     66 
     67 function isBigNumber(x) {
     68   if (!x || (0, _typeof2.default)(x) !== 'object' || typeof x.constructor !== 'function') {
     69     return false;
     70   }
     71 
     72   if (x.isBigNumber === true && (0, _typeof2.default)(x.constructor.prototype) === 'object' && x.constructor.prototype.isBigNumber === true) {
     73     return true;
     74   }
     75 
     76   if (typeof x.constructor.isDecimal === 'function' && x.constructor.isDecimal(x) === true) {
     77     return true;
     78   }
     79 
     80   return false;
     81 }
     82 
     83 function isComplex(x) {
     84   return x && (0, _typeof2.default)(x) === 'object' && Object.getPrototypeOf(x).isComplex === true || false;
     85 }
     86 
     87 function isFraction(x) {
     88   return x && (0, _typeof2.default)(x) === 'object' && Object.getPrototypeOf(x).isFraction === true || false;
     89 }
     90 
     91 function isUnit(x) {
     92   return x && x.constructor.prototype.isUnit === true || false;
     93 }
     94 
     95 function isString(x) {
     96   return typeof x === 'string';
     97 }
     98 
     99 var isArray = Array.isArray;
    100 exports.isArray = isArray;
    101 
    102 function isMatrix(x) {
    103   return x && x.constructor.prototype.isMatrix === true || false;
    104 }
    105 /**
    106  * Test whether a value is a collection: an Array or Matrix
    107  * @param {*} x
    108  * @returns {boolean} isCollection
    109  */
    110 
    111 
    112 function isCollection(x) {
    113   return Array.isArray(x) || isMatrix(x);
    114 }
    115 
    116 function isDenseMatrix(x) {
    117   return x && x.isDenseMatrix && x.constructor.prototype.isMatrix === true || false;
    118 }
    119 
    120 function isSparseMatrix(x) {
    121   return x && x.isSparseMatrix && x.constructor.prototype.isMatrix === true || false;
    122 }
    123 
    124 function isRange(x) {
    125   return x && x.constructor.prototype.isRange === true || false;
    126 }
    127 
    128 function isIndex(x) {
    129   return x && x.constructor.prototype.isIndex === true || false;
    130 }
    131 
    132 function isBoolean(x) {
    133   return typeof x === 'boolean';
    134 }
    135 
    136 function isResultSet(x) {
    137   return x && x.constructor.prototype.isResultSet === true || false;
    138 }
    139 
    140 function isHelp(x) {
    141   return x && x.constructor.prototype.isHelp === true || false;
    142 }
    143 
    144 function isFunction(x) {
    145   return typeof x === 'function';
    146 }
    147 
    148 function isDate(x) {
    149   return x instanceof Date;
    150 }
    151 
    152 function isRegExp(x) {
    153   return x instanceof RegExp;
    154 }
    155 
    156 function isObject(x) {
    157   return !!(x && (0, _typeof2.default)(x) === 'object' && x.constructor === Object && !isComplex(x) && !isFraction(x));
    158 }
    159 
    160 function isNull(x) {
    161   return x === null;
    162 }
    163 
    164 function isUndefined(x) {
    165   return x === undefined;
    166 }
    167 
    168 function isAccessorNode(x) {
    169   return x && x.isAccessorNode === true && x.constructor.prototype.isNode === true || false;
    170 }
    171 
    172 function isArrayNode(x) {
    173   return x && x.isArrayNode === true && x.constructor.prototype.isNode === true || false;
    174 }
    175 
    176 function isAssignmentNode(x) {
    177   return x && x.isAssignmentNode === true && x.constructor.prototype.isNode === true || false;
    178 }
    179 
    180 function isBlockNode(x) {
    181   return x && x.isBlockNode === true && x.constructor.prototype.isNode === true || false;
    182 }
    183 
    184 function isConditionalNode(x) {
    185   return x && x.isConditionalNode === true && x.constructor.prototype.isNode === true || false;
    186 }
    187 
    188 function isConstantNode(x) {
    189   return x && x.isConstantNode === true && x.constructor.prototype.isNode === true || false;
    190 }
    191 
    192 function isFunctionAssignmentNode(x) {
    193   return x && x.isFunctionAssignmentNode === true && x.constructor.prototype.isNode === true || false;
    194 }
    195 
    196 function isFunctionNode(x) {
    197   return x && x.isFunctionNode === true && x.constructor.prototype.isNode === true || false;
    198 }
    199 
    200 function isIndexNode(x) {
    201   return x && x.isIndexNode === true && x.constructor.prototype.isNode === true || false;
    202 }
    203 
    204 function isNode(x) {
    205   return x && x.isNode === true && x.constructor.prototype.isNode === true || false;
    206 }
    207 
    208 function isObjectNode(x) {
    209   return x && x.isObjectNode === true && x.constructor.prototype.isNode === true || false;
    210 }
    211 
    212 function isOperatorNode(x) {
    213   return x && x.isOperatorNode === true && x.constructor.prototype.isNode === true || false;
    214 }
    215 
    216 function isParenthesisNode(x) {
    217   return x && x.isParenthesisNode === true && x.constructor.prototype.isNode === true || false;
    218 }
    219 
    220 function isRangeNode(x) {
    221   return x && x.isRangeNode === true && x.constructor.prototype.isNode === true || false;
    222 }
    223 
    224 function isSymbolNode(x) {
    225   return x && x.isSymbolNode === true && x.constructor.prototype.isNode === true || false;
    226 }
    227 
    228 function isChain(x) {
    229   return x && x.constructor.prototype.isChain === true || false;
    230 }
    231 
    232 function typeOf(x) {
    233   var t = (0, _typeof2.default)(x);
    234 
    235   if (t === 'object') {
    236     // JavaScript types
    237     if (x === null) return 'null';
    238     if (Array.isArray(x)) return 'Array';
    239     if (x instanceof Date) return 'Date';
    240     if (x instanceof RegExp) return 'RegExp'; // math.js types
    241 
    242     if (isBigNumber(x)) return 'BigNumber';
    243     if (isComplex(x)) return 'Complex';
    244     if (isFraction(x)) return 'Fraction';
    245     if (isMatrix(x)) return 'Matrix';
    246     if (isUnit(x)) return 'Unit';
    247     if (isIndex(x)) return 'Index';
    248     if (isRange(x)) return 'Range';
    249     if (isResultSet(x)) return 'ResultSet';
    250     if (isNode(x)) return x.type;
    251     if (isChain(x)) return 'Chain';
    252     if (isHelp(x)) return 'Help';
    253     return 'Object';
    254   }
    255 
    256   if (t === 'function') return 'Function';
    257   return t; // can be 'string', 'number', 'boolean', ...
    258 }