Skip to content

Commit

Permalink
handle null attributes
Browse files Browse the repository at this point in the history
fixes #2
  • Loading branch information
jhchen committed Oct 24, 2016
1 parent ec3b6aa commit 9f54786
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/delta.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Delta.prototype.insert = function (text, attributes) {
var newOp = {};
if (text.length === 0) return this;
newOp.insert = text;
if (typeof attributes === 'object' && Object.keys(attributes).length > 0) newOp.attributes = attributes;
if (attributes instanceof Object && Object.keys(attributes).length > 0) newOp.attributes = attributes;
return this.push(newOp);
};

Expand All @@ -35,7 +35,7 @@ Delta.prototype['delete'] = function (length) {
Delta.prototype.retain = function (length, attributes) {
if (length <= 0) return this;
var newOp = { retain: length };
if (typeof attributes === 'object' && Object.keys(attributes).length > 0) newOp.attributes = attributes;
if (attributes instanceof Object && Object.keys(attributes).length > 0) newOp.attributes = attributes;
return this.push(newOp);
};

Expand Down
12 changes: 12 additions & 0 deletions test/delta/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ describe('insert()', function () {
expect(delta.ops[0]).toEqual({ insert: 'test' });
});

it('insert(text, null)', function () {
var delta = new Delta().insert('test', null);
expect(delta.ops.length).toEqual(1);
expect(delta.ops[0]).toEqual({ insert: 'test' });
});

it('insert(embed)', function () {
var delta = new Delta().insert(1);
expect(delta.ops.length).toEqual(1);
Expand Down Expand Up @@ -132,6 +138,12 @@ describe('retain()', function () {
expect(delta.ops[0]).toEqual({ retain: 2 });
});

it('retain(length, null)', function () {
var delta = new Delta().retain(2, null);
expect(delta.ops.length).toEqual(1);
expect(delta.ops[0]).toEqual({ retain: 2 });
});

it('retain(length, attributes)', function () {
var delta = new Delta().retain(1, { bold: true });
expect(delta.ops.length).toEqual(1);
Expand Down

0 comments on commit 9f54786

Please sign in to comment.