simple-squiggle

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

deepEqual.js (2002B)


      1 import { factory } from '../../utils/factory.js';
      2 var name = 'deepEqual';
      3 var dependencies = ['typed', 'equal'];
      4 export var createDeepEqual = /* #__PURE__ */factory(name, dependencies, _ref => {
      5   var {
      6     typed,
      7     equal
      8   } = _ref;
      9 
     10   /**
     11    * Test element wise whether two matrices are equal.
     12    * The function accepts both matrices and scalar values.
     13    *
     14    * Strings are compared by their numerical value.
     15    *
     16    * Syntax:
     17    *
     18    *    math.deepEqual(x, y)
     19    *
     20    * Examples:
     21    *
     22    *    math.deepEqual(2, 4)   // returns false
     23    *
     24    *    a = [2, 5, 1]
     25    *    b = [2, 7, 1]
     26    *
     27    *    math.deepEqual(a, b)   // returns false
     28    *    math.equal(a, b)       // returns [true, false, true]
     29    *
     30    * See also:
     31    *
     32    *    equal, unequal
     33    *
     34    * @param  {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} x First matrix to compare
     35    * @param  {number | BigNumber | Fraction | Complex | Unit | Array | Matrix} y Second matrix to compare
     36    * @return {number | BigNumber | Fraction | Complex | Unit | Array | Matrix}
     37    *            Returns true when the input matrices have the same size and each of their elements is equal.
     38    */
     39   return typed(name, {
     40     'any, any': function anyAny(x, y) {
     41       return _deepEqual(x.valueOf(), y.valueOf());
     42     }
     43   });
     44   /**
     45    * Test whether two arrays have the same size and all elements are equal
     46    * @param {Array | *} x
     47    * @param {Array | *} y
     48    * @return {boolean} Returns true if both arrays are deep equal
     49    */
     50 
     51   function _deepEqual(x, y) {
     52     if (Array.isArray(x)) {
     53       if (Array.isArray(y)) {
     54         var len = x.length;
     55 
     56         if (len !== y.length) {
     57           return false;
     58         }
     59 
     60         for (var i = 0; i < len; i++) {
     61           if (!_deepEqual(x[i], y[i])) {
     62             return false;
     63           }
     64         }
     65 
     66         return true;
     67       } else {
     68         return false;
     69       }
     70     } else {
     71       if (Array.isArray(y)) {
     72         return false;
     73       } else {
     74         return equal(x, y);
     75       }
     76     }
     77   }
     78 });