Created
February 13, 2015 00:16
-
-
Save gilbox/b110269dd98c19d1c199 to your computer and use it in GitHub Desktop.
Use transit-js Immutable.js with automatic conversion of plain JS objects to immutables
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
var Transit = require('transit-js'), | |
Imm = require('immutable'); | |
// gil: It's possible that the following reader/writer configuration is incomplete. | |
// This is a modified version of: | |
// https://gist.github.com/Tvaroh/52efbe8f4541ca537908 | |
// | |
// ... the original version of this utilized built-in JS (ES6?) types for | |
// better efficiency+speed. However, it didn't work with nested Map + OrderedMap. | |
// Improved efficiency+speed can be achieved by re-implementing with some native types. | |
var reader = Transit.reader('json', { | |
// gil: I commented out arrayBuilder because it breaks OrderedMap handler. | |
// I think that this means that at the top-most level you can't have an array. | |
// | |
//arrayBuilder: { | |
// init: function () { return Imm.List.of().asMutable(); }, | |
// add: function (ret, val) { return ret.push(val); }, | |
// finalize: function (ret) { return ret.asImmutable(); }, | |
// fromArray: function (arr) { return Imm.List(arr); } | |
//}, | |
mapBuilder: { | |
init: function () { return Imm.Map().asMutable(); }, | |
add: function (ret, key, val) { return ret.set(key, val); }, | |
finalize: function (ret) { return ret.asImmutable(); } | |
}, | |
handlers: { | |
iL: function (arr) { | |
return Imm.List(arr); | |
}, | |
iS: function (arr) { | |
return Imm.Set(arr); | |
}, | |
iM: function (o) { | |
return Imm.Map(o); | |
}, | |
iOM: function (arr) { | |
return Imm.OrderedMap(arr); | |
} | |
} | |
}); | |
var writer = Transit.writer("json-verbose", { | |
handlers: Transit.map([ | |
Imm.List, Transit.makeWriteHandler({ | |
tag: function () { return 'iL'; }, | |
rep: function (v) { return v.toArray(); }, | |
stringRep: function () { return null; } | |
}), | |
Imm.Map, Transit.makeWriteHandler({ | |
tag: function () { return 'iM'; }, | |
rep: function (v) { return v.toObject(); }, | |
stringRep: function () { return null; } | |
}), | |
Imm.Set, Transit.makeWriteHandler({ | |
tag: function () { return 'iS'; }, | |
rep: function (v) { return v.toArray(); }, | |
stringRep: function () { return null; } | |
}), | |
Imm.OrderedMap, Transit.makeWriteHandler({ | |
tag: function () { return 'iOM'; }, | |
rep: function (v) { return v.map(function(v,k) { return [k,v] } ).toArray() }, | |
stringRep: function () { return null; } | |
}) | |
]) | |
}); | |
module.exports = { | |
toTransit: function (immutableValue) { | |
return writer.write(immutableValue); | |
}, | |
fromTransit: function (ts) { | |
return reader.read(ts); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment