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
Showing
13 changed files
with
253 additions
and
474 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 |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
/components | ||
/node_modules | ||
/test/built.js | ||
/dist | ||
*.sublime-project | ||
*.sublime-workspace |
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,23 @@ | ||
|
||
(MIT) | ||
|
||
Copyright (c) 2013 Jake Rosoman <jkroso@gmail.com> | ||
|
||
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 |
---|---|---|
@@ -1,43 +1,28 @@ | ||
EXPORT = equal | ||
SRC = src/*.js | ||
GRAPH = node_modules/.bin/sourcegraph.js src/index.js --plugins=javascript,nodeish | ||
BIGFILE = node_modules/.bin/bigfile -p nodeish --export $(EXPORT) | ||
EXPORT = equals | ||
GRAPH = node_modules/.bin/sourcegraph.js index.js -p javascript,nodeish | ||
BIGFILE = node_modules/.bin/bigfile -p nodeish,javascript --export $(EXPORT) | ||
|
||
all: clean dist dist/equals.min.js test test/built.js Readme.md | ||
all: test/built.js dist/equals.js | ||
|
||
browser: dist/equals.min.js.gz dist/equals.js | ||
browser: dist/equals.js | ||
|
||
dist: | ||
@mkdir dist | ||
@mkdir -p dist | ||
|
||
dist/equals.min.js.gz: dist/equals.min.js | ||
@gzip --best -c dist/equals.min.js > dist/equals.min.js.gz | ||
|
||
dist/equals.min.js: | ||
@$(GRAPH) | $(BIGFILE)\ | ||
--production > dist/equals.min.js | ||
|
||
dist/equals.js: | ||
dist/equals.js: dist index.js | ||
@$(GRAPH) | $(BIGFILE) > dist/equals.js | ||
|
||
test: | ||
@node_modules/.bin/mocha -R spec test/*.test.js | ||
|
||
test/built.js: src/*.js test/*.test.js test/browser.js | ||
test/built.js: index.js test/* | ||
@node_modules/.bin/sourcegraph.js test/browser.js \ | ||
--plugins mocha,nodeish,javascript \ | ||
| node_modules/.bin/bigfile \ | ||
--export null \ | ||
--plugins nodeish > test/built.js | ||
| node_modules/.bin/bigfile.js \ | ||
--export null \ | ||
--plugins nodeish,javascript > $@ | ||
|
||
clean: | ||
@rm -rf dist test/built.js components build | ||
|
||
Readme.md: src/* docs/* | ||
@cat docs/head.md > Readme.md | ||
@cat src/index.js\ | ||
| sed s/.*=.$$//\ | ||
| dox -a >> Readme.md | ||
@cat docs/tail.md >> Readme.md | ||
|
||
.PHONY: all build test clean dist | ||
.PHONY: all build test clean |
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 |
---|---|---|
@@ -1,154 +1,81 @@ | ||
|
||
# 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_ | ||
|
||
`component install jkroso/equals --save` | ||
$ component install jkroso/equals | ||
|
||
In Node.js | ||
_With npm_ | ||
|
||
`npm install equals --save` | ||
$ npm install jkroso/equals --save | ||
|
||
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. | ||
then in your app: | ||
|
||
```js | ||
var equals = require('equals') | ||
``` | ||
|
||
## API | ||
|
||
```javascript | ||
var equal = require('equals') | ||
``` | ||
- [deepEqual()](#deepequal) | ||
- [objEquiv()](#objequiv) | ||
- [allEqual()](#allequal) | ||
- [make()](#make) | ||
|
||
## deepEqual() | ||
|
||
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()](#equals) | ||
|
||
### equals(...) | ||
|
||
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 ] }, | ||
{ a : [ 2, 3 ], b : [ 4 ] } | ||
) // => true | ||
``` | ||
|
||
|
||
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( | ||
{ a: [ 1, 2 ], b : [ 4 ]}, | ||
{ a: [ 2, 3 ], b : [ 4 ]} | ||
) // => false | ||
``` | ||
|
||
|
||
Same values: | ||
```js | ||
equal(new Date(0), new Date(0)) // => true | ||
``` | ||
Primitives: | ||
|
||
|
||
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. | ||
|
||
## objEquiv() | ||
|
||
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 | ||
equal(new Date(0), new Date(0), new Date(1)) // => false | ||
``` | ||
|
||
|
||
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` | ||
``` | ||
|
||
|
||
Also note that inherited properties are compared | ||
|
||
## allEqual() | ||
|
||
Check that a sequence of values are equal | ||
|
||
```js | ||
equal.all(1,1,1,1) // => true | ||
|
||
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 | ||
|
||
## Running the tests | ||
|
||
```bash | ||
$ npm install | ||
$ make | ||
``` | ||
Then open your browser to the `./test` directory. | ||
|
||
## 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: | ||
_Note: these commands don't work on windows._ | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
## License | ||
|
||
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. | ||
[MIT](License) |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.