Created
September 16, 2019 14:30
-
-
Save peekg/19742cd16135fc33e91efdedb8ed5b4c to your computer and use it in GitHub Desktop.
Immutable Record with an immutable List member
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
(() => { | |
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