Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an isImmutable function #33

Open
hughfdjackson opened this issue Jun 16, 2013 · 7 comments
Open

Add an isImmutable function #33

hughfdjackson opened this issue Jun 16, 2013 · 7 comments

Comments

@hughfdjackson
Copy link
Owner

It's possible that an .immutable flag would be contained in data. For instance:

var myRecord = {
  id: '3232',
  payload: { ... },
  immutable: true
}

As a user, I want to be sure whether or not this is an actual immutable object, or simply data.

Acceptance:
An isImmutable predicate function is created, that confirms that:
*get, has, assoc and dissoc methods are available;

  • the immutable flag is set.
@hughfdjackson
Copy link
Owner Author

isImmutable is a problematic predicate, since primitives and frozen objects are immutable, just not the spawn of this library.

@hughfdjackson
Copy link
Owner Author

A test case to make it obvious what the semantics of, for the pleasure of mr @ljharb

var a = require('assert')
var im = require('..')

describe('immutable', function(){

    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(fake), false)
            a.equal(im.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(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)
        })
    })
})

@hughfdjackson
Copy link
Owner Author

Candidate names:

  • im.is
  • im.isImmutableCollection
  • im.instance
  • im.isInstance
  • im.spawns
  • im.produces

@ljharb
Copy link

ljharb commented Jun 17, 2013

Seems like im.is or im.isInstance makes the most sense to me. PS, in your test, the convincingFake should be returning false, right?

@hughfdjackson
Copy link
Owner Author

@ljharb : convincingFake is meant to be convincing - in that it's an object that adheres to the interface required of an immutable object.

I want to make sure that two modules requiring immutable can interop with each other perfectly (and therefore can't use instanceof), so ducktyping on an 'interface' is the only way I could think of.

@ljharb
Copy link

ljharb commented Jun 17, 2013

ah, ok that makes perfect sense then

@hughfdjackson
Copy link
Owner Author

Am going with isImmutableCollection, since that's the best description of the interface I can come up with.

Can always be aliased later :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants