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
1 parent
f694aec
commit 0ed9a37
Showing
4 changed files
with
51 additions
and
43 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 |
---|---|---|
@@ -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') | ||
} |
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,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 |
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 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,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) | ||
}) | ||
}) | ||
|