Skip to content

Commit

Permalink
isImmutable created - but need a better name
Browse files Browse the repository at this point in the history
  • Loading branch information
hughfdjackson committed Jun 16, 2013
1 parent 067958f commit f694aec
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/immutable.js
@@ -1,6 +1,22 @@
'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')
array: require('./array'),
isImmutable: isImmutable
}
28 changes: 28 additions & 0 deletions test/immutable-test.js
Expand Up @@ -6,4 +6,32 @@ describe('immutable', function(){
a.equal(im.object, require('../src/object'))
a.equal(im.array, require('../src/array'))
})

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)
})
})
})

0 comments on commit f694aec

Please sign in to comment.