Browse Source

Several fixes, added build script

master
Muthu Kumar 7 years ago
parent
commit
c17edcc699
  1. 1
      .gitignore
  2. 11
      build.sh
  3. 13
      isEq.js
  4. 3668
      package-lock.json
  5. 14
      package.json
  6. 46
      umd/isEq.js
  7. 13
      umd/isEq.min.js

1
.gitignore

@ -0,0 +1 @@
node_modules

11
build.sh

@ -0,0 +1,11 @@
#!/bin/bash
echo "Babelifying..."
npx babel ./isEq.js --presets=env -o ./umd/isEq2.js
echo "Browserifying..."
npx browserify ./umd/isEq2.js -s isEq > ./umd/isEq.js
echo "Minifying output..."
npx uglifyjs ./umd/isEq.js > ./umd/isEq.min.js
echo "Cleaning up..."
rm ./umd/isEq2.js
echo "Done!"

13
isEq.js

@ -24,18 +24,20 @@ const isEq = (item1, item2, compareKeys) => {
&& !Array.isArray(item1))))
return false;
// 'NaN's are special. They aren't equal to each other.
if (typeof item1 === 'number')
if(isNaN(item1) && isNaN(item2))
return true;
else return item1 === item2;
// Since types are already equal, let's find if items are equal.
if ((typeof item1 === 'number') ||
(typeof item1 === 'string') ||
if ((typeof item1 === 'string') ||
(typeof item1 === 'boolean') ||
(item1 === null) ||
(item1 === undefined)) {
return (item1 === item2);
};
// 'NaN's are special. They aren't equal to each other.
if (Number.isNaN(item1) && Number.isNaN(item2)) return true;
// Regexp needs to be Stringified first before comparing equality
if (item1 instanceof RegExp) return String(item1) === String(item2);
@ -72,7 +74,6 @@ const isEq = (item1, item2, compareKeys) => {
// Inequality at this point can be because it's just comparing
// two objects or arrays that may or may not be equivalent.
if (item1[Key] !== item2[Key]) {
if (typeof (item1[Key] === 'object') && typeof (item2[Key] === 'object') ||
(Array.isArray(item1[Key]) && Array.isArray(item2[Key]))) {
if (!isEq(item1[Key], item2[Key])) {

3668
package-lock.json

File diff suppressed because it is too large

14
package.json

@ -1,10 +1,12 @@
{
"name": "@codefeathers/iseq",
"version": "1.0.4",
"version": "1.1.0",
"description": "Deep compare objects and arrays",
"main": "umd/isEq.min.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"build": "./build.sh",
"prepublishOnly": "./build.sh"
},
"repository": {
"type": "git",
@ -25,5 +27,11 @@
"bugs": {
"url": "https://github.com/codefeathers/isEq/issues"
},
"homepage": "https://github.com/codefeathers/isEq#readme"
"homepage": "https://github.com/codefeathers/isEq#readme",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.6.1",
"browserify": "^16.1.1",
"uglify-js": "^3.3.13"
}
}

46
umd/isEq.js

@ -2,7 +2,6 @@
'use strict';
/**
* UMD Module
*
* @author Muthu Kumar <https://mkr.pw>
* isEq(item1, item2 [, compareKeys])
@ -13,40 +12,33 @@
* @param {Array} [compareKeys] - Keys to compare against. Ignores other keys.
*/
const isEq = (item1, item2, compareKeys) => {
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var isEq = function isEq(item1, item2, compareKeys) {
// Returns false if different types are used.
if (typeof item1 !== typeof item2) return false;
if ((typeof item1 === 'undefined' ? 'undefined' : _typeof(item1)) !== (typeof item2 === 'undefined' ? 'undefined' : _typeof(item2))) return false;
// Arrays are of type object, hence let's go one further step
// to prove type inequality
if ((Array.isArray(item1)
&& !Array.isArray(item2))
|| ((Array.isArray(item2)
&& !Array.isArray(item1))))
return false;
if (Array.isArray(item1) && !Array.isArray(item2) || Array.isArray(item2) && !Array.isArray(item1)) return false;
// 'NaN's are special. They aren't equal to each other.
if (typeof item1 === 'number') if (isNaN(item1) && isNaN(item2)) return true;else return item1 === item2;
// Since types are already equal, let's find if items are equal.
if ((typeof item1 === 'number') ||
(typeof item1 === 'string') ||
(typeof item1 === 'boolean') ||
(item1 === null) ||
(item1 === undefined)) {
return (item1 === item2);
if (typeof item1 === 'string' || typeof item1 === 'boolean' || item1 === null || item1 === undefined) {
return item1 === item2;
};
// 'NaN's are special. They aren't equal to each other.
if (Number.isNaN(item1) && Number.isNaN(item2)) return true;
// Regexp needs to be Stringified first before comparing equality
if (item1 instanceof RegExp) return String(item1) === String(item2);
// If none of the above conditions returned, then check if inputs are handlable
if (typeof (item1) !== 'object' || typeof (item2) !== 'object')
throw new Error('[isEq] Unhandleable input!');
if ((typeof item1 === 'undefined' ? 'undefined' : _typeof(item1)) !== 'object' || (typeof item2 === 'undefined' ? 'undefined' : _typeof(item2)) !== 'object') throw new Error('[isEq] Unhandleable input!');
const item1Keys = Object.keys(item1);
const item2Keys = Object.keys(item2);
var item1Keys = Object.keys(item1);
var item2Keys = Object.keys(item2);
// If compare keys array is not given, let's check length of props
// and check second array against first.
@ -57,16 +49,15 @@ const isEq = (item1, item2, compareKeys) => {
};
};
if(!(Array.isArray(compareKeys)))
throw new Error('[isEq] third parameter should be an array of keys!');
if (!Array.isArray(compareKeys)) throw new Error('[isEq] third parameter should be an array of keys!');
// Either both are empty objects or arrays, or compareArray is empty. Return true.
if (compareKeys.length === 0) return true;
// For each key in our reference...
for (let KeyIndex in compareKeys) {
for (var KeyIndex in compareKeys) {
let Key = compareKeys[KeyIndex];
var Key = compareKeys[KeyIndex];
if (Array.isArray(item1[Key]) && Array.isArray(item2[Key])) {
Key = KeyIndex;
};
@ -74,9 +65,7 @@ const isEq = (item1, item2, compareKeys) => {
// Inequality at this point can be because it's just comparing
// two objects or arrays that may or may not be equivalent.
if (item1[Key] !== item2[Key]) {
if (typeof (item1[Key] === 'object') && typeof (item2[Key] === 'object') ||
(Array.isArray(item1[Key]) && Array.isArray(item2[Key]))) {
if (_typeof(item1[Key] === 'object') && _typeof(item2[Key] === 'object') || Array.isArray(item1[Key]) && Array.isArray(item2[Key])) {
if (!isEq(item1[Key], item2[Key])) {
return false;
};
@ -90,5 +79,6 @@ const isEq = (item1, item2, compareKeys) => {
};
module.exports = isEq;
},{}]},{},[1])(1)
});

13
umd/isEq.min.js

@ -1,12 +1 @@
/**
*
* @author Muthu Kumar <https://mkr.pw>
* isEq(item1, item2 [, compareKeys])
* @description Deep compares objects or arrays and returns boolean.
* Supports Number, String, Boolean, Regexp, Objects, Arrays
* @param {*} item1 - Object or Array to compare against.
* @param {*} item2 - Object or Array to compare with.
* @param {Array} [compareKeys] - Keys to compare against. Ignores other keys.
*/
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.isEq=f()}})(function(){var define,module,exports;return(function(){function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}return e})()({1:[function(require,module,exports){'use strict';const isEq=(item1,item2,compareKeys)=>{if(typeof item1!==typeof item2)return!1;if((Array.isArray(item1)&&!Array.isArray(item2))||((Array.isArray(item2)&&!Array.isArray(item1))))return!1;if((typeof item1==='number')||(typeof item1==='string')||(typeof item1==='boolean')||(item1===null)||(item1===undefined)){return(item1===item2)};if(Number.isNaN(item1)&&Number.isNaN(item2))return!0;if(item1 instanceof RegExp)return String(item1)===String(item2);if(typeof(item1)!=='object'||typeof(item2)!=='object')
throw new Error('[isEq] Unhandleable input!');const item1Keys=Object.keys(item1);const item2Keys=Object.keys(item2);if(!compareKeys){compareKeys=item1Keys;if(item1Keys.length!==item2Keys.length){return!1}};if(!(Array.isArray(compareKeys)))throw new Error('[isEq] third parameter should be an array of keys!');if(compareKeys.length===0)return!0;for(let KeyIndex in compareKeys){let Key=compareKeys[KeyIndex];if(Array.isArray(item1[Key])&&Array.isArray(item2[Key])){Key=KeyIndex};if(item1[Key]!==item2[Key]){if(typeof(item1[Key]==='object')&&typeof(item2[Key]==='object')||(Array.isArray(item1[Key])&&Array.isArray(item2[Key]))){if(!isEq(item1[Key],item2[Key])){return!1}}else{return!1}}};return!0};module.exports=isEq},{}]},{},[1])(1)})
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.isEq=f()}})(function(){var define,module,exports;return function(){function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}return e}()({1:[function(require,module,exports){"use strict";var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj};var isEq=function isEq(item1,item2,compareKeys){if((typeof item1==="undefined"?"undefined":_typeof(item1))!==(typeof item2==="undefined"?"undefined":_typeof(item2)))return false;if(Array.isArray(item1)&&!Array.isArray(item2)||Array.isArray(item2)&&!Array.isArray(item1))return false;if(typeof item1==="number")if(isNaN(item1)&&isNaN(item2))return true;else return item1===item2;if(typeof item1==="string"||typeof item1==="boolean"||item1===null||item1===undefined){return item1===item2}if(item1 instanceof RegExp)return String(item1)===String(item2);if((typeof item1==="undefined"?"undefined":_typeof(item1))!=="object"||(typeof item2==="undefined"?"undefined":_typeof(item2))!=="object")throw new Error("[isEq] Unhandleable input!");var item1Keys=Object.keys(item1);var item2Keys=Object.keys(item2);if(!compareKeys){compareKeys=item1Keys;if(item1Keys.length!==item2Keys.length){return false}}if(!Array.isArray(compareKeys))throw new Error("[isEq] third parameter should be an array of keys!");if(compareKeys.length===0)return true;for(var KeyIndex in compareKeys){var Key=compareKeys[KeyIndex];if(Array.isArray(item1[Key])&&Array.isArray(item2[Key])){Key=KeyIndex}if(item1[Key]!==item2[Key]){if(_typeof(item1[Key]==="object")&&_typeof(item2[Key]==="object")||Array.isArray(item1[Key])&&Array.isArray(item2[Key])){if(!isEq(item1[Key],item2[Key])){return false}}else{return false}}}return true};module.exports=isEq},{}]},{},[1])(1)});

Loading…
Cancel
Save