Skip to content

Commit

Permalink
add delta.partition and bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Oct 18, 2016
1 parent 7096647 commit 970338e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,39 @@ var text = delta.map(function(op) {

---

### partition()

Create an array op two arrays, the first with operations that pass the given function, the other that failed.

#### Methods

- `partition(predicate)`

#### Parameters

- `predicate` - Function to call, passing in the current operation, returning whether that operation passed

#### Returns

- `Array` - A new array of two Arrays, the first with passed operations, the other with failed operations

#### Example

```js
var delta = new Delta().insert('Hello', { bold: true })
.insert({ image: 'https://octodex.github.com/images/labtocat.png' })
.insert('World!');

var results = delta.partition(function(op) {
return typeof op.insert === 'string';
});
var passed = results[0]; // [{ insert: 'Hello', attributes: { bold: true }},
{ insert: 'World'}]
var failed = results[1]; // [{ insert: { image: 'https://octodex.github.com/images/labtocat.png' }}]
```

---

### reduce()

Applies given function against an accumulator and each operation to reduce to a single value.
Expand Down
9 changes: 9 additions & 0 deletions lib/delta.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ Delta.prototype.map = function (predicate) {
return this.ops.map(predicate);
};

Delta.prototype.partition = function (predicate) {
var passed = [], failed = [];
this.forEach(function(op) {
var target = predicate(op) ? passed : failed;
target.push(op);
});
return [passed, failed];
};

Delta.prototype.reduce = function (predicate, initial) {
return this.ops.reduce(predicate, initial);
};
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": "quill-delta",
"version": "3.2.0",
"version": "3.3.0",
"description": "Format for representing rich text documents and changes.",
"author": "Jason Chen <[email protected]>",
"homepage": "https://github.com/quilljs/delta",
Expand Down
9 changes: 9 additions & 0 deletions test/delta/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ describe('helpers', function () {
});
expect(arr).toEqual(['Hello', '', 'World!']);
});

it('partition()', function () {
var arr = this.delta.partition(function (op) {
return typeof op.insert === 'string';
});
var passed = arr[0], failed = arr[1];
expect(passed).toEqual([this.delta.ops[0], this.delta.ops[2]]);
expect(failed).toEqual([this.delta.ops[1]]);
});
});

describe('length()', function () {
Expand Down

0 comments on commit 970338e

Please sign in to comment.