-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5824a54
Showing
7 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
.DS_Store | ||
modules | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Alberto Leal <[email protected]> (github.com/dashed) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# shallowequal | ||
|
||
> `shallowequal` is exactly like lodash's `isEqual` but for shallow equal. | ||
`shallowequal(value, other, [customizer], [thisArg])` | ||
|
||
Performs a shallow comparison between two given values to determine if they are equivalent. If `customizer` is provided it is invoked to compare values. If `customizer` returns `undefined` (i.e. `void 0`), then comparisons are handled by the `shallowequal` function instead. The `customizer` is bound to `thisArg` and invoked with three arguments: `(value, other, key)`. | ||
|
||
**NOTE:** Docs is (shamelessly) lifted from [lodash's docs](https://lodash.com/docs#isEqual) | ||
|
||
## Usage | ||
|
||
``` | ||
$ npm install --save shallowequal | ||
``` | ||
|
||
```js | ||
const shallowequal = require('shallowequal'); | ||
|
||
const object = { 'user': 'fred' }; | ||
const other = { 'user': 'fred' }; | ||
|
||
object == other; | ||
// → false | ||
|
||
shallowequal(object, other); | ||
// → true | ||
``` | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "shallowequal", | ||
"version": "0.0.0", | ||
"description": "Like lodash isEqual but for shallow equal", | ||
"main": "modules/index.js", | ||
"scripts": { | ||
"test": "mocha --compilers js:babel/register", | ||
"build": "./scripts/build.sh", | ||
"prepublish": "npm run build" | ||
}, | ||
"author": { | ||
"name": "Alberto Leal", | ||
"email": "[email protected]", | ||
"url": "github.com/Dashed" | ||
}, | ||
"repository": "dashed/shallowequal", | ||
"license": "MIT", | ||
"files": [ | ||
"modules" | ||
], | ||
"keywords": [ | ||
"shallowequal", | ||
"shallow", | ||
"equal", | ||
"isequal", | ||
"compare" | ||
], | ||
"devDependencies": { | ||
"babel": "^5.5.6" | ||
}, | ||
"dependencies": { | ||
"lodash.keys": "^3.1.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env bash | ||
./node_modules/.bin/babel src --out-dir modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
const fetchKeys = require('lodash.keys'); | ||
|
||
function shallowEqual(objA, objB, compare, compareContext) { | ||
if (objA === objB) { | ||
return true; | ||
} | ||
|
||
if (typeof objA !== 'object' || objA === null || | ||
typeof objB !== 'object' || objB === null) { | ||
return false; | ||
} | ||
|
||
const keysA = fetchKeys(objA); | ||
const keysB = fetchKeys(objB); | ||
|
||
if (keysA.length !== keysB.length) { | ||
return false; | ||
} | ||
|
||
compareContext = compareContext || null; | ||
|
||
// Test for A's keys different from B. | ||
const bHasOwnProperty = Object.prototype.hasOwnProperty.bind(objB); | ||
const len = keysA.length; | ||
for (let i = 0; i < len; i++) { | ||
if (!bHasOwnProperty(keysA[i])) { | ||
return false; | ||
} | ||
const key = keysA[i]; | ||
const valueA = objA[key]; | ||
const valueB = objB[key]; | ||
|
||
const ret = compare ? compare.call(compareContext, valueA, valueB, key) : void 0; | ||
if(ret === false || ret === void 0 && valueA !== valueB) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} |