Permalink
Browse files

add is.object

  • Loading branch information...
1 parent 003da69 commit fb205f3905ddcc921fac44b7e397be96c1247278 @mrDarcyMurphy committed Sep 25, 2013
Showing with 227 additions and 80 deletions.
  1. +14 −13 README.md
  2. +19 −11 lib/is.js
  3. +194 −0 test/objects.js
  4. +0 −56 test/plainObject.js
View
@@ -1,8 +1,8 @@
# is-it
-A simple validation module striving for code minimalism, a clear declarative syntax, and a thorough test suite.
+A simple comparison module striving for code minimalism and a thorough test suite.
-Pairs with [prove](https://github.com/mikefrey/prove) for validation sugar.
+Pair `is` with [the-thing-is](https://github.com/mrDarcyMurphy/the-thing-is) for broader validation.
[![browser support](http://ci.testling.com/mrDarcyMurphy/is.png)](http://ci.testling.com/mrDarcyMurphy/is)
@@ -27,34 +27,36 @@ if (is.present(metal) && is.aNumber(metal)) {
For now, check the source and tests for a full run down of how things work.
-## Types of Validations
-
+## Comparisons
### Operators
+* `present`
* `equal`, `eq`
* `greaterThan`, `gt`
* `greaterThanOrEqualTo`, `gte`
* `lessThan`, `lt`
* `lessThanOrEqualTo`, `lte`
-
### Primitives
-* `array`, `arr`
* `boolean`, `bool`
-* `func`
* `nil`
* `undef`
-* `present`
+### Objects
+
+* `object`, `obj`
+* `plainObject`
+* `args`
+* `array`, `arr`
+* `func`
### Strings
* `string`, `str`
* `emptyString`, `emtStr`
-
### Numbers
* `number`, `num`
@@ -64,7 +66,6 @@ For now, check the source and tests for a full run down of how things work.
* `zero`
* `aZero`
-
### Dates
* `date`
@@ -73,7 +74,6 @@ For now, check the source and tests for a full run down of how things work.
* `futureDate`, `future`
* `pastDate`, `past`
-
### Antonyms
`is.not[methodName]` will return the inverse all methods.
@@ -82,6 +82,7 @@ For now, check the source and tests for a full run down of how things work.
is.not.greaterThan(1, 2) // true : 1 is not greater than 2
```
+## Helpers
-[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/mrDarcyMurphy/is/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
-
+* `toInteger`, `toInt`: converts a number to an integer, and `NaN` to `null` because you can do math with `null`.
+* `toNumber`, `toNum`: same, but respects decimals.
View
@@ -18,6 +18,16 @@
// OPERATORS
/**
+ * Is the subject not null, undefined, or an empty string?
+ *
+ * @method present
+ * @param {Object} subject, required
+ */
+ present: function(x) {
+ return x != null && !is.emtStr(x)
+ },
+
+ /**
* Is the subject loosely equivalent to the standard?
*
* @method equal
@@ -121,8 +131,13 @@
return x === void 0
},
+
// OBJECTS
+ object: function(x) {
+ return typeof x == 'object'
+ },
+
/**
* Is the subject a plain Object?
*
@@ -131,7 +146,7 @@
*/
plainObject: function(x) {
// covers most things
- if (!x || typeof x != 'object' || x.nodeType || is.args(x) || is.arr(x) || is.bool(x) || is.date(x) || is.func(x) || is.num(x) || is.str(x))
+ if (!x || !is.obj(x) || x.nodeType || is.args(x) || is.arr(x) || is.bool(x) || is.date(x) || is.func(x) || is.num(x) || is.str(x))
return false
// Object.create(null) creates objects w/o `valueOf` which throws when the object is an argument of isNaN
@@ -169,15 +184,6 @@
return typeof x == 'function'
},
- /**
- * Is the subject not null, undefined, or an empty string?
- *
- * @method present
- * @param {Object} subject, required
- */
- present: function(x) {
- return x != null && !is.emtStr(x)
- },
// STRINGS
@@ -336,9 +342,11 @@
is.lt = is.lessThan
is.lte = is.lessThanOrEqualTo
- is.arr = is.array
is.bool = is.boolean
+ is.obj = is.object
+ is.arr = is.array
+
is.str = is.string
is.emtStr = is.emptyString
View
@@ -0,0 +1,194 @@
+var assert = require('assert')
+var is = require('../lib/is')
+
+var fn = function(){}
+var exp = (666).toExponential()
+var hex = 0x1
+
+describe('objects', function(){
+
+ it('is.object', function(){
+ assert.equal(false, is.object( fn ), 'fn should be false')
+ assert.equal(true , is.object( new Date() ), 'new Date() should be true ')
+ assert.equal(false, is.object( undefined ), 'undefined should be false')
+ assert.equal(false, is.object( NaN ), 'NaN should be false')
+ assert.equal(true , is.object( null ), 'null should be true ')
+ assert.equal(false, is.object( true ), 'true should be false')
+ assert.equal(false, is.object( false ), 'false should be false')
+ assert.equal(true , is.object( Object.create(null) ), 'Object.create(null) should be true ')
+ assert.equal(true , is.object( Object.create(Object.prototype) ), 'Object.create(Object.prototype) should be true ')
+ assert.equal(true , is.object( new Object() ), 'new Object() should be true ')
+ assert.equal(true , is.object( {} ), '{} should be true ')
+ assert.equal(true , is.object( {foo:"bar"} ), '{foo:"bar"} should be true ')
+ assert.equal(true , is.object( {length:1} ), '{length:1} should be true ')
+ assert.equal(true , is.object( new Array() ), 'new Array() should be true ')
+ assert.equal(true , is.object( [] ), '[] should be true ')
+ assert.equal(true , is.object( ["foo", "bar"] ), '["foo", "bar"] should be true ')
+ assert.equal(true , is.object( arguments ), 'arguments should be true ')
+ assert.equal(false, is.object( "" ), '"" should be false')
+ assert.equal(false, is.object( " " ), '" " should be false')
+ assert.equal(false, is.object( "asdf" ), '"asdf" should be false')
+ assert.equal(false, is.object( "1.23" ), '"1.23" should be false')
+ assert.equal(false, is.object( "-42" ), '"-42" should be false')
+ assert.equal(false, is.object( "-42.01" ), '"-42.01" should be false')
+ assert.equal(false, is.object( "-1" ), '"-1" should be false')
+ assert.equal(false, is.object( "0" ), '"0" should be false')
+ assert.equal(false, is.object( "666" ), '"666" should be false')
+ assert.equal(false, is.object( 1.23 ), '1.23 should be false')
+ assert.equal(false, is.object( -42 ), '-42 should be false')
+ assert.equal(false, is.object( -1 ), '-1 should be false')
+ assert.equal(false, is.object( 0 ), '0 should be false')
+ assert.equal(false, is.object( 666 ), '666 should be false')
+ assert.equal(false, is.object( -exp ), '-exp should be false')
+ assert.equal(false, is.object( exp ), 'exp should be false')
+ assert.equal(false, is.object( +exp ), '+exp should be false')
+ assert.equal(false, is.object( -hex ), '-hex should be false')
+ assert.equal(false, is.object( hex ), 'hex should be false')
+ assert.equal(false, is.object( +hex ), '+hex should be false')
+ assert.equal(false, is.object( -Infinity ), '-Infinity should be false')
+ assert.equal(false, is.object( Infinity ), 'Infinity should be false')
+ assert.equal(false, is.object( +Infinity ), '+Infinity should be false')
+ assert.equal(false, is.object( "-Infinity" ), '"-Infinity" should be false')
+ assert.equal(false, is.object( "Infinity" ), '"Infinity" should be false')
+ assert.equal(false, is.object( "+Infinity" ), '"+Infinity" should be false')
+ })
+
+ it('is.not.object', function(){
+ assert.equal(true , is.not.object( fn ), 'fn should be true ')
+ assert.equal(false, is.not.object( new Date() ), 'new Date() should be false')
+ assert.equal(true , is.not.object( undefined ), 'undefined should be true ')
+ assert.equal(true , is.not.object( NaN ), 'NaN should be true ')
+ assert.equal(false, is.not.object( null ), 'null should be false')
+ assert.equal(true , is.not.object( true ), 'true should be true ')
+ assert.equal(true , is.not.object( false ), 'false should be true ')
+ assert.equal(false, is.not.object( Object.create(null) ), 'Object.create(null) should be false')
+ assert.equal(false, is.not.object( Object.create(Object.prototype) ), 'Object.create(Object.prototype) should be false')
+ assert.equal(false, is.not.object( new Object() ), 'new Object() should be false')
+ assert.equal(false, is.not.object( {} ), '{} should be false')
+ assert.equal(false, is.not.object( {foo:"bar"} ), '{foo:"bar"} should be false')
+ assert.equal(false, is.not.object( {length:1} ), '{length:1} should be false')
+ assert.equal(false, is.not.object( new Array() ), 'new Array() should be false')
+ assert.equal(false, is.not.object( [] ), '[] should be false')
+ assert.equal(false, is.not.object( ["foo", "bar"] ), '["foo", "bar"] should be false')
+ assert.equal(false, is.not.object( arguments ), 'arguments should be false')
+ assert.equal(true , is.not.object( "" ), '"" should be true')
+ assert.equal(true , is.not.object( " " ), '" " should be true')
+ assert.equal(true , is.not.object( "asdf" ), '"asdf" should be true')
+ assert.equal(true , is.not.object( "1.23" ), '"1.23" should be true')
+ assert.equal(true , is.not.object( "-42" ), '"-42" should be true')
+ assert.equal(true , is.not.object( "-42.01" ), '"-42.01" should be true')
+ assert.equal(true , is.not.object( "-1" ), '"-1" should be true')
+ assert.equal(true , is.not.object( "0" ), '"0" should be true')
+ assert.equal(true , is.not.object( "666" ), '"666" should be true')
+ assert.equal(true , is.not.object( 1.23 ), '1.23 should be true')
+ assert.equal(true , is.not.object( -42 ), '-42 should be true')
+ assert.equal(true , is.not.object( -1 ), '-1 should be true')
+ assert.equal(true , is.not.object( 0 ), '0 should be true')
+ assert.equal(true , is.not.object( 666 ), '666 should be true')
+ assert.equal(true , is.not.object( -exp ), '-exp should be true')
+ assert.equal(true , is.not.object( exp ), 'exp should be true')
+ assert.equal(true , is.not.object( +exp ), '+exp should be true')
+ assert.equal(true , is.not.object( -hex ), '-hex should be true')
+ assert.equal(true , is.not.object( hex ), 'hex should be true')
+ assert.equal(true , is.not.object( +hex ), '+hex should be true')
+ assert.equal(true , is.not.object( -Infinity ), '-Infinity should be true')
+ assert.equal(true , is.not.object( Infinity ), 'Infinity should be true')
+ assert.equal(true , is.not.object( +Infinity ), '+Infinity should be true')
+ assert.equal(true , is.not.object( "-Infinity" ), '"-Infinity" should be true')
+ assert.equal(true , is.not.object( "Infinity" ), '"Infinity" should be true')
+ assert.equal(true , is.not.object( "+Infinity" ), '"+Infinity" should be true')
+ })
+
+ it('is.plainObject', function(){
+ assert.equal(false, is.plainObject( fn ), 'fn should be false')
+ assert.equal(false, is.plainObject( new Date() ), 'new Date() should be false')
+ assert.equal(false, is.plainObject( undefined ), 'undefined should be false')
+ assert.equal(false, is.plainObject( NaN ), 'NaN should be false')
+ assert.equal(false, is.plainObject( null ), 'null should be false')
+ assert.equal(false, is.plainObject( true ), 'true should be false')
+ assert.equal(false, is.plainObject( false ), 'false should be false')
+ assert.equal(true , is.plainObject( Object.create(null) ), 'Object.create(null) should be true ')
+ assert.equal(true , is.plainObject( Object.create(Object.prototype) ), 'Object.create(Object.prototype) should be true ')
+ assert.equal(true , is.plainObject( new Object() ), 'new Object() should be true ')
+ assert.equal(true , is.plainObject( {} ), '{} should be true ')
+ assert.equal(true , is.plainObject( {foo:"bar"} ), '{foo:"bar"} should be true ')
+ assert.equal(true , is.plainObject( {length:1} ), '{length:1} should be true ')
+ assert.equal(false, is.plainObject( new Array() ), 'new Array() should be false')
+ assert.equal(false, is.plainObject( [] ), '[] should be false')
+ assert.equal(false, is.plainObject( ["foo", "bar"] ), '["foo", "bar"] should be false')
+ assert.equal(false, is.plainObject( arguments ), 'arguments should be false')
+ assert.equal(false, is.plainObject( "" ), '"" should be false')
+ assert.equal(false, is.plainObject( " " ), '" " should be false')
+ assert.equal(false, is.plainObject( "asdf" ), '"asdf" should be false')
+ assert.equal(false, is.plainObject( "1.23" ), '"1.23" should be false')
+ assert.equal(false, is.plainObject( "-42" ), '"-42" should be false')
+ assert.equal(false, is.plainObject( "-42.01" ), '"-42.01" should be false')
+ assert.equal(false, is.plainObject( "-1" ), '"-1" should be false')
+ assert.equal(false, is.plainObject( "0" ), '"0" should be false')
+ assert.equal(false, is.plainObject( "666" ), '"666" should be false')
+ assert.equal(false, is.plainObject( 1.23 ), '1.23 should be false')
+ assert.equal(false, is.plainObject( -42 ), '-42 should be false')
+ assert.equal(false, is.plainObject( -1 ), '-1 should be false')
+ assert.equal(false, is.plainObject( 0 ), '0 should be false')
+ assert.equal(false, is.plainObject( 666 ), '666 should be false')
+ assert.equal(false, is.plainObject( -exp ), '-exp should be false')
+ assert.equal(false, is.plainObject( exp ), 'exp should be false')
+ assert.equal(false, is.plainObject( +exp ), '+exp should be false')
+ assert.equal(false, is.plainObject( -hex ), '-hex should be false')
+ assert.equal(false, is.plainObject( hex ), 'hex should be false')
+ assert.equal(false, is.plainObject( +hex ), '+hex should be false')
+ assert.equal(false, is.plainObject( -Infinity ), '-Infinity should be false')
+ assert.equal(false, is.plainObject( Infinity ), 'Infinity should be false')
+ assert.equal(false, is.plainObject( +Infinity ), '+Infinity should be false')
+ assert.equal(false, is.plainObject( "-Infinity" ), '"-Infinity" should be false')
+ assert.equal(false, is.plainObject( "Infinity" ), '"Infinity" should be false')
+ assert.equal(false, is.plainObject( "+Infinity" ), '"+Infinity" should be false')
+ })
+
+ it('is.not.plainObject', function(){
+ assert.equal(true , is.not.plainObject( fn ), 'fn should be true ')
+ assert.equal(true , is.not.plainObject( new Date() ), 'new Date() should be true ')
+ assert.equal(true , is.not.plainObject( undefined ), 'undefined should be true ')
+ assert.equal(true , is.not.plainObject( NaN ), 'NaN should be true ')
+ assert.equal(true , is.not.plainObject( null ), 'null should be true ')
+ assert.equal(true , is.not.plainObject( true ), 'true should be true ')
+ assert.equal(true , is.not.plainObject( false ), 'false should be true')
+ assert.equal(false, is.not.plainObject( Object.create(null) ), 'Object.create(null) should be false')
+ assert.equal(false, is.not.plainObject( Object.create(Object.prototype) ), 'Object.create(Object.prototype) should be false')
+ assert.equal(false, is.not.plainObject( new Object() ), 'new Object() should be false')
+ assert.equal(false, is.not.plainObject( {} ), '{} should be false')
+ assert.equal(false, is.not.plainObject( {foo:"bar"} ), '{foo:"bar"} should be false')
+ assert.equal(false, is.not.plainObject( {length:1} ), '{length:1} should be false')
+ assert.equal(true , is.not.plainObject( new Array() ), 'new Array() should be true ')
+ assert.equal(true , is.not.plainObject( [] ), '[] should be true ')
+ assert.equal(true , is.not.plainObject( ["foo", "bar"] ), '["foo", "bar"] should be true ')
+ assert.equal(true , is.not.plainObject( arguments ), 'arguments should be true ')
+ assert.equal(true , is.not.plainObject( "" ), '"" should be true ')
+ assert.equal(true , is.not.plainObject( " " ), '" " should be true ')
+ assert.equal(true , is.not.plainObject( "asdf" ), '"asdf" should be true ')
+ assert.equal(true , is.not.plainObject( "1.23" ), '"1.23" should be true ')
+ assert.equal(true , is.not.plainObject( "-42" ), '"-42" should be true ')
+ assert.equal(true , is.not.plainObject( "-42.01" ), '"-42.01" should be true ')
+ assert.equal(true , is.not.plainObject( "-1" ), '"-1" should be true ')
+ assert.equal(true , is.not.plainObject( "0" ), '"0" should be true ')
+ assert.equal(true , is.not.plainObject( "666" ), '"666" should be true ')
+ assert.equal(true , is.not.plainObject( 1.23 ), '1.23 should be true ')
+ assert.equal(true , is.not.plainObject( -42 ), '-42 should be true ')
+ assert.equal(true , is.not.plainObject( -1 ), '-1 should be true ')
+ assert.equal(true , is.not.plainObject( 0 ), '0 should be true ')
+ assert.equal(true , is.not.plainObject( 666 ), '666 should be true ')
+ assert.equal(true , is.not.plainObject( -exp ), '-exp should be true ')
+ assert.equal(true , is.not.plainObject( exp ), 'exp should be true ')
+ assert.equal(true , is.not.plainObject( +exp ), '+exp should be true ')
+ assert.equal(true , is.not.plainObject( -hex ), '-hex should be true ')
+ assert.equal(true , is.not.plainObject( hex ), 'hex should be true ')
+ assert.equal(true , is.not.plainObject( +hex ), '+hex should be true ')
+ assert.equal(true , is.not.plainObject( -Infinity ), '-Infinity should be true ')
+ assert.equal(true , is.not.plainObject( Infinity ), 'Infinity should be true ')
+ assert.equal(true , is.not.plainObject( +Infinity ), '+Infinity should be true ')
+ assert.equal(true , is.not.plainObject( "-Infinity" ), '"-Infinity" should be true ')
+ assert.equal(true , is.not.plainObject( "Infinity" ), '"Infinity" should be true ')
+ assert.equal(true , is.not.plainObject( "+Infinity" ), '"+Infinity" should be true ')
+ })
+
+})
Oops, something went wrong.

0 comments on commit fb205f3

Please sign in to comment.