source: node_modules/fast-json-patch/commonjs/helpers.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

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