@karmaniverous/jsonmap 1.0.1 → 2.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.
@@ -1,247 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.JsonMap = void 0;
7
- var _lodash = _interopRequireDefault(require("lodash.castarray"));
8
- var _lodash2 = _interopRequireDefault(require("lodash.clonedeepwith"));
9
- var _lodash3 = _interopRequireDefault(require("lodash.get"));
10
- var _lodash4 = _interopRequireDefault(require("lodash.invoke"));
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"));
21
- var _nanoid = require("nanoid");
22
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
- // npm imports
24
-
25
- const getJsonFns = () => {
26
- const seen = new WeakSet();
27
- const undefinedToken = (0, _nanoid.nanoid)();
28
- const replacer = (key, value) => {
29
- if (typeof value === 'object' && value !== null) {
30
- if (seen.has(value)) {
31
- return 'CIRCULAR REFERENCE';
32
- }
33
- seen.add(value);
34
- if (!(0, _lodash5.default)(value)) return Object.getOwnPropertyNames(value).reduce((v, p) => ({
35
- ...v,
36
- [p]: value[p]
37
- }), {});
38
- }
39
- return (0, _lodash9.default)(value) ? undefinedToken : value;
40
- };
41
- const reviver = (key, value) => value === undefinedToken ? undefined : value;
42
- return {
43
- replacer,
44
- reviver
45
- };
46
- };
47
-
48
- /**
49
- * JsonMap class to apply transformations to a JSON object
50
- */
51
- class JsonMap {
52
- /**
53
- * Creates an instance of JsonMap.
54
- *
55
- * @param {object} [map] - The data mapping configuration.
56
- * @param {object} [lib] - A collection of function libraries.
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
60
- */
61
- constructor() {
62
- let map = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
63
- let lib = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
64
- let {
65
- ignore = '^\\$',
66
- logger = console
67
- } = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
68
- this.map = map;
69
- this.lib = lib;
70
- this.ignore = new RegExp(ignore);
71
- this.logger = logger;
72
- }
73
-
74
- /**
75
- * Transforms the input data according to the map configuration.
76
- *
77
- * @param {object} input - The input data to be transformed.
78
- * @return {object} - The transformed data.
79
- */
80
- async transform(input) {
81
- // Sets the input data and initializes an empty output object
82
- this.input = input;
83
- this.output = {};
84
-
85
- // Perform transformation & eliminate recursion from result.
86
- const {
87
- replacer,
88
- reviver
89
- } = getJsonFns();
90
- const result = JSON.parse(JSON.stringify(await this.#transform(this.map, this.input, this.output), replacer), reviver);
91
-
92
- // Recursively eliminate non-string keys & string keys starting with $ and not in ignoreExclusions.
93
- const deep = value => (0, _lodash7.default)(value) ? (0, _lodash10.default)((0, _lodash11.default)(value, (v, k) => !this.ignore.test(k)), value => (0, _lodash2.default)(value, deep)) : undefined;
94
- return (0, _lodash2.default)(result, deep);
95
- }
96
-
97
- /**
98
- * Recursive function to handle transformations.
99
- *
100
- * @param {object} node - The current map node.
101
- * @param {object} input - The current input node.
102
- * @param {object} output - The current output node.
103
- * @param {string} path - The path to the current node.
104
- * @return {object} - The transformed node.
105
- * @private
106
- */
107
- async #transform(node, input, output) {
108
- let path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
109
- this.logger.debug('#transform params:\n', {
110
- node,
111
- input,
112
- output,
113
- path
114
- });
115
-
116
- // Checks if the current node is an object and has a '$' key
117
- if ((0, _lodash7.default)(node) && (0, _lodash13.default)(node) === 1 && '$' in node) {
118
- // Retrieves the transformations to be applied (can be an array or a single object)
119
- const transformations = (0, _lodash.default)(node['$']);
120
- this.logger.debug('transformations:\n', transformations);
121
-
122
- // Array to store the results of the transformations
123
- let results = [];
124
-
125
- // Iterates over each transformation
126
- for (const transformation of transformations) {
127
- this.logger.debug('processing transformation:\n', transformation);
128
-
129
- // Resolves the object path for the transformation
130
- const {
131
- obj: methodObj,
132
- path: methodPath
133
- } = this.#resolvePath(transformation.method, results);
134
-
135
- // Resolves the parameter paths for the transformation
136
- const params = (0, _lodash.default)(transformation.params).map(param => {
137
- const {
138
- obj: paramObj,
139
- path: paramPath
140
- } = this.#resolvePath(param, results);
141
- return paramObj ? paramPath ? (0, _lodash3.default)(paramObj, paramPath) : paramObj : paramPath;
142
- });
143
- this.logger.debug('resolved transformation params:\n', params);
144
-
145
- // Calls the specified method on the resolved object with the resolved parameters
146
- const result = await (0, _lodash4.default)(methodObj, methodPath, ...params);
147
- this.logger.debug('transformation result:\n', result);
148
-
149
- // Stores the result of the transformation
150
- results.unshift(result);
151
- }
152
-
153
- // Sets the output at the specified path to the last result of the transformations & returns.
154
- (0, _lodash12.default)(output, path, results[0]);
155
- this.logger.debug('updated output:\n', output);
156
- return results[0];
157
- }
158
-
159
- // Checks if the current node is an object
160
- if ((0, _lodash6.default)(node)) {
161
- // Creates an empty array or object based on whether the current node is an array or not
162
- const transformedNode = Array.isArray(node) ? [] : {};
163
-
164
- // Iterates over each key-value pair in the current node in ascending order by key
165
- for (const [key, value] of (0, _lodash14.default)(Object.entries(node), _ref => {
166
- let [key] = _ref;
167
- return key;
168
- })) {
169
- // Constructs the current path by appending the current key to the previous path (if any)
170
- const currentPath = path ? `${path}.${key}` : key;
171
-
172
- // Recursively calls #transform with the current value, input, output, and path
173
- // Assigns the transformed value to the corresponding key in the transformedNode
174
- transformedNode[key] = await this.#transform(value, input, output, currentPath);
175
- }
176
-
177
- // Sets the output at the specified path to the transformedNode & returnsd.
178
- (0, _lodash12.default)(output, path, transformedNode);
179
- return transformedNode;
180
- }
181
-
182
- // Sets the output at the specified path to the current node & returns.
183
- (0, _lodash12.default)(output, path, node);
184
- return node;
185
- }
186
-
187
- /**
188
- * @typedef {object} PathResolution
189
- * @property {object} obj - The object to be used for the transformation.
190
- * @property {string} path - The path to the value to be used for the transformation.
191
- * @private
192
- */
193
-
194
- /**
195
- * Resolves the method/params path for a transformation
196
- *
197
- * @param {string} path - The path to be resolved.
198
- * @param {Array} results - The results from previous transformations.
199
- * @return {PathResolution} - The resolved path.
200
- * @private
201
- */
202
- #resolvePath(path, results) {
203
- // If the path is not a string, return it as is
204
- if (!(0, _lodash8.default)(path)) {
205
- return {
206
- path
207
- };
208
- }
209
-
210
- // Defines special patterns and their corresponding resolution functions
211
- const patterns = {
212
- '^\\$\\.(?<obj>lib|input|output)\\.?(?<path>.*)': _ref2 => {
213
- let {
214
- obj,
215
- path
216
- } = _ref2;
217
- return {
218
- obj: this[obj],
219
- path
220
- };
221
- },
222
- '^\\$(?<path>\\[\\d+\\].*)': _ref3 => {
223
- let {
224
- path
225
- } = _ref3;
226
- return {
227
- obj: results,
228
- path
229
- };
230
- }
231
- };
232
-
233
- // Iterates over the special patterns
234
- for (const [pattern, resolve] of Object.entries(patterns)) {
235
- const match = path.match(pattern);
236
- if (match) return resolve(match.groups);
237
- }
238
-
239
- // Returns the path as is if it does not match any special patterns
240
- return {
241
- path
242
- };
243
- }
244
- }
245
-
246
- // Exports the JsonMap class as the default export of this module
247
- exports.JsonMap = JsonMap;
@@ -1,12 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- Object.defineProperty(exports, "JsonMap", {
7
- enumerable: true,
8
- get: function () {
9
- return _JsonMap.JsonMap;
10
- }
11
- });
12
- var _JsonMap = require("./JsonMap/JsonMap.js");
package/dist/package.json DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "type": "commonjs"
3
- }
package/env/.env.dev DELETED
@@ -1,2 +0,0 @@
1
- # These are ENV-specific values. A version of this file should exist for
2
- # every ENV supported by the project, named .env.ENV
package/env/dynamic.js DELETED
@@ -1 +0,0 @@
1
- () => ({});
@@ -1,233 +0,0 @@
1
- // npm imports
2
- import castArray from 'lodash.castarray';
3
- import cloneDeepWith from 'lodash.clonedeepwith';
4
- import get from 'lodash.get';
5
- import invoke from 'lodash.invoke';
6
- import isArrayLikeObject from 'lodash.isarraylikeobject';
7
- import isObject from 'lodash.isobject';
8
- import isPlainObject from 'lodash.isplainobject';
9
- import isString from 'lodash.isstring';
10
- import isUndefined from 'lodash.isundefined';
11
- import mapValues from 'lodash.mapvalues';
12
- import pickBy from 'lodash.pickby';
13
- import set from 'lodash.set';
14
- import size from 'lodash.size';
15
- import sortBy from 'lodash.sortby';
16
- import { nanoid } from 'nanoid';
17
-
18
- const getJsonFns = () => {
19
- const seen = new WeakSet();
20
- const undefinedToken = nanoid();
21
-
22
- const replacer = (key, value) => {
23
- if (typeof value === 'object' && value !== null) {
24
- if (seen.has(value)) {
25
- return 'CIRCULAR REFERENCE';
26
- }
27
- seen.add(value);
28
-
29
- if (!isArrayLikeObject(value))
30
- return Object.getOwnPropertyNames(value).reduce(
31
- (v, p) => ({ ...v, [p]: value[p] }),
32
- {}
33
- );
34
- }
35
- return isUndefined(value) ? undefinedToken : value;
36
- };
37
-
38
- const reviver = (key, value) =>
39
- value === undefinedToken ? undefined : value;
40
-
41
- return { replacer, reviver };
42
- };
43
-
44
- /**
45
- * JsonMap class to apply transformations to a JSON object
46
- */
47
- class JsonMap {
48
- /**
49
- * Creates an instance of JsonMap.
50
- *
51
- * @param {object} [map] - The data mapping configuration.
52
- * @param {object} [lib] - A collection of function libraries.
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
56
- */
57
- constructor(map = {}, lib = {}, { ignore = '^\\$', logger = console } = {}) {
58
- this.map = map;
59
- this.lib = lib;
60
- this.ignore = new RegExp(ignore);
61
- this.logger = logger;
62
- }
63
-
64
- /**
65
- * Transforms the input data according to the map configuration.
66
- *
67
- * @param {object} input - The input data to be transformed.
68
- * @return {object} - The transformed data.
69
- */
70
- async transform(input) {
71
- // Sets the input data and initializes an empty output object
72
- this.input = input;
73
- this.output = {};
74
-
75
- // Perform transformation & eliminate recursion from result.
76
- const { replacer, reviver } = getJsonFns();
77
- const result = JSON.parse(
78
- JSON.stringify(
79
- await this.#transform(this.map, this.input, this.output),
80
- replacer
81
- ),
82
- reviver
83
- );
84
-
85
- // Recursively eliminate non-string keys & string keys starting with $ and not in ignoreExclusions.
86
- const deep = (value) =>
87
- isPlainObject(value)
88
- ? mapValues(
89
- pickBy(value, (v, k) => !this.ignore.test(k)),
90
- (value) => cloneDeepWith(value, deep)
91
- )
92
- : undefined;
93
-
94
- return cloneDeepWith(result, deep);
95
- }
96
-
97
- /**
98
- * Recursive function to handle transformations.
99
- *
100
- * @param {object} node - The current map node.
101
- * @param {object} input - The current input node.
102
- * @param {object} output - The current output node.
103
- * @param {string} path - The path to the current node.
104
- * @return {object} - The transformed node.
105
- * @private
106
- */
107
- async #transform(node, input, output, path = '') {
108
- this.logger.debug('#transform params:\n', { node, input, output, path });
109
-
110
- // Checks if the current node is an object and has a '$' key
111
- if (isPlainObject(node) && size(node) === 1 && '$' in node) {
112
- // Retrieves the transformations to be applied (can be an array or a single object)
113
- const transformations = castArray(node['$']);
114
- this.logger.debug('transformations:\n', transformations);
115
-
116
- // Array to store the results of the transformations
117
- let results = [];
118
-
119
- // Iterates over each transformation
120
- for (const transformation of transformations) {
121
- this.logger.debug('processing transformation:\n', transformation);
122
-
123
- // Resolves the object path for the transformation
124
- const { obj: methodObj, path: methodPath } = this.#resolvePath(
125
- transformation.method,
126
- results
127
- );
128
-
129
- // Resolves the parameter paths for the transformation
130
- const params = castArray(transformation.params).map((param) => {
131
- const { obj: paramObj, path: paramPath } = this.#resolvePath(
132
- param,
133
- results
134
- );
135
- return paramObj
136
- ? paramPath
137
- ? get(paramObj, paramPath)
138
- : paramObj
139
- : paramPath;
140
- });
141
- this.logger.debug('resolved transformation params:\n', params);
142
-
143
- // Calls the specified method on the resolved object with the resolved parameters
144
- const result = await invoke(methodObj, methodPath, ...params);
145
- this.logger.debug('transformation result:\n', result);
146
-
147
- // Stores the result of the transformation
148
- results.unshift(result);
149
- }
150
-
151
- // Sets the output at the specified path to the last result of the transformations & returns.
152
- set(output, path, results[0]);
153
- this.logger.debug('updated output:\n', output);
154
-
155
- return results[0];
156
- }
157
-
158
- // Checks if the current node is an object
159
- if (isObject(node)) {
160
- // Creates an empty array or object based on whether the current node is an array or not
161
- const transformedNode = Array.isArray(node) ? [] : {};
162
-
163
- // Iterates over each key-value pair in the current node in ascending order by key
164
- for (const [key, value] of sortBy(Object.entries(node), ([key]) => key)) {
165
- // Constructs the current path by appending the current key to the previous path (if any)
166
- const currentPath = path ? `${path}.${key}` : key;
167
-
168
- // Recursively calls #transform with the current value, input, output, and path
169
- // Assigns the transformed value to the corresponding key in the transformedNode
170
- transformedNode[key] = await this.#transform(
171
- value,
172
- input,
173
- output,
174
- currentPath
175
- );
176
- }
177
-
178
- // Sets the output at the specified path to the transformedNode & returnsd.
179
- set(output, path, transformedNode);
180
- return transformedNode;
181
- }
182
-
183
- // Sets the output at the specified path to the current node & returns.
184
- set(output, path, node);
185
- return node;
186
- }
187
-
188
- /**
189
- * @typedef {object} PathResolution
190
- * @property {object} obj - The object to be used for the transformation.
191
- * @property {string} path - The path to the value to be used for the transformation.
192
- * @private
193
- */
194
-
195
- /**
196
- * Resolves the method/params path for a transformation
197
- *
198
- * @param {string} path - The path to be resolved.
199
- * @param {Array} results - The results from previous transformations.
200
- * @return {PathResolution} - The resolved path.
201
- * @private
202
- */
203
- #resolvePath(path, results) {
204
- // If the path is not a string, return it as is
205
- if (!isString(path)) {
206
- return { path };
207
- }
208
-
209
- // Defines special patterns and their corresponding resolution functions
210
- const patterns = {
211
- '^\\$\\.(?<obj>lib|input|output)\\.?(?<path>.*)': ({ obj, path }) => ({
212
- obj: this[obj],
213
- path,
214
- }),
215
- '^\\$(?<path>\\[\\d+\\].*)': ({ path }) => ({
216
- obj: results,
217
- path,
218
- }),
219
- };
220
-
221
- // Iterates over the special patterns
222
- for (const [pattern, resolve] of Object.entries(patterns)) {
223
- const match = path.match(pattern);
224
- if (match) return resolve(match.groups);
225
- }
226
-
227
- // Returns the path as is if it does not match any special patterns
228
- return { path };
229
- }
230
- }
231
-
232
- // Exports the JsonMap class as the default export of this module
233
- export { JsonMap };
package/lib/index.js DELETED
@@ -1 +0,0 @@
1
- export { JsonMap } from './JsonMap/JsonMap.js';