Skip to content

Instantly share code, notes, and snippets.

@peekg
Created September 16, 2019 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peekg/19742cd16135fc33e91efdedb8ed5b4c to your computer and use it in GitHub Desktop.
Save peekg/19742cd16135fc33e91efdedb8ed5b4c to your computer and use it in GitHub Desktop.
Immutable Record with an immutable List member
(() => {
const {List, Record} = require("immutable")
class Foo extends Record({ list: List() }) {
constructor( props ) {
const inst = super( props )
const list = inst.get( "list" )
if( list instanceof Array ) {
return inst.set( "list", List( list ) )
}
if( !List.isList( list ) ) { throw new Error ( "List member should be type List, or Array" ) }
}
}
assert.ok( List.isList((new Foo()).get("list")) )
assert.ok( List.isList((new Foo( {list: ["one"]} )).get("list")) )
assert.throws( () => new Foo( {list:""} ) )
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment