Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
dashed committed Jun 12, 2015
0 parents commit 5824a54
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
.DS_Store
modules
*.log
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src
21 changes: 21 additions & 0 deletions LICENSE
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.
32 changes: 32 additions & 0 deletions README.md
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
34 changes: 34 additions & 0 deletions package.json
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"
}
}
2 changes: 2 additions & 0 deletions scripts/build.sh
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
41 changes: 41 additions & 0 deletions src/index.js
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;
}

0 comments on commit 5824a54

Please sign in to comment.