@karmaniverous/jsonmap 0.2.6 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -8,17 +8,16 @@ var _lodash = _interopRequireDefault(require("lodash.castarray"));
|
|
|
8
8
|
var _lodash2 = _interopRequireDefault(require("lodash.clonedeepwith"));
|
|
9
9
|
var _lodash3 = _interopRequireDefault(require("lodash.get"));
|
|
10
10
|
var _lodash4 = _interopRequireDefault(require("lodash.invoke"));
|
|
11
|
-
var _lodash5 = _interopRequireDefault(require("lodash.
|
|
12
|
-
var _lodash6 = _interopRequireDefault(require("lodash.
|
|
13
|
-
var _lodash7 = _interopRequireDefault(require("lodash.
|
|
14
|
-
var _lodash8 = _interopRequireDefault(require("lodash.
|
|
15
|
-
var _lodash9 = _interopRequireDefault(require("lodash.
|
|
16
|
-
var _lodash10 = _interopRequireDefault(require("lodash.
|
|
17
|
-
var _lodash11 = _interopRequireDefault(require("lodash.
|
|
18
|
-
var _lodash12 = _interopRequireDefault(require("lodash.
|
|
19
|
-
var _lodash13 = _interopRequireDefault(require("lodash.
|
|
20
|
-
var _lodash14 = _interopRequireDefault(require("lodash.
|
|
21
|
-
var _lodash15 = _interopRequireDefault(require("lodash.sortby"));
|
|
11
|
+
var _lodash5 = _interopRequireDefault(require("lodash.isarraylikeobject"));
|
|
12
|
+
var _lodash6 = _interopRequireDefault(require("lodash.isobject"));
|
|
13
|
+
var _lodash7 = _interopRequireDefault(require("lodash.isplainobject"));
|
|
14
|
+
var _lodash8 = _interopRequireDefault(require("lodash.isstring"));
|
|
15
|
+
var _lodash9 = _interopRequireDefault(require("lodash.isundefined"));
|
|
16
|
+
var _lodash10 = _interopRequireDefault(require("lodash.mapvalues"));
|
|
17
|
+
var _lodash11 = _interopRequireDefault(require("lodash.pickby"));
|
|
18
|
+
var _lodash12 = _interopRequireDefault(require("lodash.set"));
|
|
19
|
+
var _lodash13 = _interopRequireDefault(require("lodash.size"));
|
|
20
|
+
var _lodash14 = _interopRequireDefault(require("lodash.sortby"));
|
|
22
21
|
var _nanoid = require("nanoid");
|
|
23
22
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
24
23
|
// npm imports
|
|
@@ -32,12 +31,12 @@ const getJsonFns = () => {
|
|
|
32
31
|
return 'CIRCULAR REFERENCE';
|
|
33
32
|
}
|
|
34
33
|
seen.add(value);
|
|
35
|
-
if (!(0,
|
|
34
|
+
if (!(0, _lodash5.default)(value)) return Object.getOwnPropertyNames(value).reduce((v, p) => ({
|
|
36
35
|
...v,
|
|
37
36
|
[p]: value[p]
|
|
38
37
|
}), {});
|
|
39
38
|
}
|
|
40
|
-
return (0,
|
|
39
|
+
return (0, _lodash9.default)(value) ? undefinedToken : value;
|
|
41
40
|
};
|
|
42
41
|
const reviver = (key, value) => value === undefinedToken ? undefined : value;
|
|
43
42
|
return {
|
|
@@ -82,7 +81,7 @@ class JsonMap {
|
|
|
82
81
|
const result = JSON.parse(JSON.stringify(await this.#transform(this.map, this.input, this.output), replacer), reviver);
|
|
83
82
|
|
|
84
83
|
// Recursively eliminate non-string keys & string keys starting with $.
|
|
85
|
-
const deep = value => (0,
|
|
84
|
+
const deep = value => (0, _lodash7.default)(value) ? (0, _lodash10.default)((0, _lodash11.default)(value, (v, k) => /^[^$]/.test(k)), value => (0, _lodash2.default)(value, deep)) : undefined;
|
|
86
85
|
return (0, _lodash2.default)(result, deep);
|
|
87
86
|
}
|
|
88
87
|
|
|
@@ -98,32 +97,27 @@ class JsonMap {
|
|
|
98
97
|
*/
|
|
99
98
|
async #transform(node, input, output) {
|
|
100
99
|
let path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
|
|
101
|
-
console.debug('#transform params:\n', {
|
|
102
|
-
node,
|
|
103
|
-
input,
|
|
104
|
-
output,
|
|
105
|
-
path
|
|
106
|
-
});
|
|
100
|
+
// console.debug('#transform params:\n', { node, input, output, path });
|
|
107
101
|
|
|
108
102
|
// Checks if the current node is an object and has a '$' key
|
|
109
|
-
if ((0,
|
|
103
|
+
if ((0, _lodash7.default)(node) && (0, _lodash13.default)(node) === 1 && '$' in node) {
|
|
110
104
|
// Retrieves the transformations to be applied (can be an array or a single object)
|
|
111
105
|
const transformations = (0, _lodash.default)(node['$']);
|
|
112
|
-
console.debug('transformations:\n', transformations);
|
|
106
|
+
// console.debug('transformations:\n', transformations);
|
|
113
107
|
|
|
114
108
|
// Array to store the results of the transformations
|
|
115
109
|
let results = [];
|
|
116
110
|
|
|
117
111
|
// Iterates over each transformation
|
|
118
112
|
for (const transformation of transformations) {
|
|
119
|
-
console.debug('processing transformation:\n', transformation);
|
|
113
|
+
// console.debug('processing transformation:\n', transformation);
|
|
120
114
|
|
|
121
115
|
// Resolves the object path for the transformation
|
|
122
116
|
const {
|
|
123
117
|
obj: methodObj,
|
|
124
118
|
path: methodPath
|
|
125
119
|
} = this.#resolvePath(transformation.method, results);
|
|
126
|
-
console.debug('methodObj:\n', methodObj, '\nmethodPath:\n', methodPath);
|
|
120
|
+
// console.debug('methodObj:\n', methodObj, '\nmethodPath:\n', methodPath);
|
|
127
121
|
|
|
128
122
|
// Resolves the parameter paths for the transformation
|
|
129
123
|
const params = (0, _lodash.default)(transformation.params).map(param => {
|
|
@@ -133,29 +127,30 @@ class JsonMap {
|
|
|
133
127
|
} = this.#resolvePath(param, results);
|
|
134
128
|
return paramObj ? paramPath ? (0, _lodash3.default)(paramObj, paramPath) : paramObj : paramPath;
|
|
135
129
|
});
|
|
136
|
-
console.debug('resolved transformation params:\n', params);
|
|
130
|
+
// console.debug('resolved transformation params:\n', params);
|
|
137
131
|
|
|
138
132
|
// Calls the specified method on the resolved object with the resolved parameters
|
|
139
133
|
const result = await (0, _lodash4.default)(methodObj, methodPath, ...params);
|
|
140
|
-
console.debug('transformation result:\n', result);
|
|
134
|
+
// console.debug('transformation result:\n', result);
|
|
141
135
|
|
|
142
136
|
// Stores the result of the transformation
|
|
143
137
|
results.unshift(result);
|
|
144
138
|
}
|
|
145
139
|
|
|
146
140
|
// Sets the output at the specified path to the last result of the transformations & returns.
|
|
147
|
-
(0,
|
|
148
|
-
console.debug('updated output:\n', output);
|
|
141
|
+
(0, _lodash12.default)(output, path, results[0]);
|
|
142
|
+
// console.debug('updated output:\n', output);
|
|
143
|
+
|
|
149
144
|
return results[0];
|
|
150
145
|
}
|
|
151
146
|
|
|
152
147
|
// Checks if the current node is an object
|
|
153
|
-
if ((0,
|
|
148
|
+
if ((0, _lodash6.default)(node)) {
|
|
154
149
|
// Creates an empty array or object based on whether the current node is an array or not
|
|
155
|
-
const transformedNode =
|
|
150
|
+
const transformedNode = Array.isArray(node) ? [] : {};
|
|
156
151
|
|
|
157
152
|
// Iterates over each key-value pair in the current node in ascending order by key
|
|
158
|
-
for (const [key, value] of (0,
|
|
153
|
+
for (const [key, value] of (0, _lodash14.default)(Object.entries(node), _ref => {
|
|
159
154
|
let [key] = _ref;
|
|
160
155
|
return key;
|
|
161
156
|
})) {
|
|
@@ -168,12 +163,12 @@ class JsonMap {
|
|
|
168
163
|
}
|
|
169
164
|
|
|
170
165
|
// Sets the output at the specified path to the transformedNode & returnsd.
|
|
171
|
-
(0,
|
|
166
|
+
(0, _lodash12.default)(output, path, transformedNode);
|
|
172
167
|
return transformedNode;
|
|
173
168
|
}
|
|
174
169
|
|
|
175
170
|
// Sets the output at the specified path to the current node & returns.
|
|
176
|
-
(0,
|
|
171
|
+
(0, _lodash12.default)(output, path, node);
|
|
177
172
|
return node;
|
|
178
173
|
}
|
|
179
174
|
|
|
@@ -194,7 +189,7 @@ class JsonMap {
|
|
|
194
189
|
*/
|
|
195
190
|
#resolvePath(path, results) {
|
|
196
191
|
// If the path is not a string, return it as is
|
|
197
|
-
if (!(0,
|
|
192
|
+
if (!(0, _lodash8.default)(path)) {
|
|
198
193
|
return {
|
|
199
194
|
path
|
|
200
195
|
};
|
package/lib/JsonMap/JsonMap.js
CHANGED
|
@@ -3,7 +3,6 @@ import castArray from 'lodash.castarray';
|
|
|
3
3
|
import cloneDeepWith from 'lodash.clonedeepwith';
|
|
4
4
|
import get from 'lodash.get';
|
|
5
5
|
import invoke from 'lodash.invoke';
|
|
6
|
-
import isArray from 'lodash.isarray';
|
|
7
6
|
import isArrayLikeObject from 'lodash.isarraylikeobject';
|
|
8
7
|
import isObject from 'lodash.isobject';
|
|
9
8
|
import isPlainObject from 'lodash.isplainobject';
|
|
@@ -101,27 +100,27 @@ class JsonMap {
|
|
|
101
100
|
* @private
|
|
102
101
|
*/
|
|
103
102
|
async #transform(node, input, output, path = '') {
|
|
104
|
-
console.debug('#transform params:\n', { node, input, output, path });
|
|
103
|
+
// console.debug('#transform params:\n', { node, input, output, path });
|
|
105
104
|
|
|
106
105
|
// Checks if the current node is an object and has a '$' key
|
|
107
106
|
if (isPlainObject(node) && size(node) === 1 && '$' in node) {
|
|
108
107
|
// Retrieves the transformations to be applied (can be an array or a single object)
|
|
109
108
|
const transformations = castArray(node['$']);
|
|
110
|
-
console.debug('transformations:\n', transformations);
|
|
109
|
+
// console.debug('transformations:\n', transformations);
|
|
111
110
|
|
|
112
111
|
// Array to store the results of the transformations
|
|
113
112
|
let results = [];
|
|
114
113
|
|
|
115
114
|
// Iterates over each transformation
|
|
116
115
|
for (const transformation of transformations) {
|
|
117
|
-
console.debug('processing transformation:\n', transformation);
|
|
116
|
+
// console.debug('processing transformation:\n', transformation);
|
|
118
117
|
|
|
119
118
|
// Resolves the object path for the transformation
|
|
120
119
|
const { obj: methodObj, path: methodPath } = this.#resolvePath(
|
|
121
120
|
transformation.method,
|
|
122
121
|
results
|
|
123
122
|
);
|
|
124
|
-
console.debug('methodObj:\n', methodObj, '\nmethodPath:\n', methodPath);
|
|
123
|
+
// console.debug('methodObj:\n', methodObj, '\nmethodPath:\n', methodPath);
|
|
125
124
|
|
|
126
125
|
// Resolves the parameter paths for the transformation
|
|
127
126
|
const params = castArray(transformation.params).map((param) => {
|
|
@@ -135,11 +134,11 @@ class JsonMap {
|
|
|
135
134
|
: paramObj
|
|
136
135
|
: paramPath;
|
|
137
136
|
});
|
|
138
|
-
console.debug('resolved transformation params:\n', params);
|
|
137
|
+
// console.debug('resolved transformation params:\n', params);
|
|
139
138
|
|
|
140
139
|
// Calls the specified method on the resolved object with the resolved parameters
|
|
141
140
|
const result = await invoke(methodObj, methodPath, ...params);
|
|
142
|
-
console.debug('transformation result:\n', result);
|
|
141
|
+
// console.debug('transformation result:\n', result);
|
|
143
142
|
|
|
144
143
|
// Stores the result of the transformation
|
|
145
144
|
results.unshift(result);
|
|
@@ -147,7 +146,7 @@ class JsonMap {
|
|
|
147
146
|
|
|
148
147
|
// Sets the output at the specified path to the last result of the transformations & returns.
|
|
149
148
|
set(output, path, results[0]);
|
|
150
|
-
console.debug('updated output:\n', output);
|
|
149
|
+
// console.debug('updated output:\n', output);
|
|
151
150
|
|
|
152
151
|
return results[0];
|
|
153
152
|
}
|
|
@@ -155,7 +154,7 @@ class JsonMap {
|
|
|
155
154
|
// Checks if the current node is an object
|
|
156
155
|
if (isObject(node)) {
|
|
157
156
|
// Creates an empty array or object based on whether the current node is an array or not
|
|
158
|
-
const transformedNode = isArray(node) ? [] : {};
|
|
157
|
+
const transformedNode = Array.isArray(node) ? [] : {};
|
|
159
158
|
|
|
160
159
|
// Iterates over each key-value pair in the current node in ascending order by key
|
|
161
160
|
for (const [key, value] of sortBy(Object.entries(node), ([key]) => key)) {
|