Skip to content

Commit

Permalink
added separate prediate file
Browse files Browse the repository at this point in the history
  • Loading branch information
hughfdjackson committed Jun 16, 2013
1 parent f694aec commit 0ed9a37
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 43 deletions.
16 changes: 1 addition & 15 deletions src/immutable.js
@@ -1,22 +1,8 @@
'use strict'

// The interface all immutable objects implement
var interfaceFns = ['get', 'has', 'assoc', 'dissoc']

// Predicate that indicates whether an object is
var isImmutable = function(o){

if ( typeof o !== 'object' || o === null ) return false

for ( var i = 0, len = interfaceFns.length; i < len; i += 1 )
if ( typeof o[interfaceFns[i]] !== 'function' ) return false

if ( o.immutable !== true ) return false
return true
}

module.exports = {
object: require('./object'),
array: require('./array'),
isImmutable: isImmutable
isImmutable: require('./predicate')
}
16 changes: 16 additions & 0 deletions src/predicate.js
@@ -0,0 +1,16 @@
// The interface all immutable objects implement
var interfaceFns = ['get', 'has', 'assoc', 'dissoc']

// Predicate that indicates whether an object is
var isImmutable = function(o){

if ( typeof o !== 'object' || o === null ) return false

for ( var i = 0, len = interfaceFns.length; i < len; i += 1 )
if ( typeof o[interfaceFns[i]] !== 'function' ) return false

if ( o.immutable !== true ) return false
return true
}

module.exports = isImmutable
29 changes: 1 addition & 28 deletions test/immutable-test.js
Expand Up @@ -5,33 +5,6 @@ describe('immutable', function(){
it('should export API', function(){
a.equal(im.object, require('../src/object'))
a.equal(im.array, require('../src/array'))
a.equal(im.isImmutable, require('../src/predicate'))
})

describe('isImmutable', function(){
it('should return true on immutable objects', function(){
a.equal(im.isImmutable(im.object()), true)
a.equal(im.isImmutable(im.array()), true)
})

it('should return false on data that has an immutable flag', function(){
var fake = { immutable: true }
a.equal(im.isImmutable(im.object()), true)
a.equal(im.isImmutable(im.array()), true)
})

it('should require get, set, has, assoc and dissoc to be functions, along with an immutable flag', function(){
var noop = function(){}
var convincingFake = { immutable: true, get: noop, set: noop, assoc: noop, dissoc: noop, has: noop }

a.equal(im.isImmutable(convincingFake), true)
})

it('should return false on all js primitives', function(){
a.equal(im.isImmutable(null), false)
a.equal(im.isImmutable(undefined), false)
a.equal(im.isImmutable('foo'), false)
a.equal(im.isImmutable(1), false)
a.equal(im.isImmutable(true), false)
})
})
})
33 changes: 33 additions & 0 deletions test/predicate-test.js
@@ -0,0 +1,33 @@
var a = require('assert')
var isImmutable = require('../src/predicate')
var im = require('..')


describe('isImmutable', function(){
it('should return true on immutable objects', function(){
a.equal(isImmutable(im.object()), true)
a.equal(isImmutable(im.array()), true)
})

it('should return false on data that has an immutable flag', function(){
var fake = { immutable: true }
a.equal(isImmutable(fake), false)
a.equal(isImmutable(fake), false)
})

it('should require get, set, has, assoc and dissoc to be functions, along with an immutable flag', function(){
var noop = function(){}
var convincingFake = { immutable: true, get: noop, set: noop, assoc: noop, dissoc: noop, has: noop }

a.equal(isImmutable(convincingFake), true)
})

it('should return false on all js primitives', function(){
a.equal(isImmutable(null), false)
a.equal(isImmutable(undefined), false)
a.equal(isImmutable('foo'), false)
a.equal(isImmutable(1), false)
a.equal(isImmutable(true), false)
})
})

0 comments on commit 0ed9a37

Please sign in to comment.