mirror of https://github.com/codefeathers/isEq
Muthu Kumar
7 years ago
5 changed files with 187 additions and 96 deletions
@ -0,0 +1,81 @@ |
|||
# isEq |
|||
|
|||
### Deep-compare objects |
|||
|
|||
`isEq` is a very tiny module that deep compares objects or arrays. |
|||
|
|||
Although the intention is deep comparison, `isEq` can compare several datatypes. |
|||
|
|||
## Supports |
|||
|
|||
| Datatype | Support | |
|||
|--------------------- |--- | |
|||
| Number | ✅ | |
|||
| String | ✅ | |
|||
| Boolean | ✅ | |
|||
| Regexp | ✅ | |
|||
| Object | ✅ | |
|||
| Array | ✅ | |
|||
| Cyclic Object/Array | ❌ | |
|||
| Function | ❌ | |
|||
| Symbol | ❌ | |
|||
| Blob | ❌ | |
|||
|
|||
## Installation |
|||
|
|||
Node: |
|||
|
|||
```Shell |
|||
npm install --save iseq |
|||
``` |
|||
|
|||
In the browser: |
|||
```HTML |
|||
<script src="https://unpkg.com/iseq"> |
|||
``` |
|||
|
|||
## Usage |
|||
|
|||
```JavaScript |
|||
isEq(<sourceObject>, <compareObject>, [comparisonKeys]) |
|||
``` |
|||
|
|||
where if comparison keys are not given, `compareObject` is compared against all keys of `sourceObject`. |
|||
|
|||
## Example |
|||
|
|||
So you want to filter all the entities in `Steins;Gate` that have the property `isEvil: true`. |
|||
|
|||
```JavaScript |
|||
var entities = [ |
|||
{ |
|||
name: 'Future Gadget Laboratory', |
|||
isEvil: false, |
|||
after: 'Disrupting the status quo', |
|||
"known-members": [ |
|||
{ name: 'Okabe Rintarou', gender: 'Male', occupation: 'University student' }, |
|||
{ name: 'Mayuri Shiina', gender: 'Female', occupation: 'Maid at May Queen' }, |
|||
{ name: 'Itaru Hashida', gender: 'Male', occupation: 'Hacker' }, |
|||
], |
|||
}, |
|||
{ |
|||
name: 'SERN', |
|||
isEvil: true, |
|||
after: 'IBN 5100', |
|||
"known-members": [ |
|||
{ name: 'Yuugo Tennouji', gender: 'Male', occupation: 'CRT mechanic' }, |
|||
{ name: 'Moeka Kiryū', gender: 'Female', occupation: 'Part-time editor' }, |
|||
], |
|||
}, |
|||
{ |
|||
name: 'Strategic Focus', |
|||
isEvil: true, |
|||
after: 'Time travel', |
|||
"known-members": [ |
|||
{ name: 'Alexis Leskinen', gender: 'Male', occupation: 'Professor' }, |
|||
], |
|||
}, |
|||
] |
|||
|
|||
const evilEntities = entities.filter(eachEntity => isEq(eachEntity, { isEvil: true }, ['isEvil'])); |
|||
``` |
@ -0,0 +1,10 @@ |
|||
/** |
|||
* Written by Muthu Kumar <https://mkr.pw>
|
|||
* Original source: https://github.com/CodeFeathers/isEq
|
|||
* |
|||
* Deep compares objects or arrays and returns boolean. |
|||
* Supports Number, String, Boolean, Regexp, Objects, Arrays. |
|||
* |
|||
* Usage: isEq(<*>, <*>, [compareKeys, ...]); |
|||
*/ |
|||
'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)){if(!Array.isArray(item2)){return false}}if(Array.isArray(item2)){if(!Array.isArray(item1)){return false}}if(typeof item1==='number'||typeof item1==='string'||typeof item1==='boolean'||item1===null||item1===undefined){if(item1===item2){return true}else{return false}};if(item1===NaN&&item2===NaN){return true}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])){var _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;break}}else{return false;break}}};return true};module.exports=isEq; |
Loading…
Reference in new issue