Skip to content

Commit

Permalink
mass refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jkroso committed Mar 31, 2013
1 parent 36e32df commit 49599f8
Show file tree
Hide file tree
Showing 13 changed files with 253 additions and 474 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,5 +2,6 @@
/components
/node_modules
/test/built.js
/dist
*.sublime-project
*.sublime-workspace
23 changes: 23 additions & 0 deletions License
@@ -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.
39 changes: 12 additions & 27 deletions Makefile
@@ -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
161 changes: 44 additions & 117 deletions Readme.md
@@ -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)
21 changes: 15 additions & 6 deletions component.json
Expand Up @@ -2,13 +2,22 @@
"name": "equals",
"repo": "jkroso/equals",
"description": "Check if two values are deeply equivalent",
"version": "0.2.0",
"keywords": ["equality", "equal", "same", "equivalent", "equivalence", "=", "equals"],
"dependencies": {},
"version": "0.3.0",
"keywords": [
"equality",
"equal",
"same",
"equivalent",
"equivalence",
"=",
"equals"
],
"dependencies": {
"jkroso/type": "*"
},
"main": "src/index.js",
"scripts": [
"src/index.js"
"index.js"
],
"styles":[],
"license": "MIT"
}
}
1 change: 0 additions & 1 deletion dist/equals.min.js

This file was deleted.

21 changes: 0 additions & 21 deletions docs/head.md

This file was deleted.

33 changes: 0 additions & 33 deletions docs/tail.md

This file was deleted.

0 comments on commit 49599f8

Please sign in to comment.