diff --git a/README.md b/README.md index 67e8bab..9a8f47a 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ JSON8 Patch passes the entire [json-patch-tests](https://github.com/json-patch/j * [revert](#revert) * [diff](#diff) * [valid](#valid) + * [concat](#concat) * [Operations](#operations) * [add](#add) * [remove](#remove) @@ -137,6 +138,23 @@ ooPatch.valid([{op: "add", path: "/foo", value: "bar"}]) // true [↑](#json8-patch) +## concat + +Concats multiple patches into one. + +```javascript +var patch1 = [{op: "add", value: "bar", path: "/foo"}] +var patch2 = [{op: "remove", path: "/foo"}] +var patch = ooPatch.concat(patch1, patch2) + +// patch is +[ + {op: "add", value: "bar", path: "/foo"}, + {op: "remove", path: "/foo"} +] +``` + +[↑](#json8-patch) ## Operations diff --git a/index.js b/index.js index 3bbbcb6..17d6b0d 100644 --- a/index.js +++ b/index.js @@ -23,3 +23,6 @@ module.exports.has = require('./lib/has') // Packing module.exports.pack = require('./lib/pack') module.exports.unpack = require('./lib/unpack') + +// Utilities +module.exports.concat = require('./lib/concat') diff --git a/lib/concat.js b/lib/concat.js new file mode 100644 index 0000000..2032f72 --- /dev/null +++ b/lib/concat.js @@ -0,0 +1,9 @@ +'use strict' + +/** + * concat (or merge) multiple patches into one + * @returns {Array} + */ +module.exports = function concat(/* patch0, patch1, ... */) { + return [].concat.apply([], arguments) +} diff --git a/test/concat.js b/test/concat.js new file mode 100644 index 0000000..06fce95 --- /dev/null +++ b/test/concat.js @@ -0,0 +1,14 @@ +'use strict' + +import assert from 'assert' +import concat from '../lib/concat' + +describe('concat', () => { + it('concats the patches into a bigger patch', () => { + const patch0 = [{}] + const patch1 = [{}] + const patch2 = [{}] + const patch = concat(patch0, patch1, patch2) + assert.deepEqual(patch, [{}, {}, {}]) + }) +})