javascript - Is there a way to force immutable.js to use string id's? -
i'm using immutable.js, times i'm passing string id's , times i'm passing numeric id's immutables.
as result, immutable.map can have 2 identical children so:
map({ 1: foo, '1': bar, }); which i - don't - want!
unfortunately immutable not strict enough id types, , causes me lot of headache.
is there way force immutable.js convert numeric id's strings before saves'em ?
thank you.
you can create proxy (mdn documentation link) check existing key (whatever type) in object on set accessor, this:
var handler = { set: function(obj, prop, value) { if (obj[string(prop)] == undefined && obj[number(prop)] == undefined) { obj[string(prop)] = value } else { throw new error("already exists") } } }; var p = new proxy({}, handler); p[1] = "test" // p: { 1: "test" } p["2"] = "foo" // p: { 1: "test", 2: "foo" } p["1"] = "bar" // throws "already exists" error
Comments
Post a Comment