@karmaniverous/jsonmap 0.2.5 → 0.2.7
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,32 +98,27 @@ class JsonMap {
|
|
|
97
98
|
*/
|
|
98
99
|
async #transform(node, input, output) {
|
|
99
100
|
let path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
|
|
100
|
-
console.debug('#transform params:\n', {
|
|
101
|
-
node,
|
|
102
|
-
input,
|
|
103
|
-
output,
|
|
104
|
-
path
|
|
105
|
-
});
|
|
101
|
+
// console.debug('#transform params:\n', { node, input, output, path });
|
|
106
102
|
|
|
107
103
|
// Checks if the current node is an object and has a '$' key
|
|
108
104
|
if ((0, _lodash8.default)(node) && (0, _lodash14.default)(node) === 1 && '$' in node) {
|
|
109
105
|
// Retrieves the transformations to be applied (can be an array or a single object)
|
|
110
106
|
const transformations = (0, _lodash.default)(node['$']);
|
|
111
|
-
console.debug('transformations:\n', transformations);
|
|
107
|
+
// console.debug('transformations:\n', transformations);
|
|
112
108
|
|
|
113
109
|
// Array to store the results of the transformations
|
|
114
110
|
let results = [];
|
|
115
111
|
|
|
116
112
|
// Iterates over each transformation
|
|
117
113
|
for (const transformation of transformations) {
|
|
118
|
-
console.debug('processing transformation:\n', transformation);
|
|
114
|
+
// console.debug('processing transformation:\n', transformation);
|
|
119
115
|
|
|
120
116
|
// Resolves the object path for the transformation
|
|
121
117
|
const {
|
|
122
118
|
obj: methodObj,
|
|
123
119
|
path: methodPath
|
|
124
120
|
} = this.#resolvePath(transformation.method, results);
|
|
125
|
-
console.debug('methodObj:\n', methodObj, '\nmethodPath:\n', methodPath);
|
|
121
|
+
// console.debug('methodObj:\n', methodObj, '\nmethodPath:\n', methodPath);
|
|
126
122
|
|
|
127
123
|
// Resolves the parameter paths for the transformation
|
|
128
124
|
const params = (0, _lodash.default)(transformation.params).map(param => {
|
|
@@ -132,11 +128,11 @@ class JsonMap {
|
|
|
132
128
|
} = this.#resolvePath(param, results);
|
|
133
129
|
return paramObj ? paramPath ? (0, _lodash3.default)(paramObj, paramPath) : paramObj : paramPath;
|
|
134
130
|
});
|
|
135
|
-
console.debug('resolved transformation params:\n', params);
|
|
131
|
+
// console.debug('resolved transformation params:\n', params);
|
|
136
132
|
|
|
137
133
|
// Calls the specified method on the resolved object with the resolved parameters
|
|
138
134
|
const result = await (0, _lodash4.default)(methodObj, methodPath, ...params);
|
|
139
|
-
console.debug('transformation result:\n', result);
|
|
135
|
+
// console.debug('transformation result:\n', result);
|
|
140
136
|
|
|
141
137
|
// Stores the result of the transformation
|
|
142
138
|
results.unshift(result);
|
|
@@ -144,7 +140,8 @@ class JsonMap {
|
|
|
144
140
|
|
|
145
141
|
// Sets the output at the specified path to the last result of the transformations & returns.
|
|
146
142
|
(0, _lodash13.default)(output, path, results[0]);
|
|
147
|
-
console.debug('updated output:\n', output);
|
|
143
|
+
// console.debug('updated output:\n', output);
|
|
144
|
+
|
|
148
145
|
return results[0];
|
|
149
146
|
}
|
|
150
147
|
|
|
@@ -153,8 +150,11 @@ class JsonMap {
|
|
|
153
150
|
// Creates an empty array or object based on whether the current node is an array or not
|
|
154
151
|
const transformedNode = (0, _lodash5.default)(node) ? [] : {};
|
|
155
152
|
|
|
156
|
-
// Iterates over each key-value pair in the current node
|
|
157
|
-
for (const [key, value] of Object.entries(node)
|
|
153
|
+
// Iterates over each key-value pair in the current node in ascending order by key
|
|
154
|
+
for (const [key, value] of (0, _lodash15.default)(Object.entries(node), _ref => {
|
|
155
|
+
let [key] = _ref;
|
|
156
|
+
return key;
|
|
157
|
+
})) {
|
|
158
158
|
// Constructs the current path by appending the current key to the previous path (if any)
|
|
159
159
|
const currentPath = path ? `${path}.${key}` : key;
|
|
160
160
|
|
|
@@ -198,20 +198,20 @@ class JsonMap {
|
|
|
198
198
|
|
|
199
199
|
// Defines special patterns and their corresponding resolution functions
|
|
200
200
|
const patterns = {
|
|
201
|
-
'^\\$\\.(?<obj>lib|input|output)\\.?(?<path>.*)':
|
|
201
|
+
'^\\$\\.(?<obj>lib|input|output)\\.?(?<path>.*)': _ref2 => {
|
|
202
202
|
let {
|
|
203
203
|
obj,
|
|
204
204
|
path
|
|
205
|
-
} =
|
|
205
|
+
} = _ref2;
|
|
206
206
|
return {
|
|
207
207
|
obj: this[obj],
|
|
208
208
|
path
|
|
209
209
|
};
|
|
210
210
|
},
|
|
211
|
-
'^\\$(?<path>\\[\\d+\\].*)':
|
|
211
|
+
'^\\$(?<path>\\[\\d+\\].*)': _ref3 => {
|
|
212
212
|
let {
|
|
213
213
|
path
|
|
214
|
-
} =
|
|
214
|
+
} = _ref3;
|
|
215
215
|
return {
|
|
216
216
|
obj: results,
|
|
217
217
|
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,27 +101,27 @@ class JsonMap {
|
|
|
100
101
|
* @private
|
|
101
102
|
*/
|
|
102
103
|
async #transform(node, input, output, path = '') {
|
|
103
|
-
console.debug('#transform params:\n', { node, input, output, path });
|
|
104
|
+
// console.debug('#transform params:\n', { node, input, output, path });
|
|
104
105
|
|
|
105
106
|
// Checks if the current node is an object and has a '$' key
|
|
106
107
|
if (isPlainObject(node) && size(node) === 1 && '$' in node) {
|
|
107
108
|
// Retrieves the transformations to be applied (can be an array or a single object)
|
|
108
109
|
const transformations = castArray(node['$']);
|
|
109
|
-
console.debug('transformations:\n', transformations);
|
|
110
|
+
// console.debug('transformations:\n', transformations);
|
|
110
111
|
|
|
111
112
|
// Array to store the results of the transformations
|
|
112
113
|
let results = [];
|
|
113
114
|
|
|
114
115
|
// Iterates over each transformation
|
|
115
116
|
for (const transformation of transformations) {
|
|
116
|
-
console.debug('processing transformation:\n', transformation);
|
|
117
|
+
// console.debug('processing transformation:\n', transformation);
|
|
117
118
|
|
|
118
119
|
// Resolves the object path for the transformation
|
|
119
120
|
const { obj: methodObj, path: methodPath } = this.#resolvePath(
|
|
120
121
|
transformation.method,
|
|
121
122
|
results
|
|
122
123
|
);
|
|
123
|
-
console.debug('methodObj:\n', methodObj, '\nmethodPath:\n', methodPath);
|
|
124
|
+
// console.debug('methodObj:\n', methodObj, '\nmethodPath:\n', methodPath);
|
|
124
125
|
|
|
125
126
|
// Resolves the parameter paths for the transformation
|
|
126
127
|
const params = castArray(transformation.params).map((param) => {
|
|
@@ -134,11 +135,11 @@ class JsonMap {
|
|
|
134
135
|
: paramObj
|
|
135
136
|
: paramPath;
|
|
136
137
|
});
|
|
137
|
-
console.debug('resolved transformation params:\n', params);
|
|
138
|
+
// console.debug('resolved transformation params:\n', params);
|
|
138
139
|
|
|
139
140
|
// Calls the specified method on the resolved object with the resolved parameters
|
|
140
141
|
const result = await invoke(methodObj, methodPath, ...params);
|
|
141
|
-
console.debug('transformation result:\n', result);
|
|
142
|
+
// console.debug('transformation result:\n', result);
|
|
142
143
|
|
|
143
144
|
// Stores the result of the transformation
|
|
144
145
|
results.unshift(result);
|
|
@@ -146,7 +147,7 @@ class JsonMap {
|
|
|
146
147
|
|
|
147
148
|
// Sets the output at the specified path to the last result of the transformations & returns.
|
|
148
149
|
set(output, path, results[0]);
|
|
149
|
-
console.debug('updated output:\n', output);
|
|
150
|
+
// console.debug('updated output:\n', output);
|
|
150
151
|
|
|
151
152
|
return results[0];
|
|
152
153
|
}
|
|
@@ -156,8 +157,8 @@ class JsonMap {
|
|
|
156
157
|
// Creates an empty array or object based on whether the current node is an array or not
|
|
157
158
|
const transformedNode = isArray(node) ? [] : {};
|
|
158
159
|
|
|
159
|
-
// Iterates over each key-value pair in the current node
|
|
160
|
-
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)) {
|
|
161
162
|
// Constructs the current path by appending the current key to the previous path (if any)
|
|
162
163
|
const currentPath = path ? `${path}.${key}` : key;
|
|
163
164
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@karmaniverous/jsonmap",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7",
|
|
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": {
|