Skip to content

Commit

Permalink
Merge pull request #16 from reedsy/serialize-config
Browse files Browse the repository at this point in the history
✨ Allow configuration of serialized fields
  • Loading branch information
alecgibson authored Aug 25, 2023
2 parents 5f5c95c + 880226f commit 15f1f84
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 8 deletions.
5 changes: 5 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
serializedProperties: {
metadata: true,
},
};
8 changes: 6 additions & 2 deletions lib/delta-with-metadata.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var config = require('./config');

var DeltaWithMetadata = function (obj) {
let ops = [];

Expand All @@ -9,8 +11,10 @@ var DeltaWithMetadata = function (obj) {

this.ops = ops;

if (obj && obj.metadata) {
this.metadata = obj.metadata;
if (!obj) return;

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

Expand Down
13 changes: 9 additions & 4 deletions lib/type.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
var Delta = require('@reedsy/quill-delta');
var DeltaWithMetadata = require('./delta-with-metadata');
const config = require('./config');

module.exports = {
Delta: Delta,
config: config,
type: {
name: 'rich-text',
uri: 'https://github.com/reedsy/rich-text',
Expand Down Expand Up @@ -53,10 +55,13 @@ module.exports = {
},

serialize: function(delta) {
return {
ops: delta.ops,
metadata: delta.metadata,
};
var serialized = {ops: delta.ops};

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

return serialized;
},

deserialize: function(ops) {
Expand Down
5 changes: 3 additions & 2 deletions 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.0.0",
"version": "4.1.0-reedsy.3.1.0",
"description": "OT type for rich text",
"author": "Jason Chen <jhchen7@gmail.com>",
"homepage": "https://github.com/ottypes/rich-text",
Expand All @@ -12,7 +12,8 @@
"chai": "^4.2.0",
"lodash": "^4.17.15",
"mocha": "^6.1.4",
"ot-fuzzer": "^1.3.1"
"ot-fuzzer": "^1.3.1",
"sinon": "^15.2.0"
},
"engines": {
"node": ">=0.10"
Expand Down
66 changes: 66 additions & 0 deletions test/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
var richText = require('../lib/type').type;
var expect = require('chai').expect;
var sinon = require('sinon');
const config = require('../lib/config');

describe('config', function() {
describe('serialization', function() {
it('serializes metadata by default', function() {
var delta = richText.create();
delta.metadata = {foo: '123'};
expect(richText.serialize(delta)).to.eql({
ops: [],
metadata: {foo: '123'},
});
});

it('deserializes metadata by default', function() {
var delta = richText.deserialize({
ops: [],
metadata: {lorem: 'ipsum'},
});

expect(delta.metadata).to.eql({lorem: 'ipsum'});
});

it('does not serialize an unspecified prop', function() {
var delta = richText.create();
delta.$doNotSerialize = {foo: '123'};
expect(richText.serialize(delta)).to.eql({
ops: [],
metadata: undefined,
});
});

it('does not deserialize an unspecified prop', function () {
var delta = richText.deserialize({
ops: [],
$doNotSerialize: {lorem: 'ipsum'},
});

expect(delta).not.to.have.property('$doNotSerialize');
});

it('can specify extra props to serialize using config', function() {
sinon.stub(config, 'serializedProperties').get(() => ({extra: true}));

var delta = richText.create();
delta.extra = {foo: '123'};
expect(richText.serialize(delta)).to.eql({
ops: [],
extra: {foo: '123'},
});
});

it('can specify extra props to deserialize using config', function () {
sinon.stub(config, 'serializedProperties').get(() => ({extra: true}));

var delta = richText.deserialize({
ops: [],
extra: {lorem: 'ipsum'},
});

expect(delta.extra).to.eql({lorem: 'ipsum'});
});
});
});

0 comments on commit 15f1f84

Please sign in to comment.