Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

equals

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

equals - npm Package Compare versions

Comparing version 0.2.0 to 0.3.2

index.js

18

package.json
{
"name": "equals",
"version": "0.2.0",
"version": "0.3.2",
"description": "Check if two values are deeply equivalent",
"main": "src/index.js",
"dependencies": {},
"dependencies": {
"type": "https://github.com/jkroso/type/tarball/1.0.1"
},
"devDependencies": {
"mocha": "*",
"chai": "*",
"laissez-faire": "*",
"bigfile": "~0.2.2",
"sourcegraph": "~0.4.1"
"chai": "~1.6.1",
"mocha": "~1.10.0",
"serve": "http://github.com/jkroso/serve/tarball/1.2.4"
},
"scripts": {
"test": "make test"
},
"repository": {

@@ -18,0 +14,0 @@ "type": "git",

@@ -0,39 +1,30 @@

# equals
Check if two values are equivalent. This is a deep equivalence check so if an object has other objects on it properties they must also be equivalent in order for the overall comparison to hold.
compare values of any complexity for equivalence
## Getting Started
With component(1)
_With [component](//github.com/component/component), [packin](//github.com/jkroso/packin) or [npm](//github.com/isaacs/npm)_
`component install jkroso/equals --save`
$ {package mananger} install jkroso/equals
In Node.js
then in your app:
`npm install equals --save`
```js
var equals = require('equals')
```
A pre-built browser version is also [available](https://raw.github.com/jkroso/equals/master/dist/equals.min.js). It exports the global variable equal.
## API
```javascript
var equal = require('equals')
```
- [deepEqual()](#deepequal)
- [objEquiv()](#objequiv)
- [allEqual()](#allequal)
- [make()](#make)
- [equals()](#equals)
- [compare()](#compare)
## deepEqual()
### equals(...)
Primitive types are equal if they represent the same value.
While composite types, i.e. Objects and Arrays, are considered
equal if their structure is the same, i.e. they have the same set of
properties, and the primitive type bound to each property has the
same value. Composite structures can be nested preactically as deep
as you like and circular references are fine
Same structure:
equals takes as many arguments as you like of any type you like and returns a boolean result. Primitive types are equal if they are equal. While composite types, i.e. Objects and Arrays, are considered equal if they have both the same structure and the same content. Specifically that means the same set of keys each pointing to the same values. Composite structures can be as big as you like and and circular references are perfectly safe.
Same structure:
```js
equal(
equals(
{ a : [ 2, 3 ], b : [ 4 ] },

@@ -44,15 +35,14 @@ { a : [ 2, 3 ], b : [ 4 ] }

Different Structure:
Different Structure:
```js
equal(
equals(
{ x : 5, y : [6] },
{ x : 5, y : 6 }
{ x : 5}
) // => false
```
Same structure, different values:
Same structure, different values:
```js
equal(
equals(
{ a: [ 1, 2 ], b : [ 4 ]},

@@ -62,95 +52,30 @@ { a: [ 2, 3 ], b : [ 4 ]}

```
Primitives:
Same values:
```js
equal(new Date(0), new Date(0)) // => true
equals(new Date(0), new Date(0), new Date(1)) // => false
```
Some possible gotchas:
- null is __not__ equal to undefined
- NaN __is__ equal to NaN (normally not the case in JS)
- -0 is equal to +0
- Strings will __not__ coerce to numbers
- Non enumerable properties will not be checked. (They can't be) Though
special exceptions are made for `length` and `constructor` properties
since many common patterns make these normally non-enumerable properties
enumerable
- Arguments objects may differ on callee though this property is
non-enumerable so will not be considered. Usually this is the desired
behavior though so no special case has been made for it.
Some possible gotchas:
- `null` __is not__ equal to `undefined`.
- `NaN` __is__ equal to `NaN` (normally not the case).
- `-0` __is__ equal to `+0`.
- Strings will __not__ coerce to numbers.
- Non enumerable properties will not be checked. They can't be.
- `arguments.callee` is not considered when comparing arguments
## objEquiv()
### compare(a, b)
If you already know your values are non-primitive you can save
processing time by calling `equal.object` directly.
```js
equals.object(
{0:'first', 1: 'second', length:2},
['first', 'second']
) // => true
```
compare two values.
For objects equivalence is determined by having the same number of
enumerable properties, the same set of keys, and equivalent values for
every key.
Note: `length` is always checked even if it isn't enumerable while
`constructor` is never checked:
```js
equals([], {length:0}) // => true`
equals([], {}) // => false`
equals.compare({}, {}) // => true
```
Also note that inherited properties are compared
## Running the tests
## allEqual()
Check that a sequence of values are equal
```js
equal.all(1,1,1,1) // => true
```
## make()
Create a custom version of this module
properties matching `regex` will be excluded
## Contributing
As with all my work this is both a work in progress and a thought in progress. Feel free to chip in in any way you can. Optimisations are welcome so long as they are supported with benchmarks.
## Release History
_(Nothing yet)_
## Credit
A large part of this code was taken from node's assert module
## License
Copyright (c) 2012 Jakeb Rosoman
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.
```bash
$ npm install
$ make test
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc