Befor generating
This commit is contained in:
+21
@@ -0,0 +1,21 @@
|
||||
# Change Log
|
||||
|
||||
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
||||
|
||||
<a name="0.1.2"></a>
|
||||
## [0.1.2](https://github.com/QuentinRoy/tiny-merge-patch/compare/v0.1.1...v0.1.2) (2017-10-09)
|
||||
|
||||
|
||||
|
||||
<a name="0.1.1"></a>
|
||||
## [0.1.1](https://github.com/QuentinRoy/tiny-merge-patch/compare/v0.1.0...v0.1.1) (2017-10-06)
|
||||
|
||||
|
||||
|
||||
<a name="0.1.0"></a>
|
||||
# 0.1.0 (2017-10-06)
|
||||
|
||||
|
||||
### Features
|
||||
|
||||
* start tiny-merge-patch
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Original work Copyright (c) 2015 Pierre Inglebert
|
||||
Copyright (c) 2017 Quentin Roy
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
Tiny Merge Patch
|
||||
================
|
||||
|
||||
[](https://travis-ci.org/QuentinRoy/tiny-merge-patch)
|
||||
[](https://codecov.io/gh/QuentinRoy/tiny-merge-patch)
|
||||
[](https://david-dm.org/quentinroy/tiny-merge-patch)
|
||||
[](https://david-dm.org/quentinroy/tiny-merge-patch?type=dev)
|
||||
|
||||
An implementation of the JSON Merge Patch
|
||||
[RFC 7396](http://tools.ietf.org/html/rfc7396): a standard format used to
|
||||
describe modifications to JSON documents.
|
||||
|
||||
This library complies with the functional programming style: it does not mutate
|
||||
the original target, but recycles what it can.
|
||||
|
||||
`tiny-merge-patch` passes all [RFC 7396](http://tools.ietf.org/html/rfc7396) tests.
|
||||
|
||||
## Install
|
||||
|
||||
Install the current version (and save it as a dependency):
|
||||
|
||||
### npm
|
||||
|
||||
```sh
|
||||
npm install tiny-merge-patch --save
|
||||
```
|
||||
|
||||
## Import
|
||||
|
||||
### CommonJs with node
|
||||
|
||||
```js
|
||||
// Fetch `apply` from the module.
|
||||
const mergePatch = require('tiny-merge-patch').apply;
|
||||
```
|
||||
|
||||
### ES modules in the browser
|
||||
|
||||
```js
|
||||
// `apply` is also the default export.
|
||||
import mergePatch from 'https://unpkg.com/tiny-merge-patch/esm/index.js'
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
// Original document / object.
|
||||
const doc = {
|
||||
a: 'b',
|
||||
c: { d: 'e', f: 'g' },
|
||||
h: { i: 0 }
|
||||
};
|
||||
|
||||
// JSON merge patch to apply.
|
||||
const patch = {
|
||||
a: 'z',
|
||||
c: { f: null } // null marks deletions.
|
||||
};
|
||||
|
||||
// Apply the patch.
|
||||
const patchedDoc = mergePatch(doc, patch);
|
||||
|
||||
// tiny-merge-patch complies with the RFC specification.
|
||||
assert.deepEqual(patchedDoc, {
|
||||
a: 'z',
|
||||
c: { d: 'e' },
|
||||
h: { i: 0 },
|
||||
});
|
||||
|
||||
// Additionally, it does not mutate the original document...
|
||||
assert(patchedDoc !== doc);
|
||||
|
||||
// ...nor its content...
|
||||
assert(patchedDoc.c !== doc.c);
|
||||
|
||||
// ...but recycles what it can.
|
||||
assert(patchedDoc.h === doc.h);
|
||||
```
|
||||
|
||||
## Alternatives
|
||||
|
||||
- [`json-merge-patch`](https://github.com/pierreinglebert/json-merge-patch) (from which this library is forked)
|
||||
|
||||
- [`merge-patch`](https://github.com/krisnye/merge-patch)
|
||||
|
||||
- [`json8-merge-patch`](https://github.com/JSON8/merge-patch)
|
||||
|
||||
All are in-place.
|
||||
To avoid mutations of the original object, one can deep-clone beforehand, but it can be expensive.
|
||||
At the contrary, `tiny-merge-patch` does not alter any of its arguments—but
|
||||
recycles what it can.
|
||||
Recycling also allows efficient strict identity-based memoization
|
||||
(used by [React](https://reactjs.org)'s [PureComponent](https://reactjs.org/docs/react-api.html#reactpurecomponent) for example).
|
||||
|
||||
All of the above libraries also embed additional functionalities, such as patch generation from two objects or merge of patches.
|
||||
`tiny-merge-patch` only focuses on the IETF standard and on patch applications.
|
||||
|
||||
(None of the above libraries are particularly big.
|
||||
Still, `tiny-merge-patch` is smaller if you only need to apply patches.
|
||||
It is also worth mentioning that unlike
|
||||
[JSON patches](https://tools.ietf.org/html/rfc6902), there is no way to
|
||||
implement merge of merge patches that reliably preserves deletion.)
|
||||
|
||||
- [`immutable-merge-patch`](https://www.npmjs.com/package/immutable-merge-patch)
|
||||
|
||||
JSON merge patch implementation for [Immutable.js](https://facebook.github.io/immutable-js/).
|
||||
|
||||
# License
|
||||
|
||||
[MIT](./LICENSE)
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
|
||||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
|
||||
|
||||
exports.apply = apply;
|
||||
/**
|
||||
* Test if a value is a plain object.
|
||||
* @param {*} val - A value.
|
||||
* @return {boolean} true if `val` is a plain object.
|
||||
*/
|
||||
var isObject = function isObject(val) {
|
||||
return val != null && (typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object' && Array.isArray(val) === false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Apply a JSON merge patch. The origin is *not* modified, but unchanged
|
||||
* properties will be recycled.
|
||||
*
|
||||
* @param {*} origin - The value to patch.
|
||||
* @param {*} patch - An [RFC 7396](https://tools.ietf.org/html/rfc7396) patch.
|
||||
* @return {*} The patched value.
|
||||
*/
|
||||
function apply(origin, patch) {
|
||||
if (!isObject(patch)) {
|
||||
// If the patch is not an object, it replaces the origin.
|
||||
return patch;
|
||||
}
|
||||
|
||||
var result = !isObject(origin) ? // Non objects are being replaced.
|
||||
{} : // Make sure we never modify the origin.
|
||||
Object.assign({}, origin);
|
||||
|
||||
Object.keys(patch).forEach(function (key) {
|
||||
var patchVal = patch[key];
|
||||
if (patchVal === null) {
|
||||
delete result[key];
|
||||
} else {
|
||||
result[key] = apply(result[key], patchVal);
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
exports.default = apply;
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Test if a value is a plain object.
|
||||
* @param {*} val - A value.
|
||||
* @return {boolean} true if `val` is a plain object.
|
||||
*/
|
||||
const isObject = val =>
|
||||
val != null && typeof val === 'object' && Array.isArray(val) === false;
|
||||
|
||||
/**
|
||||
* Apply a JSON merge patch. The origin is *not* modified, but unchanged
|
||||
* properties will be recycled.
|
||||
*
|
||||
* @param {*} origin - The value to patch.
|
||||
* @param {*} patch - An [RFC 7396](https://tools.ietf.org/html/rfc7396) patch.
|
||||
* @return {*} The patched value.
|
||||
*/
|
||||
export function apply(origin, patch) {
|
||||
if (!isObject(patch)) {
|
||||
// If the patch is not an object, it replaces the origin.
|
||||
return patch;
|
||||
}
|
||||
|
||||
const result = !isObject(origin)
|
||||
? // Non objects are being replaced.
|
||||
{}
|
||||
: // Make sure we never modify the origin.
|
||||
Object.assign({}, origin);
|
||||
|
||||
Object.keys(patch).forEach(key => {
|
||||
const patchVal = patch[key];
|
||||
if (patchVal === null) {
|
||||
delete result[key];
|
||||
} else {
|
||||
result[key] = apply(result[key], patchVal);
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
export default apply;
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
{
|
||||
"name": "tiny-merge-patch",
|
||||
"version": "0.1.2",
|
||||
"description": "JSON Merge Patch (RFC 7396) Implementation",
|
||||
"main": "./cjs/index.js",
|
||||
"module": "./esm/index.js",
|
||||
"files": [
|
||||
"cjs",
|
||||
"esm"
|
||||
],
|
||||
"devDependencies": {
|
||||
"@commitlint/cli": "^4.1.0",
|
||||
"@commitlint/config-angular": "^3.1.1",
|
||||
"ava": "^0.22.0",
|
||||
"babel-cli": "^6.26.0",
|
||||
"babel-register": "^6.26.0",
|
||||
"babel-plugin-istanbul": "^4.1.5",
|
||||
"babel-preset-env": "^1.6.0",
|
||||
"eslint": "^4.8.0",
|
||||
"eslint-config-airbnb-base": "^12.0.2",
|
||||
"eslint-config-prettier": "^2.6.0",
|
||||
"eslint-plugin-import": "^2.7.0",
|
||||
"eslint-plugin-prettier": "^2.3.1",
|
||||
"husky": "^0.14.3",
|
||||
"istanbul": "^0.4.5",
|
||||
"lodash": "^4.17.4",
|
||||
"nyc": "^11.2.1",
|
||||
"prettier": "^1.7.4",
|
||||
"standard-version": "^4.2.0"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "babel esm -d cjs --extensions \".mjs\",\".js\"",
|
||||
"release": "standard-version",
|
||||
"prepublishOnly": "npm run build",
|
||||
"test": "NODE_ENV=test nyc --reporter=text --reporter=lcovonly --reporter=html --no-cache ava",
|
||||
"lint": "eslint ./esm ./test",
|
||||
"git-hook": "npm run lint && npm run test",
|
||||
"postrewrite": "npm run git-hook",
|
||||
"precommit": "npm run git-hook",
|
||||
"commitmsg": "commitlint -e"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/QuentinRoy/tiny-merge-patch.git"
|
||||
},
|
||||
"keywords": [
|
||||
"JSON",
|
||||
"merge",
|
||||
"patch",
|
||||
"RFC",
|
||||
"7396"
|
||||
],
|
||||
"author": "Quentin Roy <quentin@quentinroy.fr>",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/quentinroy/tiny-merge-patch/issues"
|
||||
},
|
||||
"homepage": "https://github.com/QuentinRoy/tiny-merge-patch",
|
||||
"ava": {
|
||||
"require": [
|
||||
"babel-register"
|
||||
]
|
||||
},
|
||||
"nyc": {
|
||||
"sourceMap": false,
|
||||
"instrument": false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user