@karmaniverous/jsonmap 1.0.0 → 2.0.0

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,188 @@
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, _c;
44
+ const npmPackageRegex = /^(?:(?<packageScope>@[a-z0-9-~][a-z0-9-._~]*)\/)?(?<packageName>[a-z0-9-~][a-z0-9-._~]*)$/;
45
+ const { packageScope, packageName } = ((_c = (_b = (_a$1 = process.env.npm_package_name) === null || _a$1 === void 0 ? void 0 : _a$1.match(npmPackageRegex)) === null || _b === void 0 ? void 0 : _b.groups) !== null && _c !== void 0 ? _c : {});
46
+
47
+ var _a;
48
+ const logLevel = _.parseInt((_a = process.env.LOG_LEVEL) !== null && _a !== void 0 ? _a : '');
49
+ const logger = new tslog.Logger({
50
+ hideLogPositionForProduction: true,
51
+ minLevel: _.isNaN(logLevel) ? undefined : logLevel,
52
+ name: packageName,
53
+ type: _.isNaN(logLevel) ? 'hidden' : 'pretty',
54
+ });
55
+
56
+ var _JsonMap_instances, _JsonMap_transform, _JsonMap_resolvePath;
57
+ // The transformation process leverages JSON.stringify and JSON.parse to eliminate circular references.
58
+ const getJsonFns = () => {
59
+ const seen = new WeakSet();
60
+ const undefinedToken = nanoid.nanoid();
61
+ const replacer = (key, value) => {
62
+ if (typeof value === 'object' && value !== null) {
63
+ if (seen.has(value)) {
64
+ return 'CIRCULAR REFERENCE';
65
+ }
66
+ seen.add(value);
67
+ if (!_.isArrayLikeObject(value))
68
+ return Object.getOwnPropertyNames(value).reduce((v, p) => (Object.assign(Object.assign({}, v), { [p]: value[p] })), {});
69
+ }
70
+ return _.isUndefined(value) ? undefinedToken : value;
71
+ };
72
+ const reviver = (key, value) => value === undefinedToken ? undefined : value;
73
+ return { replacer, reviver };
74
+ };
75
+ /**
76
+ * JsonMap class to apply transformations to a JSON object
77
+ */
78
+ class JsonMap {
79
+ constructor(map = {}, lib = {}, { ignore = /^\$/ } = {}) {
80
+ _JsonMap_instances.add(this);
81
+ this.map = map;
82
+ this.lib = lib;
83
+ this.ignore = _.isString(ignore) ? new RegExp(ignore) : ignore;
84
+ }
85
+ /**
86
+ * Transforms the input data according to the map configuration.
87
+ */
88
+ transform(input) {
89
+ return __awaiter(this, void 0, void 0, function* () {
90
+ // Sets the input data and initializes an empty output object
91
+ this.input = input;
92
+ this.output = {};
93
+ // Perform transformation & eliminate recursion from result.
94
+ const { replacer, reviver } = getJsonFns();
95
+ const result = JSON.parse(JSON.stringify(yield __classPrivateFieldGet(this, _JsonMap_instances, "m", _JsonMap_transform).call(this, this.map, this.input, this.output), replacer), reviver);
96
+ // Recursively eliminate non-string keys & string keys starting with $ and not in ignoreExclusions.
97
+ const deep = (value) => value instanceof Object && !_.isArray(value)
98
+ ? _.mapValues(_.pickBy(value, (v, k) => !this.ignore.test(k)), (value) => _.cloneDeepWith(value, deep))
99
+ : undefined;
100
+ return _.cloneDeepWith(result, deep);
101
+ });
102
+ }
103
+ }
104
+ _JsonMap_instances = new WeakSet(), _JsonMap_transform = function _JsonMap_transform(node_1, input_1, output_1) {
105
+ return __awaiter(this, arguments, void 0, function* (node, input, output, path = '') {
106
+ logger.debug('#transform params:\n', { node, input, output, path });
107
+ // Checks if the current node is an object and has only a '$' key
108
+ if (node instanceof Object && _.size(node) === 1 && '$' in node) {
109
+ // Retrieves the transformations to be applied (can be an array or a single object)
110
+ const transformations = _.castArray(node.$);
111
+ logger.debug('transformations:\n', transformations);
112
+ // Array to store the results of the transformations
113
+ const results = [];
114
+ // Iterates over each transformation
115
+ for (const transformation of transformations) {
116
+ logger.debug('processing transformation:\n', transformation);
117
+ // Resolves the object path for the transformation
118
+ const { obj: methodObj, path: methodPath } = __classPrivateFieldGet(this, _JsonMap_instances, "m", _JsonMap_resolvePath).call(this, transformation.method, results);
119
+ // Resolves the parameter paths for the transformation
120
+ const params = _.castArray(transformation.params).map((param) => {
121
+ const { obj: paramObj, path: paramPath } = __classPrivateFieldGet(this, _JsonMap_instances, "m", _JsonMap_resolvePath).call(this, param, results);
122
+ return paramObj
123
+ ? paramPath
124
+ ? _.get(paramObj, paramPath)
125
+ : paramObj
126
+ : paramPath;
127
+ });
128
+ logger.debug('resolved transformation params:\n', params);
129
+ // Calls the specified method on the resolved object with the resolved parameters
130
+ const result = (yield _.invoke(methodObj, methodPath, ...params));
131
+ logger.debug('transformation result:\n', result);
132
+ // Stores the result of the transformation
133
+ results.unshift(result);
134
+ }
135
+ // Sets the output at the specified path to the last result of the transformations & returns.
136
+ _.set(output, path, results[0]);
137
+ logger.debug('updated output:\n', output);
138
+ return results[0];
139
+ }
140
+ // Checks if the current node is an object
141
+ if (_.isObject(node)) {
142
+ // Creates an empty array or object based on whether the current node is an array or not
143
+ const transformedNode = (Array.isArray(node) ? [] : {});
144
+ // Iterates over each key-value pair in the current node in ascending order by key
145
+ for (const [key, value] of _.sortBy(Object.entries(node), ([key]) => key)) {
146
+ // Constructs the current path by appending the current key to the previous path (if any)
147
+ const currentPath = path ? `${path}.${key}` : key;
148
+ // Recursively calls #transform with the current value, input, output, and path
149
+ // Assigns the transformed value to the corresponding key in the transformedNode
150
+ transformedNode[key] = yield __classPrivateFieldGet(this, _JsonMap_instances, "m", _JsonMap_transform).call(this, value, input, output, currentPath);
151
+ }
152
+ // Sets the output at the specified path to the transformedNode & returnsd.
153
+ _.set(output, path, transformedNode);
154
+ return transformedNode;
155
+ }
156
+ // Sets the output at the specified path to the current node & returns.
157
+ _.set(output, path, node);
158
+ return node;
159
+ });
160
+ }, _JsonMap_resolvePath = function _JsonMap_resolvePath(path, results) {
161
+ // If the path is not a string, return it as is
162
+ if (!_.isString(path)) {
163
+ return { path };
164
+ }
165
+ // Defines special patterns and their corresponding resolution functions
166
+ const patterns = {
167
+ '^\\$\\.(?<obj>lib|input|output)\\.?(?<path>.*)': ({ obj, path }) => ({
168
+ obj: this[obj],
169
+ path,
170
+ }),
171
+ '^\\$(?<path>\\[\\d+\\].*)': ({ path }) => ({
172
+ obj: results,
173
+ path,
174
+ }),
175
+ };
176
+ // Iterates over the special patterns
177
+ for (const [pattern, resolve] of Object.entries(patterns)) {
178
+ const match = path.match(pattern);
179
+ if (match === null || match === void 0 ? void 0 : match.groups)
180
+ return resolve(match.groups);
181
+ }
182
+ // Returns the path as is if it does not match any special patterns
183
+ return { path };
184
+ };
185
+
186
+ exports.JsonMap = JsonMap;
187
+
188
+ })(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 h;const d=e.parseInt(null!==(h=process.env.LOG_LEVEL)&&void 0!==h?h:""),f=new r.Logger({hideLogPositionForProduction:!0,minLevel:e.isNaN(d)?void 0:d,name:p,type:e.isNaN(d)?"hidden":"pretty"});var l,m,g;l=new WeakSet,m=function t(n,r,s){return o(this,arguments,void 0,(function*(n,r,o,s=""){if(f.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.$);f.debug("transformations:\n",t);const r=[];for(const n of t){f.debug("processing transformation:\n",n);const{obj:t,path:o}=i(this,l,"m",g).call(this,n.method,r),s=e.castArray(n.params).map((t=>{const{obj:n,path:o}=i(this,l,"m",g).call(this,t,r);return n?o?e.get(n,o):n:o}));f.debug("resolved transformation params:\n",s);const a=yield e.invoke(t,o,...s);f.debug("transformation result:\n",a),r.unshift(a)}return e.set(o,s,r[0]),f.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,l,"m",t).call(this,u,r,o,e)}return e.set(o,s,a),a}return e.set(o,s,n),n}))},g=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=/^\$/}={}){l.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,l,"m",m).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,187 @@
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, _c;
45
+ const npmPackageRegex = /^(?:(?<packageScope>@[a-z0-9-~][a-z0-9-._~]*)\/)?(?<packageName>[a-z0-9-~][a-z0-9-._~]*)$/;
46
+ const { packageScope, packageName } = ((_c = (_b = (_a$1 = process.env.npm_package_name) === null || _a$1 === void 0 ? void 0 : _a$1.match(npmPackageRegex)) === null || _b === void 0 ? void 0 : _b.groups) !== null && _c !== void 0 ? _c : {});
47
+
48
+ var _a;
49
+ const logLevel = _.parseInt((_a = process.env.LOG_LEVEL) !== null && _a !== void 0 ? _a : '');
50
+ const logger = new Logger({
51
+ hideLogPositionForProduction: true,
52
+ minLevel: _.isNaN(logLevel) ? undefined : logLevel,
53
+ name: packageName,
54
+ type: _.isNaN(logLevel) ? 'hidden' : 'pretty',
55
+ });
56
+
57
+ var _JsonMap_instances, _JsonMap_transform, _JsonMap_resolvePath;
58
+ // The transformation process leverages JSON.stringify and JSON.parse to eliminate circular references.
59
+ const getJsonFns = () => {
60
+ const seen = new WeakSet();
61
+ const undefinedToken = nanoid();
62
+ const replacer = (key, value) => {
63
+ if (typeof value === 'object' && value !== null) {
64
+ if (seen.has(value)) {
65
+ return 'CIRCULAR REFERENCE';
66
+ }
67
+ seen.add(value);
68
+ if (!_.isArrayLikeObject(value))
69
+ return Object.getOwnPropertyNames(value).reduce((v, p) => (Object.assign(Object.assign({}, v), { [p]: value[p] })), {});
70
+ }
71
+ return _.isUndefined(value) ? undefinedToken : value;
72
+ };
73
+ const reviver = (key, value) => value === undefinedToken ? undefined : value;
74
+ return { replacer, reviver };
75
+ };
76
+ /**
77
+ * JsonMap class to apply transformations to a JSON object
78
+ */
79
+ class JsonMap {
80
+ constructor(map = {}, lib = {}, { ignore = /^\$/ } = {}) {
81
+ _JsonMap_instances.add(this);
82
+ this.map = map;
83
+ this.lib = lib;
84
+ this.ignore = _.isString(ignore) ? new RegExp(ignore) : ignore;
85
+ }
86
+ /**
87
+ * Transforms the input data according to the map configuration.
88
+ */
89
+ transform(input) {
90
+ return __awaiter(this, void 0, void 0, function* () {
91
+ // Sets the input data and initializes an empty output object
92
+ this.input = input;
93
+ this.output = {};
94
+ // Perform transformation & eliminate recursion from result.
95
+ const { replacer, reviver } = getJsonFns();
96
+ const result = JSON.parse(JSON.stringify(yield __classPrivateFieldGet(this, _JsonMap_instances, "m", _JsonMap_transform).call(this, this.map, this.input, this.output), replacer), reviver);
97
+ // Recursively eliminate non-string keys & string keys starting with $ and not in ignoreExclusions.
98
+ const deep = (value) => value instanceof Object && !_.isArray(value)
99
+ ? _.mapValues(_.pickBy(value, (v, k) => !this.ignore.test(k)), (value) => _.cloneDeepWith(value, deep))
100
+ : undefined;
101
+ return _.cloneDeepWith(result, deep);
102
+ });
103
+ }
104
+ }
105
+ _JsonMap_instances = new WeakSet(), _JsonMap_transform = function _JsonMap_transform(node_1, input_1, output_1) {
106
+ return __awaiter(this, arguments, void 0, function* (node, input, output, path = '') {
107
+ logger.debug('#transform params:\n', { node, input, output, path });
108
+ // Checks if the current node is an object and has only a '$' key
109
+ if (node instanceof Object && _.size(node) === 1 && '$' in node) {
110
+ // Retrieves the transformations to be applied (can be an array or a single object)
111
+ const transformations = _.castArray(node.$);
112
+ logger.debug('transformations:\n', transformations);
113
+ // Array to store the results of the transformations
114
+ const results = [];
115
+ // Iterates over each transformation
116
+ for (const transformation of transformations) {
117
+ logger.debug('processing transformation:\n', transformation);
118
+ // Resolves the object path for the transformation
119
+ const { obj: methodObj, path: methodPath } = __classPrivateFieldGet(this, _JsonMap_instances, "m", _JsonMap_resolvePath).call(this, transformation.method, results);
120
+ // Resolves the parameter paths for the transformation
121
+ const params = _.castArray(transformation.params).map((param) => {
122
+ const { obj: paramObj, path: paramPath } = __classPrivateFieldGet(this, _JsonMap_instances, "m", _JsonMap_resolvePath).call(this, param, results);
123
+ return paramObj
124
+ ? paramPath
125
+ ? _.get(paramObj, paramPath)
126
+ : paramObj
127
+ : paramPath;
128
+ });
129
+ logger.debug('resolved transformation params:\n', params);
130
+ // Calls the specified method on the resolved object with the resolved parameters
131
+ const result = (yield _.invoke(methodObj, methodPath, ...params));
132
+ logger.debug('transformation result:\n', result);
133
+ // Stores the result of the transformation
134
+ results.unshift(result);
135
+ }
136
+ // Sets the output at the specified path to the last result of the transformations & returns.
137
+ _.set(output, path, results[0]);
138
+ logger.debug('updated output:\n', output);
139
+ return results[0];
140
+ }
141
+ // Checks if the current node is an object
142
+ if (_.isObject(node)) {
143
+ // Creates an empty array or object based on whether the current node is an array or not
144
+ const transformedNode = (Array.isArray(node) ? [] : {});
145
+ // Iterates over each key-value pair in the current node in ascending order by key
146
+ for (const [key, value] of _.sortBy(Object.entries(node), ([key]) => key)) {
147
+ // Constructs the current path by appending the current key to the previous path (if any)
148
+ const currentPath = path ? `${path}.${key}` : key;
149
+ // Recursively calls #transform with the current value, input, output, and path
150
+ // Assigns the transformed value to the corresponding key in the transformedNode
151
+ transformedNode[key] = yield __classPrivateFieldGet(this, _JsonMap_instances, "m", _JsonMap_transform).call(this, value, input, output, currentPath);
152
+ }
153
+ // Sets the output at the specified path to the transformedNode & returnsd.
154
+ _.set(output, path, transformedNode);
155
+ return transformedNode;
156
+ }
157
+ // Sets the output at the specified path to the current node & returns.
158
+ _.set(output, path, node);
159
+ return node;
160
+ });
161
+ }, _JsonMap_resolvePath = function _JsonMap_resolvePath(path, results) {
162
+ // If the path is not a string, return it as is
163
+ if (!_.isString(path)) {
164
+ return { path };
165
+ }
166
+ // Defines special patterns and their corresponding resolution functions
167
+ const patterns = {
168
+ '^\\$\\.(?<obj>lib|input|output)\\.?(?<path>.*)': ({ obj, path }) => ({
169
+ obj: this[obj],
170
+ path,
171
+ }),
172
+ '^\\$(?<path>\\[\\d+\\].*)': ({ path }) => ({
173
+ obj: results,
174
+ path,
175
+ }),
176
+ };
177
+ // Iterates over the special patterns
178
+ for (const [pattern, resolve] of Object.entries(patterns)) {
179
+ const match = path.match(pattern);
180
+ if (match === null || match === void 0 ? void 0 : match.groups)
181
+ return resolve(match.groups);
182
+ }
183
+ // Returns the path as is if it does not match any special patterns
184
+ return { path };
185
+ };
186
+
187
+ export { JsonMap };
package/package.json CHANGED
@@ -1,84 +1,77 @@
1
1
  {
2
- "name": "@karmaniverous/jsonmap",
3
- "version": "1.0.0",
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.6",
9
+ "tslog": "^4.9.2",
10
+ "zod": "^3.22.4"
41
11
  },
