@karmaniverous/jsonmap 0.2.4 → 0.2.6
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.
|
@@ -18,6 +18,7 @@ var _lodash11 = _interopRequireDefault(require("lodash.mapvalues"));
|
|
|
18
18
|
var _lodash12 = _interopRequireDefault(require("lodash.pickby"));
|
|
19
19
|
var _lodash13 = _interopRequireDefault(require("lodash.set"));
|
|
20
20
|
var _lodash14 = _interopRequireDefault(require("lodash.size"));
|
|
21
|
+
var _lodash15 = _interopRequireDefault(require("lodash.sortby"));
|
|
21
22
|
var _nanoid = require("nanoid");
|
|
22
23
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
23
24
|
// npm imports
|
|
@@ -97,21 +98,32 @@ class JsonMap {
|
|
|
97
98
|
*/
|
|
98
99
|
async #transform(node, input, output) {
|
|
99
100
|
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
|
+
});
|
|
107
|
+
|
|
100
108
|
// Checks if the current node is an object and has a '$' key
|
|
101
109
|
if ((0, _lodash8.default)(node) && (0, _lodash14.default)(node) === 1 && '$' in node) {
|
|
102
110
|
// Retrieves the transformations to be applied (can be an array or a single object)
|
|
103
111
|
const transformations = (0, _lodash.default)(node['$']);
|
|
112
|
+
console.debug('transformations:\n', transformations);
|
|
104
113
|
|
|
105
114
|
// Array to store the results of the transformations
|
|
106
115
|
let results = [];
|
|
107
116
|
|
|
108
117
|
// Iterates over each transformation
|
|
109
118
|
for (const transformation of transformations) {
|
|
119
|
+
console.debug('processing transformation:\n', transformation);
|
|
120
|
+
|
|
110
121
|
// Resolves the object path for the transformation
|
|
111
122
|
const {
|
|
112
123
|
obj: methodObj,
|
|
113
124
|
path: methodPath
|
|
114
125
|
} = this.#resolvePath(transformation.method, results);
|
|
126
|
+
console.debug('methodObj:\n', methodObj, '\nmethodPath:\n', methodPath);
|
|
115
127
|
|
|
116
128
|
// Resolves the parameter paths for the transformation
|
|
117
129
|
const params = (0, _lodash.default)(transformation.params).map(param => {
|
|
@@ -121,9 +133,11 @@ class JsonMap {
|
|
|
121
133
|
} = this.#resolvePath(param, results);
|
|
122
134
|
return paramObj ? paramPath ? (0, _lodash3.default)(paramObj, paramPath) : paramObj : paramPath;
|
|
123
135
|
});
|
|
136
|
+
console.debug('resolved transformation params:\n', params);
|
|
124
137
|
|
|
125
138
|
// Calls the specified method on the resolved object with the resolved parameters
|
|
126
139
|
const result = await (0, _lodash4.default)(methodObj, methodPath, ...params);
|
|
140
|
+
console.debug('transformation result:\n', result);
|
|
127
141
|
|
|
128
142
|
// Stores the result of the transformation
|
|
129
143
|
results.unshift(result);
|
|
@@ -131,6 +145,7 @@ class JsonMap {
|
|
|
131
145
|
|
|
132
146
|
// Sets the output at the specified path to the last result of the transformations & returns.
|
|
133
147
|
(0, _lodash13.default)(output, path, results[0]);
|
|
148
|
+
console.debug('updated output:\n', output);
|
|
134
149
|
return results[0];
|
|
135
150
|
}
|
|
136
151
|
|
|
@@ -139,8 +154,11 @@ class JsonMap {
|
|
|
139
154
|
// Creates an empty array or object based on whether the current node is an array or not
|
|
140
155
|
const transformedNode = (0, _lodash5.default)(node) ? [] : {};
|
|
141
156
|
|
|
142
|
-
// Iterates over each key-value pair in the current node
|
|
143
|
-
for (const [key, value] of Object.entries(node)
|
|
157
|
+
// Iterates over each key-value pair in the current node in ascending order by key
|
|
158
|
+
for (const [key, value] of (0, _lodash15.default)(Object.entries(node), _ref => {
|
|
159
|
+
let [key] = _ref;
|
|
160
|
+
return key;
|
|
161
|
+
})) {
|
|
144
162
|
// Constructs the current path by appending the current key to the previous path (if any)
|
|
145
163
|
const currentPath = path ? `${path}.${key}` : key;
|
|
146
164
|
|
|
@@ -184,20 +202,20 @@ class JsonMap {
|
|
|
184
202
|
|
|
185
203
|
// Defines special patterns and their corresponding resolution functions
|
|
186
204
|
const patterns = {
|
|
187
|
-
'^\\$\\.(?<obj>lib|input|output)\\.?(?<path>.*)':
|
|
205
|
+
'^\\$\\.(?<obj>lib|input|output)\\.?(?<path>.*)': _ref2 => {
|
|
188
206
|
let {
|
|
189
207
|
obj,
|
|
190
208
|
path
|
|
191
|
-
} =
|
|
209
|
+
} = _ref2;
|
|
192
210
|
return {
|
|
193
211
|
obj: this[obj],
|
|
194
212
|
path
|
|
195
213
|
};
|
|
196
214
|
},
|
|
197
|
-
'^\\$(?<path>\\[\\d+\\].*)':
|
|
215
|
+
'^\\$(?<path>\\[\\d+\\].*)': _ref3 => {
|
|
198
216
|
let {
|
|
199
217
|
path
|
|
200
|
-
} =
|
|
218
|
+
} = _ref3;
|
|
201
219
|
return {
|
|
202
220
|
obj: results,
|
|
203
221
|
path
|
package/lib/JsonMap/JsonMap.js
CHANGED
|
@@ -13,6 +13,7 @@ import mapValues from 'lodash.mapvalues';
|
|
|
13
13
|
import pickBy from 'lodash.pickby';
|
|
14
14
|
import set from 'lodash.set';
|
|
15
15
|
import size from 'lodash.size';
|
|
16
|
+
import sortBy from 'lodash.sortby';
|
|
16
17
|
import { nanoid } from 'nanoid';
|
|
17
18
|
|
|
18
19
|
const getJsonFns = () => {
|
|
@@ -100,21 +101,27 @@ class JsonMap {
|
|
|
100
101
|
* @private
|
|
101
102
|
*/
|
|
102
103
|
async #transform(node, input, output, path = '') {
|
|
104
|
+
console.debug('#transform params:\n', { node, input, output, path });
|
|
105
|
+
|
|
103
106
|
// Checks if the current node is an object and has a '$' key
|
|
104
107
|
if (isPlainObject(node) && size(node) === 1 && '$' in node) {
|
|
105
108
|
// Retrieves the transformations to be applied (can be an array or a single object)
|
|
106
109
|
const transformations = castArray(node['$']);
|
|
110
|
+
console.debug('transformations:\n', transformations);
|
|
107
111
|
|
|
108
112
|
// Array to store the results of the transformations
|
|
109
113
|
let results = [];
|
|
110
114
|
|
|
111
115
|
// Iterates over each transformation
|
|
112
116
|
for (const transformation of transformations) {
|
|
117
|
+
console.debug('processing transformation:\n', transformation);
|
|
118
|
+
|
|
113
119
|
// Resolves the object path for the transformation
|
|
114
120
|
const { obj: methodObj, path: methodPath } = this.#resolvePath(
|
|
115
121
|
transformation.method,
|
|
116
122
|
results
|
|
117
123
|
);
|
|
124
|
+
console.debug('methodObj:\n', methodObj, '\nmethodPath:\n', methodPath);
|
|
118
125
|
|
|
119
126
|
// Resolves the parameter paths for the transformation
|
|
120
127
|
const params = castArray(transformation.params).map((param) => {
|
|
@@ -128,9 +135,11 @@ class JsonMap {
|
|
|
128
135
|
: paramObj
|
|
129
136
|
: paramPath;
|
|
130
137
|
});
|
|
138
|
+
console.debug('resolved transformation params:\n', params);
|
|
131
139
|
|
|
132
140
|
// Calls the specified method on the resolved object with the resolved parameters
|
|
133
141
|
const result = await invoke(methodObj, methodPath, ...params);
|
|
142
|
+
console.debug('transformation result:\n', result);
|
|
134
143
|
|
|
135
144
|
// Stores the result of the transformation
|
|
136
145
|
results.unshift(result);
|
|
@@ -138,6 +147,8 @@ class JsonMap {
|
|
|
138
147
|
|
|
139
148
|
// Sets the output at the specified path to the last result of the transformations & returns.
|
|
140
149
|
set(output, path, results[0]);
|
|
150
|
+
console.debug('updated output:\n', output);
|
|
151
|
+
|
|
141
152
|
return results[0];
|
|
142
153
|
}
|
|
143
154
|
|
|
@@ -146,8 +157,8 @@ class JsonMap {
|
|
|
146
157
|
// Creates an empty array or object based on whether the current node is an array or not
|
|
147
158
|
const transformedNode = isArray(node) ? [] : {};
|
|
148
159
|
|
|
149
|
-
// Iterates over each key-value pair in the current node
|
|
150
|
-
for (const [key, value] of Object.entries(node)) {
|
|
160
|
+
// Iterates over each key-value pair in the current node in ascending order by key
|
|
161
|
+
for (const [key, value] of sortBy(Object.entries(node), ([key]) => key)) {
|
|
151
162
|
// Constructs the current path by appending the current key to the previous path (if any)
|
|
152
163
|
const currentPath = path ? `${path}.${key}` : key;
|
|
153
164
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@karmaniverous/jsonmap",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"lodash.pickby": "^4.6.0",
|
|
37
37
|
"lodash.set": "^4.3.2",
|
|
38
38
|
"lodash.size": "^4.2.0",
|
|
39
|
+
"lodash.sortby": "^4.7.0",
|
|
39
40
|
"nanoid": "^4.0.2"
|
|
40
41
|
},
|
|
41
42
|
"devDependencies": {
|