├── .editorconfig ├── .gitattributes ├── .gitignore ├── .jshintrc ├── .travis.yml ├── .verb.md ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | 15 | [test/fixtures/*] 16 | insert_final_newline = false 17 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.DS_Store 2 | *.DS_store 3 | *.sublime-* 4 | _gh_pages 5 | bower_components 6 | node_modules 7 | npm-debug.log 8 | actual 9 | test/actual 10 | temp 11 | tmp 12 | TODO.md 13 | vendor -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "asi": false, 3 | "boss": true, 4 | "curly": true, 5 | "eqeqeq": true, 6 | "eqnull": true, 7 | "esnext": true, 8 | "immed": true, 9 | "latedef": false, 10 | "laxcomma": false, 11 | "mocha": true, 12 | "newcap": true, 13 | "noarg": true, 14 | "node": true, 15 | "sub": true, 16 | "undef": true, 17 | "unused": true 18 | } -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - "0.10" 5 | - "0.12" 6 | - "iojs" 7 | git: 8 | depth: 10 -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | # {%= name %} {%= badge("fury") %} {%= badge("travis") %} 2 | 3 | > {%= description %} 4 | 5 | The purpose of this lib is to do the fastest comparison possible of two objects when the values will predictably be primitives. 6 | 7 | - only compares objects. 8 | - only compares the first level of each object 9 | - values must be primitives. If a value is not a primitive, even if the values are the same, `false` is returned. 10 | 11 | {%= include("install-npm", {save: true}) %} 12 | 13 | 14 | ## Usage 15 | 16 | ```js 17 | var equals = require('{%= name %}'); 18 | equals(object_a, object_b); 19 | ``` 20 | 21 | **Examples** 22 | 23 | ```js 24 | equals({a: true, b: true}, {a: true, b: true}); 25 | //=> 'true' 26 | 27 | equals({a: true, b: false}, {c: false, b: false}); 28 | //=> 'false' 29 | 30 | equals({a: true, b: false}, {a: false, b: false}); 31 | //=> 'false' 32 | ``` 33 | 34 | Strict comparison for equality: 35 | 36 | ```js 37 | equals({a: true, b: true}, {a: true, b: 'true'}); 38 | //=> 'false' 39 | ``` 40 | 41 | When values are not primitives, `false` is always returned: 42 | 43 | ```js 44 | equals({ b: {}}, { b: {}}); 45 | //=> 'false' 46 | 47 | equals({ b: []}, { b: []}); 48 | //=> 'false' 49 | ``` 50 | 51 | ## Related projects 52 | {%= verbiage.related.description %} 53 | {%= related(verbiage.related.list) %} 54 | 55 | ## Running tests 56 | {%= include("tests") %} 57 | 58 | ## Contributing 59 | {%= include("contributing") %} 60 | 61 | ## Author 62 | {%= include("author") %} 63 | 64 | ## License 65 | {%= copyright() %} 66 | {%= license() %} 67 | 68 | *** 69 | 70 | {%= include("footer") %} 71 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015, Jon Schlinkert. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # is-equal-shallow [![NPM version](https://badge.fury.io/js/is-equal-shallow.svg)](http://badge.fury.io/js/is-equal-shallow) [![Build Status](https://travis-ci.org/jonschlinkert/is-equal-shallow.svg)](https://travis-ci.org/jonschlinkert/is-equal-shallow) 2 | 3 | > Does a shallow comparison of two objects, returning false if the keys or values differ. 4 | 5 | The purpose of this lib is to do the fastest comparison possible of two objects when the values will predictably be primitives. 6 | 7 | * only compares objects. 8 | * only compares the first level of each object 9 | * values must be primitives. If a value is not a primitive, even if the values are the same, `false` is returned. 10 | 11 | Install with [npm](https://www.npmjs.com/) 12 | 13 | ```sh 14 | $ npm i is-equal-shallow --save 15 | ``` 16 | 17 | ## Usage 18 | 19 | ```js 20 | // legacy cjs require 21 | var equals = require('is-equal-shallow'); 22 | // use es6 import 23 | import equals from 'is-equal-shallow'; 24 | 25 | equals(object_a, object_b); 26 | ``` 27 | 28 | **Examples** 29 | 30 | ```js 31 | equals({a: true, b: true}, {a: true, b: true}); 32 | //=> 'true' 33 | 34 | equals({a: true, b: false}, {c: false, b: false}); 35 | //=> 'false' 36 | 37 | equals({a: true, b: false}, {a: false, b: false}); 38 | //=> 'false' 39 | ``` 40 | 41 | Strict comparison for equality: 42 | 43 | ```js 44 | equals({a: true, b: true}, {a: true, b: 'true'}); 45 | //=> 'false' 46 | ``` 47 | 48 | When values are not primitives, `false` is always returned: 49 | 50 | ```js 51 | equals({ b: {}}, { b: {}}); 52 | //=> 'false' 53 | 54 | equals({ b: []}, { b: []}); 55 | //=> 'false' 56 | ``` 57 | 58 | ## Related projects 59 | 60 | Other object utils: 61 | 62 | * [clone-deep](https://github.com/jonschlinkert/clone-deep): Recursively (deep) clone JavaScript native types, like Object, Array, RegExp, Date as well as primitives. 63 | * [for-in](https://github.com/jonschlinkert/for-in): Iterate over the own and inherited enumerable properties of an objecte, and return an object… [more](https://github.com/jonschlinkert/for-in) 64 | * [for-own](https://github.com/jonschlinkert/for-own): Iterate over the own enumerable properties of an object, and return an object with properties… [more](https://github.com/jonschlinkert/for-own) 65 | * [is-plain-object](https://github.com/jonschlinkert/is-plain-object): Returns true if an object was created by the `Object` constructor. 66 | * [isobject](https://github.com/jonschlinkert/isobject): Returns true if the value is an object and not an array or null. 67 | 68 | ## Running tests 69 | 70 | Install dev dependencies: 71 | 72 | ```sh 73 | $ npm i -d && npm test 74 | ``` 75 | 76 | ## Contributing 77 | 78 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/is-equal-shallow/issues/new) 79 | 80 | ## Author 81 | 82 | **Jon Schlinkert** 83 | 84 | + [github/jonschlinkert](https://github.com/jonschlinkert) 85 | + [twitter/jonschlinkert](http://twitter.com/jonschlinkert) 86 | 87 | ## License 88 | 89 | Copyright © 2015 Jon Schlinkert 90 | Released under the MIT license. 91 | 92 | *** 93 | 94 | _This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on June 22, 2015._ 95 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * is-equal-shallow 3 | * 4 | * Copyright (c) 2015, Jon Schlinkert. 5 | * Licensed under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | var isPrimitive = require('is-primitive'); 11 | 12 | module.exports = function isEqual(a, b) { 13 | if (!a && !b) { return true; } 14 | if (!a && b || a && !b) { return false; } 15 | 16 | var numKeysA = 0, numKeysB = 0, key; 17 | for (key in b) { 18 | numKeysB++; 19 | if (!isPrimitive(b[key]) || !a.hasOwnProperty(key) || (a[key] !== b[key])) { 20 | return false; 21 | } 22 | } 23 | for (key in a) { 24 | numKeysA++; 25 | } 26 | return numKeysA === numKeysB; 27 | }; 28 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "is-equal-shallow", 3 | "description": "Does a shallow comparison of two objects, returning false if the keys or values differ.", 4 | "version": "0.1.3", 5 | "homepage": "https://github.com/jonschlinkert/is-equal-shallow", 6 | "author": { 7 | "name": "Jon Schlinkert", 8 | "url": "https://github.com/jonschlinkert" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git://github.com/jonschlinkert/is-equal-shallow.git" 13 | }, 14 | "bugs": { 15 | "url": "https://github.com/jonschlinkert/is-equal-shallow/issues" 16 | }, 17 | "license": "MIT", 18 | "files": [ 19 | "index.js" 20 | ], 21 | "main": "index.js", 22 | "engines": { 23 | "node": ">=0.10.0" 24 | }, 25 | "scripts": { 26 | "test": "mocha" 27 | }, 28 | "dependencies": { 29 | "is-primitive": "^2.0.0" 30 | }, 31 | "devDependencies": { 32 | "mocha": "*", 33 | "should": "*" 34 | }, 35 | "keywords": [ 36 | "compare", 37 | "comparison", 38 | "equal", 39 | "equals", 40 | "is", 41 | "is-equal", 42 | "key", 43 | "object", 44 | "same", 45 | "shallow", 46 | "value" 47 | ], 48 | "verbiage": { 49 | "related": { 50 | "description": "Other object utils:", 51 | "list": ["is-plain-object", "isobject", "for-in", "for-own", "clone-deep"] 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * is-equal-shallow 3 | * 4 | * Copyright (c) 2015, Jon Schlinkert. 5 | * Licensed under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | /* deps: mocha */ 11 | var assert = require('assert'); 12 | var should = require('should'); 13 | var equals = require('./'); 14 | 15 | describe('equals', function () { 16 | it('should return true when keys and values are the same:', function () { 17 | equals({a: true, b: true}, {a: true, b: true}).should.be.true; 18 | }); 19 | 20 | it('should return false when keys are different:', function () { 21 | equals({a: true, b: false}, {c: false, b: false}).should.be.false; 22 | }); 23 | 24 | it('should return false when values are different:', function () { 25 | equals({a: true, b: false}, {a: false, b: false}).should.be.false; 26 | equals({a: true, b: true}, {a: true, b: 'true'}).should.be.false; 27 | }); 28 | 29 | it('should return false when a value is not a primitive:', function () { 30 | equals({ b: {}}, { b: {}}).should.be.false; 31 | equals({ b: []}, { b: []}).should.be.false; 32 | }); 33 | 34 | it('should return false when a value is present in one object but not the other', function () { 35 | equals({a: true, b: true, c: true}, {a: true, b: true}).should.be.false; 36 | equals({a: true, b: true}, {a: true, b: true, c: true}).should.be.false; 37 | }); 38 | }); 39 | --------------------------------------------------------------------------------