1 | /*!
|
---|
2 | * https://github.com/Starcounter-Jack/JSON-Patch
|
---|
3 | * (c) 2017-2022 Joachim Wester
|
---|
4 | * MIT licensed
|
---|
5 | */
|
---|
6 | var __extends = (this && this.__extends) || (function () {
|
---|
7 | var extendStatics = function (d, b) {
|
---|
8 | extendStatics = Object.setPrototypeOf ||
|
---|
9 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
---|
10 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
---|
11 | return extendStatics(d, b);
|
---|
12 | };
|
---|
13 | return function (d, b) {
|
---|
14 | extendStatics(d, b);
|
---|
15 | function __() { this.constructor = d; }
|
---|
16 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
---|
17 | };
|
---|
18 | })();
|
---|
19 | var _hasOwnProperty = Object.prototype.hasOwnProperty;
|
---|
20 | export function hasOwnProperty(obj, key) {
|
---|
21 | return _hasOwnProperty.call(obj, key);
|
---|
22 | }
|
---|
23 | export function _objectKeys(obj) {
|
---|
24 | if (Array.isArray(obj)) {
|
---|
25 | var keys_1 = new Array(obj.length);
|
---|
26 | for (var k = 0; k < keys_1.length; k++) {
|
---|
27 | keys_1[k] = "" + k;
|
---|
28 | }
|
---|
29 | return keys_1;
|
---|
30 | }
|
---|
31 | if (Object.keys) {
|
---|
32 | return Object.keys(obj);
|
---|
33 | }
|
---|
34 | var keys = [];
|
---|
35 | for (var i in obj) {
|
---|
36 | if (hasOwnProperty(obj, i)) {
|
---|
37 | keys.push(i);
|
---|
38 | }
|
---|
39 | }
|
---|
40 | return keys;
|
---|
41 | }
|
---|
42 | ;
|
---|
43 | /**
|
---|
44 | * Deeply clone the object.
|
---|
45 | * https://jsperf.com/deep-copy-vs-json-stringify-json-parse/25 (recursiveDeepCopy)
|
---|
46 | * @param {any} obj value to clone
|
---|
47 | * @return {any} cloned obj
|
---|
48 | */
|
---|
49 | export function _deepClone(obj) {
|
---|
50 | switch (typeof obj) {
|
---|
51 | case "object":
|
---|
52 | return JSON.parse(JSON.stringify(obj)); //Faster than ES5 clone - http://jsperf.com/deep-cloning-of-objects/5
|
---|
53 | case "undefined":
|
---|
54 | return null; //this is how JSON.stringify behaves for array items
|
---|
55 | default:
|
---|
56 | return obj; //no need to clone primitives
|
---|
57 | }
|
---|
58 | }
|
---|
59 | //3x faster than cached /^\d+$/.test(str)
|
---|
60 | export function isInteger(str) {
|
---|
61 | var i = 0;
|
---|
62 | var len = str.length;
|
---|
63 | var charCode;
|
---|
64 | while (i < len) {
|
---|
65 | charCode = str.charCodeAt(i);
|
---|
66 | if (charCode >= 48 && charCode <= 57) {
|
---|
67 | i++;
|
---|
68 | continue;
|
---|
69 | }
|
---|
70 | return false;
|
---|
71 | }
|
---|
72 | return true;
|
---|
73 | }
|
---|
74 | /**
|
---|
75 | * Escapes a json pointer path
|
---|
76 | * @param path The raw pointer
|
---|
77 | * @return the Escaped path
|
---|
78 | */
|
---|
79 | export function escapePathComponent(path) {
|
---|
80 | if (path.indexOf('/') === -1 && path.indexOf('~') === -1)
|
---|
81 | return path;
|
---|
82 | return path.replace(/~/g, '~0').replace(/\//g, '~1');
|
---|
83 | }
|
---|
84 | /**
|
---|
85 | * Unescapes a json pointer path
|
---|
86 | * @param path The escaped pointer
|
---|
87 | * @return The unescaped path
|
---|
88 | */
|
---|
89 | export function unescapePathComponent(path) {
|
---|
90 | return path.replace(/~1/g, '/').replace(/~0/g, '~');
|
---|
91 | }
|
---|
92 | export function _getPathRecursive(root, obj) {
|
---|
93 | var found;
|
---|
94 | for (var key in root) {
|
---|
95 | if (hasOwnProperty(root, key)) {
|
---|
96 | if (root[key] === obj) {
|
---|
97 | return escapePathComponent(key) + '/';
|
---|
98 | }
|
---|
99 | else if (typeof root[key] === 'object') {
|
---|
100 | found = _getPathRecursive(root[key], obj);
|
---|
101 | if (found != '') {
|
---|
102 | return escapePathComponent(key) + '/' + found;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 | return '';
|
---|
108 | }
|
---|
109 | export function getPath(root, obj) {
|
---|
110 | if (root === obj) {
|
---|
111 | return '/';
|
---|
112 | }
|
---|
113 | var path = _getPathRecursive(root, obj);
|
---|
114 | if (path === '') {
|
---|
115 | throw new Error("Object not found in root");
|
---|
116 | }
|
---|
117 | return "/" + path;
|
---|
118 | }
|
---|
119 | /**
|
---|
120 | * Recursively checks whether an object has any undefined values inside.
|
---|
121 | */
|
---|
122 | export function hasUndefined(obj) {
|
---|
123 | if (obj === undefined) {
|
---|
124 | return true;
|
---|
125 | }
|
---|
126 | if (obj) {
|
---|
127 | if (Array.isArray(obj)) {
|
---|
128 | for (var i_1 = 0, len = obj.length; i_1 < len; i_1++) {
|
---|
129 | if (hasUndefined(obj[i_1])) {
|
---|
130 | return true;
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 | else if (typeof obj === "object") {
|
---|
135 | var objKeys = _objectKeys(obj);
|
---|
136 | var objKeysLength = objKeys.length;
|
---|
137 | for (var i = 0; i < objKeysLength; i++) {
|
---|
138 | if (hasUndefined(obj[objKeys[i]])) {
|
---|
139 | return true;
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 | return false;
|
---|
145 | }
|
---|
146 | function patchErrorMessageFormatter(message, args) {
|
---|
147 | var messageParts = [message];
|
---|
148 | for (var key in args) {
|
---|
149 | var value = typeof args[key] === 'object' ? JSON.stringify(args[key], null, 2) : args[key]; // pretty print
|
---|
150 | if (typeof value !== 'undefined') {
|
---|
151 | messageParts.push(key + ": " + value);
|
---|
152 | }
|
---|
153 | }
|
---|
154 | return messageParts.join('\n');
|
---|
155 | }
|
---|
156 | var PatchError = /** @class */ (function (_super) {
|
---|
157 | __extends(PatchError, _super);
|
---|
158 | function PatchError(message, name, index, operation, tree) {
|
---|
159 | var _newTarget = this.constructor;
|
---|
160 | var _this = _super.call(this, patchErrorMessageFormatter(message, { name: name, index: index, operation: operation, tree: tree })) || this;
|
---|
161 | _this.name = name;
|
---|
162 | _this.index = index;
|
---|
163 | _this.operation = operation;
|
---|
164 | _this.tree = tree;
|
---|
165 | Object.setPrototypeOf(_this, _newTarget.prototype); // restore prototype chain, see https://stackoverflow.com/a/48342359
|
---|
166 | _this.message = patchErrorMessageFormatter(message, { name: name, index: index, operation: operation, tree: tree });
|
---|
167 | return _this;
|
---|
168 | }
|
---|
169 | return PatchError;
|
---|
170 | }(Error));
|
---|
171 | export { PatchError };
|
---|