transit-immutable-js
Advanced tools
+81
-54
@@ -41,56 +41,3 @@ var transit = require('transit-js'); | ||
| var writer = transit.writer('json', { | ||
| handlers: transit.map([ | ||
| Immutable.Map, transit.makeWriteHandler({ | ||
| tag: function() { | ||
| return 'iM'; | ||
| }, | ||
| rep: function(m) { | ||
| var i = 0, a = new Array(2 * m.size); | ||
| m.forEach(function(v, k) { | ||
| a[i++] = k; | ||
| a[i++] = v; | ||
| }); | ||
| return a; | ||
| } | ||
| }), | ||
| Immutable.OrderedMap, transit.makeWriteHandler({ | ||
| tag: function() { | ||
| return 'iOM'; | ||
| }, | ||
| rep: function(m) { | ||
| var i = 0, a = new Array(2 * m.size); | ||
| m.forEach(function(v, k) { | ||
| a[i++] = k; | ||
| a[i++] = v; | ||
| }); | ||
| return a; | ||
| } | ||
| }), | ||
| Immutable.List, transit.makeWriteHandler({ | ||
| tag: function() { | ||
| return "iL"; | ||
| }, | ||
| rep: function(v) { | ||
| return v.toArray(); | ||
| } | ||
| }), | ||
| Immutable.Set, transit.makeWriteHandler({ | ||
| tag: function() { | ||
| return "iS"; | ||
| }, | ||
| rep: function(v) { | ||
| return v.toArray(); | ||
| } | ||
| }), | ||
| Function, transit.makeWriteHandler({ | ||
| tag: function() { | ||
| return '_'; | ||
| }, | ||
| rep: function() { | ||
| return null; | ||
| } | ||
| }) | ||
| ]) | ||
| }); | ||
| var writer = createWriter(false); | ||
@@ -106,1 +53,81 @@ exports.toJSON = toJSON; | ||
| } | ||
| function withFilter(predicate) { | ||
| var filteredWriter = createWriter(predicate); | ||
| return { | ||
| toJSON: function(data) { | ||
| return filteredWriter.write(data); | ||
| }, | ||
| fromJSON: fromJSON | ||
| }; | ||
| } | ||
| exports.withFilter = withFilter; | ||
| function createWriter(predicate) { | ||
| return transit.writer('json', { | ||
| handlers: transit.map([ | ||
| Immutable.Map, transit.makeWriteHandler({ | ||
| tag: function() { | ||
| return 'iM'; | ||
| }, | ||
| rep: function(m) { | ||
| var i = 0, a = new Array(2 * m.size); | ||
| if (predicate) { | ||
| m = m.filter(predicate); | ||
| } | ||
| m.forEach(function(v, k) { | ||
| a[i++] = k; | ||
| a[i++] = v; | ||
| }); | ||
| return a; | ||
| } | ||
| }), | ||
| Immutable.OrderedMap, transit.makeWriteHandler({ | ||
| tag: function() { | ||
| return 'iOM'; | ||
| }, | ||
| rep: function(m) { | ||
| var i = 0, a = new Array(2 * m.size); | ||
| if (predicate) { | ||
| m = m.filter(predicate); | ||
| } | ||
| m.forEach(function(v, k) { | ||
| a[i++] = k; | ||
| a[i++] = v; | ||
| }); | ||
| return a; | ||
| } | ||
| }), | ||
| Immutable.List, transit.makeWriteHandler({ | ||
| tag: function() { | ||
| return "iL"; | ||
| }, | ||
| rep: function(v) { | ||
| if (predicate) { | ||
| v = v.filter(predicate); | ||
| } | ||
| return v.toArray(); | ||
| } | ||
| }), | ||
| Immutable.Set, transit.makeWriteHandler({ | ||
| tag: function() { | ||
| return "iS"; | ||
| }, | ||
| rep: function(v) { | ||
| if (predicate) { | ||
| v = v.filter(predicate); | ||
| } | ||
| return v.toArray(); | ||
| } | ||
| }), | ||
| Function, transit.makeWriteHandler({ | ||
| tag: function() { | ||
| return '_'; | ||
| }, | ||
| rep: function() { | ||
| return null; | ||
| } | ||
| }) | ||
| ]) | ||
| }); | ||
| } |
+4
-2
| { | ||
| "name": "transit-immutable-js", | ||
| "version": "0.2.1", | ||
| "version": "0.3.0", | ||
| "description": "Transit serialisation for Immutable.js", | ||
@@ -8,3 +8,4 @@ "main": "index.js", | ||
| "test": "mocha", | ||
| "travis": "istanbul cover _mocha --" | ||
| "lint": "eslint .", | ||
| "travis": "npm run lint && istanbul cover _mocha --" | ||
| }, | ||
@@ -29,2 +30,3 @@ "repository": { | ||
| "coveralls": "^2.11.2", | ||
| "eslint": "^0.24.1", | ||
| "immutable": "^3.7.4", | ||
@@ -31,0 +33,0 @@ "istanbul": "^0.3.14", |
+4
-0
@@ -60,1 +60,5 @@ # transit-immutable-js | ||
| Convert a JSON representation back into an immutable object | ||
| ### `transit.withFilter(function) => transit` | ||
| Create a modified version of the transit API that deeply applies the provided filter function to all immutable collections before serialising. Can be used to exclude entries. |
+41
-0
@@ -104,2 +104,43 @@ /* eslint-env mocha */ | ||
| }); | ||
| describe('.withFilter(predicate)', function(){ | ||
| var filter = transit.withFilter(function(val, key) { | ||
| return key[0] !== '_'; | ||
| }); | ||
| it('can ignore Map entries', function() { | ||
| var input = Immutable.Map({ | ||
| a: 'foo', _b: 'bar', c: Immutable.Map({d: 'deep', _e: 'hide'}) | ||
| }); | ||
| var result = filter.fromJSON(filter.toJSON(input)); | ||
| expect(result.get('a')).to.eql('foo'); | ||
| expect(result.get('_b')).to.eql(undefined); | ||
| expect(result.getIn(['c', 'd'])).to.eql('deep'); | ||
| expect(result.getIn(['c', '_e'])).to.eql(undefined); | ||
| }); | ||
| it('can ignore OrderedMap entries', function() { | ||
| var input = Immutable.OrderedMap() | ||
| .set('a', 'baz').set('_b', 'bar') | ||
| .set('c', Immutable.OrderedMap({d: 'deep', _e: 'hide'})); | ||
| var result = filter.fromJSON(filter.toJSON(input)); | ||
| expect(result.get('a')).to.eql('baz'); | ||
| expect(result.get('_b')).to.eql(undefined); | ||
| expect(result.getIn(['c', 'd'])).to.eql('deep'); | ||
| expect(result.getIn(['c', '_e'])).to.eql(undefined); | ||
| }); | ||
| it('can ignore Set entries', function() { | ||
| var input = Immutable.Set.of(1, 2, 3, 3, 'a'); | ||
| filter = transit.withFilter(function(val) { | ||
| return typeof val === 'number'; | ||
| }); | ||
| var result = filter.fromJSON(filter.toJSON(input)); | ||
| expect(result.includes('a')).to.eql(false); | ||
| }); | ||
| it('can ignore List entries', function() { | ||
| var input = Immutable.List.of(1, 2, 3, 3, 'a'); | ||
| var result = filter.fromJSON(filter.toJSON(input)); | ||
| expect(result.includes('a')).to.eql(false); | ||
| }); | ||
| }); | ||
| }); |
Sorry, the diff of this file is not supported yet
12468
28.13%244
34.81%64
6.67%8
14.29%