@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.
@@ -0,0 +1,204 @@
1
+ (function (exports, _, nanoid, tslog) {
2
+ 'use strict';
3
+
4
+ /******************************************************************************
5
+ Copyright (c) Microsoft Corporation.
6
+
7
+ Permission to use, copy, modify, and/or distribute this software for any
8
+ purpose with or without fee is hereby granted.
9
+
10
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
11
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
15
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16
+ PERFORMANCE OF THIS SOFTWARE.
17
+ ***************************************************************************** */
18
+ /* global Reflect, Promise, SuppressedError, Symbol */
19
+
20
+
21
+ function __awaiter(thisArg, _arguments, P, generator) {
22
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
+ return new (P || (P = Promise))(function (resolve, reject) {
24
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
28
+ });
29
+ }
30
+
31
+ function __classPrivateFieldGet(receiver, state, kind, f) {
32
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
33
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
34
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
35
+ }
36
+
37
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
38
+ var e = new Error(message);
39
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
40
+ };
41
+
42
+ // Get package scope & name from package.json.
43
+ var _a$1, _b$1, _c;
44
+ const npmPackageRegex = /^(?:(?<packageScope>@[a-z0-9-~][a-z0-9-._~]*)\/)?(?<packageName>[a-z0-9-~][a-z0-9-._~]*)$/;
45
+ const { packageScope, packageName } = ((_c = (_b$1 = (_a$1 = process.env.npm_package_name) === null || _a$1 === void 0 ? void 0 : _a$1.match(npmPackageRegex)) === null || _b$1 === void 0 ? void 0 : _b$1.groups) !== null && _c !== void 0 ? _c : {});
46
+
47
+ var _a, _b;
48
+ // These are the presefined tslog log levels. You can specify
49
+ // LOG_LEVEL by any of the keys or values defined below.
50
+ const logLevels = {
51
+ silly: 0,
52
+ trace: 1,
53
+ debug: 2,
54
+ info: 3,
55
+ warn: 4,
56
+ error: 5,
57
+ fatal: 6,
58
+ };
59
+ const integerLogLevel = parseInt((_a = process.env.LOG_LEVEL) !== null && _a !== void 0 ? _a : '');
60
+ const resolvedLogLevel = Number.isNaN(integerLogLevel)
61
+ ? logLevels[((_b = process.env.LOG_LEVEL) !== null && _b !== void 0 ? _b : '').toLowerCase()]
62
+ : integerLogLevel;
63
+ // Set your tslog instance options here! By default, logs are suppressed if LOG_LEVEL is invalid or undefined.
64
+ // See https://tslog.js.org for more info.
65
+ const logger = new tslog.Logger({
66
+ hideLogPositionForProduction: true,
67
+ minLevel: resolvedLogLevel,
68
+ name: packageName,
69
+ type: resolvedLogLevel === undefined ? 'hidden' : 'pretty',
70
+ });
71
+
72
+ var _JsonMap_instances, _JsonMap_transform, _JsonMap_resolvePath;
73
+ // The transformation process leverages JSON.stringify and JSON.parse to eliminate circular references.
74
+ const getJsonFns = () => {
75
+ const seen = new WeakSet();
76
+ const undefinedToken = nanoid.nanoid();
77
+ const replacer = (key, value) => {
78
+ if (typeof value === 'object' && value !== null) {
79
+ if (seen.has(value)) {
80
+ return 'CIRCULAR REFERENCE';
81
+ }
82
+ seen.add(value);
83
+ if (!_.isArrayLikeObject(value))
84
+ return Object.getOwnPropertyNames(value).reduce((v, p) => (Object.assign(Object.assign({}, v), { [p]: value[p] })), {});
85
+ }
86
+ return _.isUndefined(value) ? undefinedToken : value;
87
+ };
88
+ const reviver = (key, value) => value === undefinedToken ? undefined : value;
89
+ return { replacer, reviver };
90
+ };
91
+ /**
92
+ * JsonMap class to apply transformations to a JSON object
93
+ */
94
+ class JsonMap {
95
+ constructor(map = {}, lib = {}, { ignore = /^\$/ } = {}) {
96
+ _JsonMap_instances.add(this);
97
+ this.map = map;
98
+ this.lib = lib;
99
+ this.ignore = _.isString(ignore) ? new RegExp(ignore) : ignore;
100
+ }
101
+ /**
102
+ * Transforms the input data according to the map configuration.
103
+ */
104
+ transform(input) {
105
+ return __awaiter(this, void 0, void 0, function* () {
106
+ // Sets the input data and initializes an empty output object
107
+ this.input = input;
108
+ this.output = {};
109
+ // Perform transformation & eliminate recursion from result.
110
+ const { replacer, reviver } = getJsonFns();
111
+ const result = JSON.parse(JSON.stringify(yield __classPrivateFieldGet(this, _JsonMap_instances, "m", _JsonMap_transform).call(this, this.map, this.input, this.output), replacer), reviver);
112
+ // Recursively eliminate non-string keys & string keys starting with $ and not in ignoreExclusions.
113
+ const deep = (value) => value instanceof Object && !_.isArray(value)
114
+ ? _.mapValues(_.pickBy(value, (v, k) => !this.ignore.test(k)), (value) => _.cloneDeepWith(value, deep))
115
+ : undefined;
116
+ return _.cloneDeepWith(result, deep);
117
+ });
118
+ }
119
+ }
120
+ _JsonMap_instances = new WeakSet(), _JsonMap_transform = function _JsonMap_transform(node_1, input_1, output_1) {
121
+ return __awaiter(this, arguments, void 0, function* (node, input, output, path = '') {
122
+ logger.debug('#transform params:\n', { node, input, output, path });
123
+ // Checks if the current node is an object and has only a '$' key
124
+ if (node instanceof Object && _.size(node) === 1 && '$' in node) {
125
+ // Retrieves the transformations to be applied (can be an array or a single object)
126
+ const transformations = _.castArray(node.$);
127
+ logger.debug('transformations:\n', transformations);
128
+ // Array to store the results of the transformations
129
+ const results = [];
130
+ // Iterates over each transformation
131
+ for (const transformation of transformations) {
132
+ logger.debug('processing transformation:\n', transformation);
133
+ // Resolves the object path for the transformation
134
+ const { obj: methodObj, path: methodPath } = __classPrivateFieldGet(this, _JsonMap_instances, "m", _JsonMap_resolvePath).call(this, transformation.method, results);
135
+ // Resolves the parameter paths for the transformation
136
+ const params = _.castArray(transformation.params).map((param) => {
137
+ const { obj: paramObj, path: paramPath } = __classPrivateFieldGet(this, _JsonMap_instances, "m", _JsonMap_resolvePath).call(this, param, results);
138
+ return paramObj
139
+ ? paramPath
140
+ ? _.get(paramObj, paramPath)
141
+ : paramObj
142
+ : paramPath;
143
+ });
144
+ logger.debug('resolved transformation params:\n', params);
145
+ // Calls the specified method on the resolved object with the resolved parameters
146
+ const result = (yield _.invoke(methodObj, methodPath, ...params));
147
+ logger.debug('transformation result:\n', result);
148
+ // Stores the result of the transformation
149
+ results.unshift(result);
150
+ }
151
+ // Sets the output at the specified path to the last result of the transformations & returns.
152
+ _.set(output, path, results[0]);
153
+ logger.debug('updated output:\n', output);
154
+ return results[0];
155
+ }
156
+ // Checks if the current node is an object
157
+ if (_.isObject(node)) {
158
+ // Creates an empty array or object based on whether the current node is an array or not
159
+ const transformedNode = (Array.isArray(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)) {
162
+ // Constructs the current path by appending the current key to the previous path (if any)
163
+ const currentPath = path ? `${path}.${key}` : key;
164
+ // Recursively calls #transform with the current value, input, output, and path
165
+ // Assigns the transformed value to the corresponding key in the transformedNode
166
+ transformedNode[key] = yield __classPrivateFieldGet(this, _JsonMap_instances, "m", _JsonMap_transform).call(this, value, input, output, currentPath);
167
+ }
168
+ // Sets the output at the specified path to the transformedNode & returnsd.
169
+ _.set(output, path, transformedNode);
170
+ return transformedNode;
171
+ }
172
+ // Sets the output at the specified path to the current node & returns.
173
+ _.set(output, path, node);
174
+ return node;
175
+ });
176
+ }, _JsonMap_resolvePath = function _JsonMap_resolvePath(path, results) {
177
+ // If the path is not a string, return it as is
178
+ if (!_.isString(path)) {
179
+ return { path };
180
+ }
181
+ // Defines special patterns and their corresponding resolution functions
182
+ const patterns = {
183
+ '^\\$\\.(?<obj>lib|input|output)\\.?(?<path>.*)': ({ obj, path }) => ({
184
+ obj: this[obj],
185
+ path,
186
+ }),
187
+ '^\\$(?<path>\\[\\d+\\].*)': ({ path }) => ({
188
+ obj: results,
189
+ path,
190
+ }),
191
+ };
192
+ // Iterates over the special patterns
193
+ for (const [pattern, resolve] of Object.entries(patterns)) {
194
+ const match = path.match(pattern);
195
+ if (match === null || match === void 0 ? void 0 : match.groups)
196
+ return resolve(match.groups);
197
+ }
198
+ // Returns the path as is if it does not match any special patterns
199
+ return { path };
200
+ };
201
+
202
+ exports.JsonMap = JsonMap;
203
+
204
+ })(this.jsonmap = this.jsonmap || {}, _, nanoid, tslog);
@@ -0,0 +1 @@
1
+ !function(t,e,n,r){"use strict";function o(t,e,n,r){return new(n||(n=Promise))((function(o,i){function s(t){try{c(r.next(t))}catch(t){i(t)}}function a(t){try{c(r.throw(t))}catch(t){i(t)}}function c(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(s,a)}c((r=r.apply(t,e||[])).next())}))}function i(t,e,n,r){if("a"===n&&!r)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!r:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===n?r:"a"===n?r.call(t):r?r.value:e.get(t)}var s,a,c;"function"==typeof SuppressedError&&SuppressedError;const{packageScope:u,packageName:p}=null!==(c=null===(a=null===(s=process.env.npm_package_name)||void 0===s?void 0:s.match(/^(?:(?<packageScope>@[a-z0-9-~][a-z0-9-._~]*)\/)?(?<packageName>[a-z0-9-~][a-z0-9-._~]*)$/))||void 0===a?void 0:a.groups)&&void 0!==c?c:{};var d,h;const f=parseInt(null!==(d=process.env.LOG_LEVEL)&&void 0!==d?d:""),l=Number.isNaN(f)?{silly:0,trace:1,debug:2,info:3,warn:4,error:5,fatal:6}[(null!==(h=process.env.LOG_LEVEL)&&void 0!==h?h:"").toLowerCase()]:f,m=new r.Logger({hideLogPositionForProduction:!0,minLevel:l,name:p,type:void 0===l?"hidden":"pretty"});var g,v,b;g=new WeakSet,v=function t(n,r,s){return o(this,arguments,void 0,(function*(n,r,o,s=""){if(m.debug("#transform params:\n",{node:n,input:r,output:o,path:s}),n instanceof Object&&1===e.size(n)&&"$"in n){const t=e.castArray(n.$);m.debug("transformations:\n",t);const r=[];for(const n of t){m.debug("processing transformation:\n",n);const{obj:t,path:o}=i(this,g,"m",b).call(this,n.method,r),s=e.castArray(n.params).map((t=>{const{obj:n,path:o}=i(this,g,"m",b).call(this,t,r);return n?o?e.get(n,o):n:o}));m.debug("resolved transformation params:\n",s);const a=yield e.invoke(t,o,...s);m.debug("transformation result:\n",a),r.unshift(a)}return e.set(o,s,r[0]),m.debug("updated output:\n",o),r[0]}if(e.isObject(n)){const a=Array.isArray(n)?[]:{};for(const[c,u]of e.sortBy(Object.entries(n),(([t])=>t))){const e=s?`${s}.${c}`:c;a[c]=yield i(this,g,"m",t).call(this,u,r,o,e)}return e.set(o,s,a),a}return e.set(o,s,n),n}))},b=function(t,n){if(!e.isString(t))return{path:t};const r={"^\\$\\.(?<obj>lib|input|output)\\.?(?<path>.*)":({obj:t,path:e})=>({obj:this[t],path:e}),"^\\$(?<path>\\[\\d+\\].*)":({path:t})=>({obj:n,path:t})};for(const[e,n]of Object.entries(r)){const r=t.match(e);if(null==r?void 0:r.groups)return n(r.groups)}return{path:t}},t.JsonMap=class{constructor(t={},n={},{ignore:r=/^\$/}={}){g.add(this),this.map=t,this.lib=n,this.ignore=e.isString(r)?new RegExp(r):r}transform(t){return o(this,void 0,void 0,(function*(){this.input=t,this.output={};const{replacer:r,reviver:o}=(()=>{const t=new WeakSet,r=n.nanoid();return{replacer:(n,o)=>{if("object"==typeof o&&null!==o){if(t.has(o))return"CIRCULAR REFERENCE";if(t.add(o),!e.isArrayLikeObject(o))return Object.getOwnPropertyNames(o).reduce(((t,e)=>Object.assign(Object.assign({},t),{[e]:o[e]})),{})}return e.isUndefined(o)?r:o},reviver:(t,e)=>e===r?void 0:e}})(),s=JSON.parse(JSON.stringify(yield i(this,g,"m",v).call(this,this.map,this.input,this.output),r),o),a=t=>t instanceof Object&&!e.isArray(t)?e.mapValues(e.pickBy(t,((t,e)=>!this.ignore.test(e))),(t=>e.cloneDeepWith(t,a))):void 0;return e.cloneDeepWith(s,a)}))}}}(this.jsonmap=this.jsonmap||{},_,nanoid,tslog);
package/dist/index.mjs ADDED
@@ -0,0 +1,203 @@
1
+ import _ from 'lodash';
2
+ import { nanoid } from 'nanoid';
3
+ import { Logger } from 'tslog';
4
+
5
+ /******************************************************************************
6
+ Copyright (c) Microsoft Corporation.
7
+
8
+ Permission to use, copy, modify, and/or distribute this software for any
9
+ purpose with or without fee is hereby granted.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ ***************************************************************************** */
19
+ /* global Reflect, Promise, SuppressedError, Symbol */
20
+
21
+
22
+ function __awaiter(thisArg, _arguments, P, generator) {
23
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
24
+ return new (P || (P = Promise))(function (resolve, reject) {
25
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
26
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
27
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
28
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
29
+ });
30
+ }
31
+
32
+ function __classPrivateFieldGet(receiver, state, kind, f) {
33
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
34
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
35
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
36
+ }
37
+
38
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
39
+ var e = new Error(message);
40
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
41
+ };
42
+
43
+ // Get package scope & name from package.json.
44
+ var _a$1, _b$1, _c;
45
+ const npmPackageRegex = /^(?:(?<packageScope>@[a-z0-9-~][a-z0-9-._~]*)\/)?(?<packageName>[a-z0-9-~][a-z0-9-._~]*)$/;
46
+ const { packageScope, packageName } = ((_c = (_b$1 = (_a$1 = process.env.npm_package_name) === null || _a$1 === void 0 ? void 0 : _a$1.match(npmPackageRegex)) === null || _b$1 === void 0 ? void 0 : _b$1.groups) !== null && _c !== void 0 ? _c : {});
47
+
48
+ var _a, _b;
49
+ // These are the presefined tslog log levels. You can specify
50
+ // LOG_LEVEL by any of the keys or values defined below.
51
+ const logLevels = {
52
+ silly: 0,
53
+ trace: 1,
54
+ debug: 2,
55
+ info: 3,
56
+ warn: 4,
57
+ error: 5,
58
+ fatal: 6,
59
+ };
60
+ const integerLogLevel = parseInt((_a = process.env.LOG_LEVEL) !== null && _a !== void 0 ? _a : '');
61
+ const resolvedLogLevel = Number.isNaN(integerLogLevel)
62
+ ? logLevels[((_b = process.env.LOG_LEVEL) !== null && _b !== void 0 ? _b : '').toLowerCase()]
63
+ : integerLogLevel;
64
+ // Set your tslog instance options here! By default, logs are suppressed if LOG_LEVEL is invalid or undefined.
65
+ // See https://tslog.js.org for more info.
66
+ const logger = new Logger({
67
+ hideLogPositionForProduction: true,
68
+ minLevel: resolvedLogLevel,
69
+ name: packageName,
70
+ type: resolvedLogLevel === undefined ? 'hidden' : 'pretty',
71
+ });
72
+
73
+ var _JsonMap_instances, _JsonMap_transform, _JsonMap_resolvePath;
74
+ // The transformation process leverages JSON.stringify and JSON.parse to eliminate circular references.
75
+ const getJsonFns = () => {
76
+ const seen = new WeakSet();
77
+ const undefinedToken = nanoid();
78
+ const replacer = (key, value) => {
79
+ if (typeof value === 'object' && value !== null) {
80
+ if (seen.has(value)) {
81
+ return 'CIRCULAR REFERENCE';
82
+ }
83
+ seen.add(value);
84
+ if (!_.isArrayLikeObject(value))
85
+ return Object.getOwnPropertyNames(value).reduce((v, p) => (Object.assign(Object.assign({}, v), { [p]: value[p] })), {});
86
+ }
87
+ return _.isUndefined(value) ? undefinedToken : value;
88
+ };
89
+ const reviver = (key, value) => value === undefinedToken ? undefined : value;
90
+ return { replacer, reviver };
91
+ };
92
+ /**
93
+ * JsonMap class to apply transformations to a JSON object
94
+ */
95
+ class JsonMap {
96
+ constructor(map = {}, lib = {}, { ignore = /^\$/ } = {}) {
97
+ _JsonMap_instances.add(this);
98
+ this.map = map;
99
+ this.lib = lib;
100
+ this.ignore = _.isString(ignore) ? new RegExp(ignore) : ignore;
101
+ }
102
+ /**
103
+ * Transforms the input data according to the map configuration.
104
+ */
105
+ transform(input) {
106
+ return __awaiter(this, void 0, void 0, function* () {
107
+ // Sets the input data and initializes an empty output object
108
+ this.input = input;
109
+ this.output = {};
110
+ // Perform transformation & eliminate recursion from result.
111
+ const { replacer, reviver } = getJsonFns();
112
+ const result = JSON.parse(JSON.stringify(yield __classPrivateFieldGet(this, _JsonMap_instances, "m", _JsonMap_transform).call(this, this.map, this.input, this.output), replacer), reviver);
113
+ // Recursively eliminate non-string keys & string keys starting with $ and not in ignoreExclusions.
114
+ const deep = (value) => value instanceof Object && !_.isArray(value)
115
+ ? _.mapValues(_.pickBy(value, (v, k) => !this.ignore.test(k)), (value) => _.cloneDeepWith(value, deep))
116
+ : undefined;
117
+ return _.cloneDeepWith(result, deep);
118
+ });
119
+ }
120
+ }
121
+ _JsonMap_instances = new WeakSet(), _JsonMap_transform = function _JsonMap_transform(node_1, input_1, output_1) {
122
+ return __awaiter(this, arguments, void 0, function* (node, input, output, path = '') {
123
+ logger.debug('#transform params:\n', { node, input, output, path });
124
+ // Checks if the current node is an object and has only a '$' key
125
+ if (node instanceof Object && _.size(node) === 1 && '$' in node) {
126
+ // Retrieves the transformations to be applied (can be an array or a single object)
127
+ const transformations = _.castArray(node.$);
128
+ logger.debug('transformations:\n', transformations);
129
+ // Array to store the results of the transformations
130
+ const results = [];
131
+ // Iterates over each transformation
132
+ for (const transformation of transformations) {
133
+ logger.debug('processing transformation:\n', transformation);
134
+ // Resolves the object path for the transformation
135
+ const { obj: methodObj, path: methodPath } = __classPrivateFieldGet(this, _JsonMap_instances, "m", _JsonMap_resolvePath).call(this, transformation.method, results);
136
+ // Resolves the parameter paths for the transformation
137
+ const params = _.castArray(transformation.params).map((param) => {
138
+ const { obj: paramObj, path: paramPath } = __classPrivateFieldGet(this, _JsonMap_instances, "m", _JsonMap_resolvePath).call(this, param, results);
139
+ return paramObj
140
+ ? paramPath
141
+ ? _.get(paramObj, paramPath)
142
+ : paramObj
143
+ : paramPath;
144
+ });
145
+ logger.debug('resolved transformation params:\n', params);
146
+ // Calls the specified method on the resolved object with the resolved parameters
147
+ const result = (yield _.invoke(methodObj, methodPath, ...params));
148
+ logger.debug('transformation result:\n', result);
149
+ // Stores the result of the transformation
150
+ results.unshift(result);
151
+ }
152
+ // Sets the output at the specified path to the last result of the transformations & returns.
153
+ _.set(output, path, results[0]);
154
+ logger.debug('updated output:\n', output);
155
+ return results[0];
156
+ }
157
+ // Checks if the current node is an object
158
+ if (_.isObject(node)) {
159
+ // Creates an empty array or object based on whether the current node is an array or not
160
+ const transformedNode = (Array.isArray(node) ? [] : {});
161
+ // Iterates over each key-value pair in the current node in ascending order by key
162
+ for (const [key, value] of _.sortBy(Object.entries(node), ([key]) => key)) {
163
+ // Constructs the current path by appending the current key to the previous path (if any)
164
+ const currentPath = path ? `${path}.${key}` : key;
165
+ // Recursively calls #transform with the current value, input, output, and path
166
+ // Assigns the transformed value to the corresponding key in the transformedNode
167
+ transformedNode[key] = yield __classPrivateFieldGet(this, _JsonMap_instances, "m", _JsonMap_transform).call(this, value, input, output, currentPath);
168
+ }
169
+ // Sets the output at the specified path to the transformedNode & returnsd.
170
+ _.set(output, path, transformedNode);
171
+ return transformedNode;
172
+ }
173
+ // Sets the output at the specified path to the current node & returns.
174
+ _.set(output, path, node);
175
+ return node;
176
+ });
177
+ }, _JsonMap_resolvePath = function _JsonMap_resolvePath(path, results) {
178
+ // If the path is not a string, return it as is
179
+ if (!_.isString(path)) {
180
+ return { path };
181
+ }
182
+ // Defines special patterns and their corresponding resolution functions
183
+ const patterns = {
184
+ '^\\$\\.(?<obj>lib|input|output)\\.?(?<path>.*)': ({ obj, path }) => ({
185
+ obj: this[obj],
186
+ path,
187
+ }),
188
+ '^\\$(?<path>\\[\\d+\\].*)': ({ path }) => ({
189
+ obj: results,
190
+ path,
191
+ }),
192
+ };
193
+ // Iterates over the special patterns
194
+ for (const [pattern, resolve] of Object.entries(patterns)) {
195
+ const match = path.match(pattern);
196
+ if (match === null || match === void 0 ? void 0 : match.groups)
197
+ return resolve(match.groups);
198
+ }
199
+ // Returns the path as is if it does not match any special patterns
200
+ return { path };
201
+ };
202
+
203
+ export { JsonMap };
package/package.json CHANGED
@@ -1,84 +1,77 @@
1
1
  {
2
- "name": "@karmaniverous/jsonmap",
3
- "version": "1.0.1",
4
- "publishConfig": {
5
- "access": "public"
6
- },
7
- "repository": {
8
- "type": "git",
9
- "url": "https://github.com/karmaniverous/jsonmap"
10
- },
11
2
  "author": "Jason G. Williscroft",
12
3
  "bugs": {
13
4
  "url": "https://github.com/karmaniverous/jsonmap/issues"
14
5
  },
15
- "description": "A hyper-generic JSON mapping library.",
16
- "homepage": "https://github.com/karmaniverous/jsonmap#readme",
17
- "keywords": [
18
- "json",
19
- "map",
20
- "es6",
21
- "javascript"
22
- ],
23
- "license": "BSD-3-Clause",
24
6
  "dependencies": {
25
- "lodash.castarray": "^4.4.0",
26
- "lodash.clonedeepwith": "^4.5.0",
27
- "lodash.get": "^4.4.2",
28
- "lodash.invoke": "^4.5.2",
29
- "lodash.isarray": "^4.0.0",
30
- "lodash.isarraylikeobject": "^4.2.0",
31
- "lodash.isobject": "^3.0.2",
32
- "lodash.isplainobject": "^4.0.6",
33
- "lodash.isstring": "^4.0.1",
34
- "lodash.isundefined": "^3.0.1",
35
- "lodash.mapvalues": "^4.6.0",
36
- "lodash.pickby": "^4.6.0",
37
- "lodash.set": "^4.3.2",
38
- "lodash.size": "^4.2.0",
39
- "lodash.sortby": "^4.7.0",
40
- "nanoid": "^5.0.6"
7
+ "lodash": "^4.17.21",
8
+ "nanoid": "^5.0.7",
9
+ "tslog": "^4.9.2",
10
+ "zod": "^3.23.8"
41
11
  },
12
+ "description": "A hyper-generic JSON mapping library.",
42
13
  "devDependencies": {
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
- "@babel/register": "^7.23.7",
49
- "@karmaniverous/get-dotenv": "^3.1.19",
50
- "@types/node": "^20.11.30",
14
+ "@eslint/js": "^9.3.0",
15
+ "@rollup/plugin-terser": "^0.4.4",
16
+ "@rollup/plugin-typescript": "^11.1.6",
17
+ "@types/chai": "^4.3.16",
18
+ "@types/debug": "^4.1.12",
19
+ "@types/eslint__js": "^8.42.3",
20
+ "@types/eslint-config-prettier": "^6.11.3",
21
+ "@types/eslint-plugin-mocha": "^10.4.0",
22
+ "@types/lodash": "^4.17.4",
23
+ "@types/mocha": "^10.0.6",
24
+ "@types/numeral": "^2.0.5",
51
25
  "auto-changelog": "^2.4.0",
52
- "chai": "^5.1.0",
53
- "concat-md": "^0.5.1",
26
+ "chai": "^5.1.1",
27
+ "cross-env": "^7.0.3",
54
28
  "eslint": "^8.57.0",
55
- "eslint-config-standard": "^17.1.0",
56
- "eslint-plugin-jsdoc": "^48.2.1",
57
- "eslint-plugin-mocha": "^10.4.1",
58
- "jsdoc-to-markdown": "^8.0.1",
59
- "lodash": "^4.17.21",
29
+ "eslint-config-prettier": "^9.1.0",
30
+ "eslint-plugin-mocha": "^10.4.3",
31
+ "eslint-plugin-simple-import-sort": "^12.1.0",
32
+ "jsdom-global": "^3.0.2",
33
+ "lefthook": "^1.6.12",
60
34
  "mocha": "^10.4.0",
61
35
  "numeral": "^2.0.6",
36
+ "nyc": "^15.1.0",
62
37
  "prettier": "^3.2.5",
63
- "release-it": "^17.1.1"
38
+ "release-it": "^17.3.0",
39
+ "rimraf": "^5.0.7",
40
+ "rollup": "^4.18.0",
41
+ "rollup-plugin-dts": "^6.1.1",
42
+ "source-map-support": "^0.5.21",
43
+ "ts-node": "^10.9.2",
44
+ "tslib": "^2.6.2",
45
+ "typescript": "^5.4.5",
46
+ "typescript-eslint": "^7.10.0"
64
47
  },
65
48
  "exports": {
66
49
  ".": {
67
- "import": "./lib/index.js",
68
- "require": "./dist/default/lib/index.js"
50
+ "import": {
51
+ "types": "./dist/index.d.mts",
52
+ "default": "./dist/index.mjs"
53
+ },
54
+ "require": {
55
+ "types": "./dist/index.d.cts",
56
+ "default": "./dist/index.cjs"
57
+ }
69
58
  }
70
59
  },
71
- "main": "./lib/index.js",
72
- "mocha": {
73
- "exclude": [
74
- "./dist/**",
75
- "./node_modules/**"
76
- ],
77
- "file": "./test/setup.js",
78
- "require": [
79
- "@babel/register"
80
- ],
81
- "spec": "./**/*.test.!(*.*)"
60
+ "files": [
61
+ "dist"
62
+ ],
63
+ "homepage": "https://github.com/karmaniverous/jsonmap#readme",
64
+ "keywords": [
65
+ "json",
66
+ "map",
67
+ "typescript"
68
+ ],
69
+ "license": "BSD-3-Clause",
70
+ "main": "dist/index.cjs",
71
+ "module": "dist/index.mjs",
72
+ "name": "@karmaniverous/jsonmap",
73
+ "publishConfig": {
74
+ "access": "public"
82
75
  },
83
76
  "release-it": {
84
77
  "git": {
@@ -91,10 +84,9 @@
91
84
  },
92
85
  "hooks": {
93
86
  "after:init": [
94
- "nr lint",
95
- "nr test",
96
- "nr build",
97
- "nr doc"
87
+ "npm run lint",
88
+ "npm run test",
89
+ "npm run build"
98
90
  ],
99
91
  "after:release": [
100
92
  "git switch -c release/${version}",
@@ -106,13 +98,18 @@
106
98
  "publish": true
107
99
  }
108
100
  },
101
+ "repository": {
102
+ "type": "git",
103
+ "url": "https://github.com/karmaniverous/jsonmap"
104
+ },
109
105
  "scripts": {
110
- "build": "babel lib -d dist/default/lib --delete-dir-on-start --config-file ./dist/default/.babelrc",
111
- "doc": "jsdoc2md -c doc/jsdoc.config.json -f lib/**/*.* -t doc/api-template.hbs > doc/2-api.jsdoc2.md && concat-md doc --hide-anchor-links > README.md",
112
- "lint": "eslint lib/**",
113
- "prerelease": "npm run lint && npm run test && npm run build && npm run doc",
114
- "release": "getdotenv -c \"release-it\"",
115
- "test": "getdotenv -c \"mocha\""
106
+ "build": "rimraf dist && rollup --config rollup.config.ts --configPlugin @rollup/plugin-typescript",
107
+ "lint": "eslint src/** && prettier -c src",
108
+ "lint:fix": "eslint --fix src/** && prettier --write src",
109
+ "release": "release-it",
110
+ "test": "cross-env LOG_LEVEL=0 nyc mocha"
116
111
  },
117
- "type": "module"
112
+ "type": "module",
113
+ "types": "dist/index.d.ts",
114
+ "version": "2.0.1"
118
115
  }
package/.env DELETED
@@ -1 +0,0 @@
1
- # Load with getdotenv: https://github.com/karmaniverous/get-dotenv