Skip to content

Commit

Permalink
Merge pull request #17 from reedsy/apply-extra-props
Browse files Browse the repository at this point in the history
💥 Skip `undefined` serialized props and fix `apply()`
  • Loading branch information
alecgibson authored Aug 28, 2023
2 parents 15f1f84 + 2072fe7 commit dca8c1b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/delta-with-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var DeltaWithMetadata = function (obj) {
if (!obj) return;

for (var key in config.serializedProperties) {
if (config.serializedProperties[key]) this[key] = obj[key];
if (config.serializedProperties[key] && obj[key] !== undefined) this[key] = obj[key];
}
};

Expand Down
13 changes: 5 additions & 8 deletions lib/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@ module.exports = {
},

apply: function (snapshot, delta) {
const metadata = snapshot && snapshot.metadata;

snapshot = new Delta(snapshot);
delta = new Delta(delta);
snapshot = snapshot.compose(delta);
var composed = new Delta(snapshot).compose(delta);

if (metadata) {
snapshot.metadata = metadata;
for (var key in config.serializedProperties) {
if (config.serializedProperties[key] && snapshot[key] !== undefined) composed[key] = snapshot[key];
}

return snapshot;
return composed;
},

compose: function (delta1, delta2) {
Expand Down Expand Up @@ -58,7 +55,7 @@ module.exports = {
var serialized = {ops: delta.ops};

for (var key in config.serializedProperties) {
if (config.serializedProperties[key]) serialized[key] = delta[key];
if (config.serializedProperties[key] && delta[key] !== undefined) serialized[key] = delta[key];
}

return serialized;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@reedsy/rich-text",
"version": "4.1.0-reedsy.3.1.0",
"version": "4.1.0-reedsy.4.0.0",
"description": "OT type for rich text",
"author": "Jason Chen <[email protected]>",
"homepage": "https://github.com/ottypes/rich-text",
Expand Down
47 changes: 46 additions & 1 deletion test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ var sinon = require('sinon');
const config = require('../lib/config');

describe('config', function() {
afterEach(function() {
sinon.restore();
});

describe('serialization', function() {
it('serializes metadata by default', function() {
var delta = richText.create();
Expand All @@ -28,7 +32,6 @@ describe('config', function() {
delta.$doNotSerialize = {foo: '123'};
expect(richText.serialize(delta)).to.eql({
ops: [],
metadata: undefined,
});
});

Expand Down Expand Up @@ -63,4 +66,46 @@ describe('config', function() {
expect(delta.extra).to.eql({lorem: 'ipsum'});
});
});

describe('apply', () => {
it('keeps metadata when applying a delta to a snapshot', function() {
var snapshot = {
ops: [{insert: '\n'}],
metadata: {abc: 123},
};
var delta = {ops: [{insert: 'foo'}]};
var applied = richText.apply(snapshot, delta);
expect(applied).to.eql({
ops: [{insert: 'foo\n'}],
metadata: {abc: 123},
});
});

it('drops unspecified props when applying a delta to a snapshot', function () {
var snapshot = {
ops: [{insert: '\n'}],
extra: {abc: 123},
};
var delta = {ops: [{insert: 'foo'}]};
var applied = richText.apply(snapshot, delta);
expect(applied).to.eql({
ops: [{insert: 'foo\n'}],
});
});

it('can specify extra props to keep when applying a delta to a snapshot', function () {
sinon.stub(config, 'serializedProperties').get(() => ({extra: true}));

var snapshot = {
ops: [{insert: '\n'}],
extra: {abc: 123},
};
var delta = {ops: [{insert: 'foo'}]};
var applied = richText.apply(snapshot, delta);
expect(applied).to.eql({
ops: [{insert: 'foo\n'}],
extra: {abc: 123},
});
});
});
});

0 comments on commit dca8c1b

Please sign in to comment.