@karmaniverous/jsonmap 0.3.2 → 1.0.1
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.
- package/README.md +9 -7
- package/dist/default/lib/JsonMap/JsonMap.js +19 -10
- package/lib/JsonMap/JsonMap.js +11 -9
- package/package.json +11 -11
package/README.md
CHANGED
|
@@ -122,20 +122,22 @@ JsonMap class to apply transformations to a JSON object
|
|
|
122
122
|
**Kind**: global class
|
|
123
123
|
|
|
124
124
|
* [JsonMap](#JsonMap)
|
|
125
|
-
* [new JsonMap([map], [lib], [
|
|
125
|
+
* [new JsonMap([map], [lib], [options])](#new_JsonMap_new)
|
|
126
126
|
* [.transform(input)](#JsonMap+transform) ⇒ <code>object</code>
|
|
127
127
|
|
|
128
128
|
<a name="new_JsonMap_new"></a>
|
|
129
129
|
|
|
130
|
-
### new JsonMap([map], [lib], [
|
|
130
|
+
### new JsonMap([map], [lib], [options])
|
|
131
131
|
Creates an instance of JsonMap.
|
|
132
132
|
|
|
133
133
|
|
|
134
|
-
| Param | Type |
|
|
135
|
-
| --- | --- | --- |
|
|
136
|
-
| [map] | <code>object</code> |
|
|
137
|
-
| [lib] | <code>object</code> |
|
|
138
|
-
| [
|
|
134
|
+
| Param | Type | Description |
|
|
135
|
+
| --- | --- | --- |
|
|
136
|
+
| [map] | <code>object</code> | The data mapping configuration. |
|
|
137
|
+
| [lib] | <code>object</code> | A collection of function libraries. |
|
|
138
|
+
| [options] | <code>object</code> | Options object |
|
|
139
|
+
| [options.ignore] | <code>string</code> | Regex pattern of keys to ignore. Defaults to '^\\$'. |
|
|
140
|
+
| [options.logger] | <code>string</code> | Logger object |
|
|
139
141
|
|
|
140
142
|
<a name="JsonMap+transform"></a>
|
|
141
143
|
|
|
@@ -54,15 +54,21 @@ class JsonMap {
|
|
|
54
54
|
*
|
|
55
55
|
* @param {object} [map] - The data mapping configuration.
|
|
56
56
|
* @param {object} [lib] - A collection of function libraries.
|
|
57
|
-
* @param {
|
|
57
|
+
* @param {object} [options] - Options object
|
|
58
|
+
* @param {string} [options.ignore] - Regex pattern of keys to ignore. Defaults to '^\\$'.
|
|
59
|
+
* @param {string} [options.logger] - Logger object
|
|
58
60
|
*/
|
|
59
61
|
constructor() {
|
|
60
62
|
let map = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
61
63
|
let lib = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
62
|
-
let
|
|
64
|
+
let {
|
|
65
|
+
ignore = '^\\$',
|
|
66
|
+
logger = console
|
|
67
|
+
} = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
63
68
|
this.map = map;
|
|
64
69
|
this.lib = lib;
|
|
65
70
|
this.ignore = new RegExp(ignore);
|
|
71
|
+
this.logger = logger;
|
|
66
72
|
}
|
|
67
73
|
|
|
68
74
|
/**
|
|
@@ -100,27 +106,31 @@ class JsonMap {
|
|
|
100
106
|
*/
|
|
101
107
|
async #transform(node, input, output) {
|
|
102
108
|
let path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
|
|
103
|
-
|
|
109
|
+
this.logger.debug('#transform params:\n', {
|
|
110
|
+
node,
|
|
111
|
+
input,
|
|
112
|
+
output,
|
|
113
|
+
path
|
|
114
|
+
});
|
|
104
115
|
|
|
105
116
|
// Checks if the current node is an object and has a '$' key
|
|
106
117
|
if ((0, _lodash7.default)(node) && (0, _lodash13.default)(node) === 1 && '$' in node) {
|
|
107
118
|
// Retrieves the transformations to be applied (can be an array or a single object)
|
|
108
119
|
const transformations = (0, _lodash.default)(node['$']);
|
|
109
|
-
|
|
120
|
+
this.logger.debug('transformations:\n', transformations);
|
|
110
121
|
|
|
111
122
|
// Array to store the results of the transformations
|
|
112
123
|
let results = [];
|
|
113
124
|
|
|
114
125
|
// Iterates over each transformation
|
|
115
126
|
for (const transformation of transformations) {
|
|
116
|
-
|
|
127
|
+
this.logger.debug('processing transformation:\n', transformation);
|
|
117
128
|
|
|
118
129
|
// Resolves the object path for the transformation
|
|
119
130
|
const {
|
|
120
131
|
obj: methodObj,
|
|
121
132
|
path: methodPath
|
|
122
133
|
} = this.#resolvePath(transformation.method, results);
|
|
123
|
-
// console.debug('methodObj:\n', methodObj, '\nmethodPath:\n', methodPath);
|
|
124
134
|
|
|
125
135
|
// Resolves the parameter paths for the transformation
|
|
126
136
|
const params = (0, _lodash.default)(transformation.params).map(param => {
|
|
@@ -130,11 +140,11 @@ class JsonMap {
|
|
|
130
140
|
} = this.#resolvePath(param, results);
|
|
131
141
|
return paramObj ? paramPath ? (0, _lodash3.default)(paramObj, paramPath) : paramObj : paramPath;
|
|
132
142
|
});
|
|
133
|
-
|
|
143
|
+
this.logger.debug('resolved transformation params:\n', params);
|
|
134
144
|
|
|
135
145
|
// Calls the specified method on the resolved object with the resolved parameters
|
|
136
146
|
const result = await (0, _lodash4.default)(methodObj, methodPath, ...params);
|
|
137
|
-
|
|
147
|
+
this.logger.debug('transformation result:\n', result);
|
|
138
148
|
|
|
139
149
|
// Stores the result of the transformation
|
|
140
150
|
results.unshift(result);
|
|
@@ -142,8 +152,7 @@ class JsonMap {
|
|
|
142
152
|
|
|
143
153
|
// Sets the output at the specified path to the last result of the transformations & returns.
|
|
144
154
|
(0, _lodash12.default)(output, path, results[0]);
|
|
145
|
-
|
|
146
|
-
|
|
155
|
+
this.logger.debug('updated output:\n', output);
|
|
147
156
|
return results[0];
|
|
148
157
|
}
|
|
149
158
|
|
package/lib/JsonMap/JsonMap.js
CHANGED
|
@@ -50,12 +50,15 @@ class JsonMap {
|
|
|
50
50
|
*
|
|
51
51
|
* @param {object} [map] - The data mapping configuration.
|
|
52
52
|
* @param {object} [lib] - A collection of function libraries.
|
|
53
|
-
* @param {
|
|
53
|
+
* @param {object} [options] - Options object
|
|
54
|
+
* @param {string} [options.ignore] - Regex pattern of keys to ignore. Defaults to '^\\$'.
|
|
55
|
+
* @param {string} [options.logger] - Logger object
|
|
54
56
|
*/
|
|
55
|
-
constructor(map = {}, lib = {}, ignore = '^\\$') {
|
|
57
|
+
constructor(map = {}, lib = {}, { ignore = '^\\$', logger = console } = {}) {
|
|
56
58
|
this.map = map;
|
|
57
59
|
this.lib = lib;
|
|
58
60
|
this.ignore = new RegExp(ignore);
|
|
61
|
+
this.logger = logger;
|
|
59
62
|
}
|
|
60
63
|
|
|
61
64
|
/**
|
|
@@ -102,27 +105,26 @@ class JsonMap {
|
|
|
102
105
|
* @private
|
|
103
106
|
*/
|
|
104
107
|
async #transform(node, input, output, path = '') {
|
|
105
|
-
|
|
108
|
+
this.logger.debug('#transform params:\n', { node, input, output, path });
|
|
106
109
|
|
|
107
110
|
// Checks if the current node is an object and has a '$' key
|
|
108
111
|
if (isPlainObject(node) && size(node) === 1 && '$' in node) {
|
|
109
112
|
// Retrieves the transformations to be applied (can be an array or a single object)
|
|
110
113
|
const transformations = castArray(node['$']);
|
|
111
|
-
|
|
114
|
+
this.logger.debug('transformations:\n', transformations);
|
|
112
115
|
|
|
113
116
|
// Array to store the results of the transformations
|
|
114
117
|
let results = [];
|
|
115
118
|
|
|
116
119
|
// Iterates over each transformation
|
|
117
120
|
for (const transformation of transformations) {
|
|
118
|
-
|
|
121
|
+
this.logger.debug('processing transformation:\n', transformation);
|
|
119
122
|
|
|
120
123
|
// Resolves the object path for the transformation
|
|
121
124
|
const { obj: methodObj, path: methodPath } = this.#resolvePath(
|
|
122
125
|
transformation.method,
|
|
123
126
|
results
|
|
124
127
|
);
|
|
125
|
-
// console.debug('methodObj:\n', methodObj, '\nmethodPath:\n', methodPath);
|
|
126
128
|
|
|
127
129
|
// Resolves the parameter paths for the transformation
|
|
128
130
|
const params = castArray(transformation.params).map((param) => {
|
|
@@ -136,11 +138,11 @@ class JsonMap {
|
|
|
136
138
|
: paramObj
|
|
137
139
|
: paramPath;
|
|
138
140
|
});
|
|
139
|
-
|
|
141
|
+
this.logger.debug('resolved transformation params:\n', params);
|
|
140
142
|
|
|
141
143
|
// Calls the specified method on the resolved object with the resolved parameters
|
|
142
144
|
const result = await invoke(methodObj, methodPath, ...params);
|
|
143
|
-
|
|
145
|
+
this.logger.debug('transformation result:\n', result);
|
|
144
146
|
|
|
145
147
|
// Stores the result of the transformation
|
|
146
148
|
results.unshift(result);
|
|
@@ -148,7 +150,7 @@ class JsonMap {
|
|
|
148
150
|
|
|
149
151
|
// Sets the output at the specified path to the last result of the transformations & returns.
|
|
150
152
|
set(output, path, results[0]);
|
|
151
|
-
|
|
153
|
+
this.logger.debug('updated output:\n', output);
|
|
152
154
|
|
|
153
155
|
return results[0];
|
|
154
156
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@karmaniverous/jsonmap",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -40,24 +40,24 @@
|
|
|
40
40
|
"nanoid": "^5.0.6"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@babel/cli": "^7.
|
|
44
|
-
"@babel/core": "^7.
|
|
45
|
-
"@babel/eslint-parser": "^7.
|
|
46
|
-
"@babel/plugin-syntax-import-assertions": "^7.
|
|
47
|
-
"@babel/preset-env": "^7.
|
|
43
|
+
"@babel/cli": "^7.24.1",
|
|
44
|
+
"@babel/core": "^7.24.3",
|
|
45
|
+
"@babel/eslint-parser": "^7.24.1",
|
|
46
|
+
"@babel/plugin-syntax-import-assertions": "^7.24.1",
|
|
47
|
+
"@babel/preset-env": "^7.24.3",
|
|
48
48
|
"@babel/register": "^7.23.7",
|
|
49
|
-
"@karmaniverous/get-dotenv": "^3.1.
|
|
50
|
-
"@types/node": "^20.11.
|
|
49
|
+
"@karmaniverous/get-dotenv": "^3.1.19",
|
|
50
|
+
"@types/node": "^20.11.30",
|
|
51
51
|
"auto-changelog": "^2.4.0",
|
|
52
52
|
"chai": "^5.1.0",
|
|
53
53
|
"concat-md": "^0.5.1",
|
|
54
54
|
"eslint": "^8.57.0",
|
|
55
55
|
"eslint-config-standard": "^17.1.0",
|
|
56
|
-
"eslint-plugin-jsdoc": "^48.2.
|
|
57
|
-
"eslint-plugin-mocha": "^10.
|
|
56
|
+
"eslint-plugin-jsdoc": "^48.2.1",
|
|
57
|
+
"eslint-plugin-mocha": "^10.4.1",
|
|
58
58
|
"jsdoc-to-markdown": "^8.0.1",
|
|
59
59
|
"lodash": "^4.17.21",
|
|
60
|
-
"mocha": "^10.
|
|
60
|
+
"mocha": "^10.4.0",
|
|
61
61
|
"numeral": "^2.0.6",
|
|
62
62
|
"prettier": "^3.2.5",
|
|
63
63
|
"release-it": "^17.1.1"
|