Skip to content

Commit

Permalink
Add support for Record class
Browse files Browse the repository at this point in the history
  • Loading branch information
zalmoxisus committed Jan 9, 2017
1 parent a72610b commit 2c1477d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var jsan = require('jsan');
var serialize = require('./serialize');

module.exports = function(Immutable) {
module.exports = function(Immutable, refs) {
return {
stringify: function(data) {
return jsan.stringify(data, serialize(Immutable).replacer, null, true);
return jsan.stringify(data, serialize(Immutable, refs).replacer, null, true);
},
parse: function(data) {
return jsan.parse(data, serialize(Immutable).reviver);
return jsan.parse(data, serialize(Immutable, refs).reviver);
},
serialize: serialize
}
Expand Down
20 changes: 17 additions & 3 deletions serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,22 @@ function extract(data, type) {
};
}

module.exports = function serialize(Immutable) {
function refer(data, type, isArray, refs) {
var r = mark(data, type, isArray);
for (var i = 0; i < refs.length; i++) {
var ref = refs[i];
if (typeof ref === 'function' && data instanceof ref) {
r.__remotedevRef__ = i;
return r;
}
}
return r;
}

module.exports = function serialize(Immutable, refs) {
return {
replacer: function(key, value) {
if (value instanceof Immutable.Record) return mark(value, 'ImmutableRecord');
if (value instanceof Immutable.Record) return refer(value, 'ImmutableRecord', false, refs);
if (value instanceof Immutable.Range) return extract(value, 'ImmutableRange');
if (value instanceof Immutable.Repeat) return extract(value, 'ImmutableRepeat');
if (Immutable.OrderedMap.isOrderedMap(value)) return mark(value, 'ImmutableOrderedMap');
Expand All @@ -41,7 +53,9 @@ module.exports = function serialize(Immutable) {
case 'ImmutableOrderedSet': return Immutable.OrderedSet(data);
case 'ImmutableSeq': return Immutable.Seq(data);
case 'ImmutableStack': return Immutable.Stack(data);
default: return Immutable.fromJS(data);
case 'ImmutableRecord':
return (refs && refs[value.__remotedevRef__] || Immutable.Map)(data);
default: return data;
}
}
return value;
Expand Down
2 changes: 2 additions & 0 deletions test/__snapshots__/serialize.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
exports[`Record stringify 1`] = `"{\"data\":{\"a\":1,\"b\":3},\"__remotedevType__\":\"ImmutableRecord\",\"__remotedevRef__\":0}"`;
exports[`Stringify list 1`] = `"{\"data\":[1,2,3,4,5,6,7,8,9,10],\"__remotedevType__\":\"ImmutableList\"}"`;
exports[`Stringify map 1`] = `"{\"data\":{\"a\":1,\"b\":2,\"c\":3,\"d\":4},\"__remotedevType__\":\"ImmutableMap\"}"`;
Expand Down
20 changes: 20 additions & 0 deletions test/serialize.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,23 @@ describe('Parse', function () {
});
});
});

describe('Record', function () {
var ABRecord = Immutable.Record({ a:1, b:2 });
var myRecord = new ABRecord({ b:3 });

var serialize = Serialize(Immutable, [ABRecord]);
var stringify = serialize.stringify;
var parse = serialize.parse;
var stringifiedRecord;

it('stringify', function() {
stringifiedRecord = stringify(myRecord);
expect(stringifiedRecord).toMatchSnapshot();
});

it('parse', function() {
stringifiedRecord = stringify(myRecord);
expect(parse(stringifiedRecord)).toEqual(myRecord);
});
});

0 comments on commit 2c1477d

Please sign in to comment.