12
+ "description": "A hyper-generic JSON mapping library.",
42
13
  "devDependencies": {
43
- "@babel/cli": "^7.23.9",
44
- "@babel/core": "^7.23.9",
45
- "@babel/eslint-parser": "^7.23.10",
46
- "@babel/plugin-syntax-import-assertions": "^7.23.3",
47
- "@babel/preset-env": "^7.23.9",
48
- "@babel/register": "^7.23.7",
49
- "@karmaniverous/get-dotenv": "^3.1.18",
50
- "@types/node": "^20.11.20",
14
+ "@eslint/js": "^9.0.0",
15
+ "@rollup/plugin-terser": "^0.4.4",
16
+ "@rollup/plugin-typescript": "^11.1.6",
17
+ "@types/chai": "^4.3.14",
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.0",
23
+ "@types/mocha": "^10.0.6",
24
+ "@types/numeral": "^2.0.5",
51
25
  "auto-changelog": "^2.4.0",
52
26
  "chai": "^5.1.0",
53
- "concat-md": "^0.5.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.0",
57
- "eslint-plugin-mocha": "^10.3.0",
58
- "jsdoc-to-markdown": "^8.0.1",
59
- "lodash": "^4.17.21",
60
- "mocha": "^10.3.0",
29
+ "eslint-config-prettier": "^9.1.0",
30
+ "eslint-plugin-mocha": "^10.4.2",
31
+ "eslint-plugin-simple-import-sort": "^12.0.0",
32
+ "jsdom-global": "^3.0.2",
33
+ "lefthook": "^1.6.10",
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.2.0",
39
+ "rimraf": "^5.0.5",
40
+ "rollup": "^4.14.2",
41
+ "rollup-plugin-dts": "^6.1.0",
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.6.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.0"
118
115
  }
package/.env DELETED
@@ -1 +0,0 @@
1
- # Load with getdotenv: https://github.com/karmaniverous/get-dotenv