@karmaniverous/jsonmap 0.0.7 → 0.2.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.
- package/README.md +9 -9
- package/dist/default/lib/JsonMap/JsonMap.js +129 -103
- package/lib/JsonMap/JsonMap.js +65 -51
- package/package.json +21 -18
package/README.md
CHANGED
|
@@ -46,6 +46,9 @@ const map = {
|
|
|
46
46
|
bar: [
|
|
47
47
|
{
|
|
48
48
|
static: 'another static value',
|
|
49
|
+
// Keys starting with $ are available for progressive transformations but
|
|
50
|
+
// are not passed to the output object.
|
|
51
|
+
$remove: 'this should be removed from the output',
|
|
49
52
|
// Value defined by a mapping rule expressing an array of transformation
|
|
50
53
|
// objects. If there is only a single transformation object, no array is
|
|
51
54
|
// necessary. The output of the last transformation step is returned as
|
|
@@ -53,10 +56,9 @@ const map = {
|
|
|
53
56
|
dynamic: {
|
|
54
57
|
$: [
|
|
55
58
|
// Each transformation object uses a special syntax to reference an
|
|
56
|
-
//
|
|
59
|
+
// a method to run and an array of parameters to pass to it.
|
|
57
60
|
{
|
|
58
|
-
|
|
59
|
-
method: 'get',
|
|
61
|
+
method: '$.lib._.get',
|
|
60
62
|
params: ['$.input', 'dynamodb.NewImage.roundup.N'],
|
|
61
63
|
},
|
|
62
64
|
// The special syntax uses lodash-style paths. Its root object can
|
|
@@ -64,14 +66,12 @@ const map = {
|
|
|
64
66
|
// ($.input...), the output generated so far ($.output...), or the
|
|
65
67
|
// outputs of previous transformation steps ($.[0]..., $.[1]...).
|
|
66
68
|
{
|
|
67
|
-
|
|
68
|
-
method: 'numeral',
|
|
69
|
+
method: '$.lib.numeral',
|
|
69
70
|
// If there is only a single param, no array is necessary.
|
|
70
71
|
params: '$[0]',
|
|
71
72
|
},
|
|
72
73
|
{
|
|
73
|
-
|
|
74
|
-
method: 'format',
|
|
74
|
+
method: '$[0].format',
|
|
75
75
|
params: '$0,0.00',
|
|
76
76
|
},
|
|
77
77
|
],
|
|
@@ -82,11 +82,11 @@ const map = {
|
|
|
82
82
|
// previous output of the same mapping object.
|
|
83
83
|
progressive: {
|
|
84
84
|
$: {
|
|
85
|
-
|
|
86
|
-
method: 'toUpper',
|
|
85
|
+
method: '$.lib._.toUpper',
|
|
87
86
|
params: '$.output.bar[0].static',
|
|
88
87
|
},
|
|
89
88
|
},
|
|
89
|
+
$remove: 'this should be removed from the output',
|
|
90
90
|
};
|
|
91
91
|
```
|
|
92
92
|
|
|
@@ -5,20 +5,20 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.JsonMap = void 0;
|
|
7
7
|
var _lodash = _interopRequireDefault(require("lodash.castarray"));
|
|
8
|
-
var _lodash2 = _interopRequireDefault(require("lodash.
|
|
9
|
-
var _lodash3 = _interopRequireDefault(require("lodash.
|
|
10
|
-
var _lodash4 = _interopRequireDefault(require("lodash.
|
|
11
|
-
var _lodash5 = _interopRequireDefault(require("lodash.
|
|
12
|
-
var _lodash6 = _interopRequireDefault(require("lodash.
|
|
13
|
-
var _lodash7 = _interopRequireDefault(require("lodash.
|
|
14
|
-
var _lodash8 = _interopRequireDefault(require("lodash.
|
|
15
|
-
var _lodash9 = _interopRequireDefault(require("lodash.
|
|
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.isarray"));
|
|
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.mapvalues"));
|
|
16
|
+
var _lodash10 = _interopRequireDefault(require("lodash.pickby"));
|
|
17
|
+
var _lodash11 = _interopRequireDefault(require("lodash.set"));
|
|
18
|
+
var _lodash12 = _interopRequireDefault(require("lodash.size"));
|
|
16
19
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
|
|
20
|
-
var _transform = /*#__PURE__*/new WeakSet();
|
|
21
|
-
var _resolvePath = /*#__PURE__*/new WeakSet();
|
|
20
|
+
// npm imports
|
|
21
|
+
|
|
22
22
|
/**
|
|
23
23
|
* JsonMap class to apply transformations to a JSON object
|
|
24
24
|
*/
|
|
@@ -32,8 +32,6 @@ class JsonMap {
|
|
|
32
32
|
constructor() {
|
|
33
33
|
let map = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
34
34
|
let lib = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
35
|
-
_classPrivateMethodInitSpec(this, _resolvePath);
|
|
36
|
-
_classPrivateMethodInitSpec(this, _transform);
|
|
37
35
|
this.map = map;
|
|
38
36
|
this.lib = lib;
|
|
39
37
|
}
|
|
@@ -42,7 +40,7 @@ class JsonMap {
|
|
|
42
40
|
* Transforms the input data according to the map configuration.
|
|
43
41
|
*
|
|
44
42
|
* @param {object} input - The input data to be transformed.
|
|
45
|
-
* @
|
|
43
|
+
* @return {object} - The transformed data.
|
|
46
44
|
*/
|
|
47
45
|
async transform(input) {
|
|
48
46
|
// Sets the input data and initializes an empty output object
|
|
@@ -50,7 +48,11 @@ class JsonMap {
|
|
|
50
48
|
this.output = {};
|
|
51
49
|
|
|
52
50
|
// Calls the #transform method to perform the transformation
|
|
53
|
-
|
|
51
|
+
const result = await this.#transform(this.map, this.input, this.output);
|
|
52
|
+
|
|
53
|
+
// Recursively eliminate keys starting with $.
|
|
54
|
+
const deep = value => (0, _lodash7.default)(value) ? (0, _lodash9.default)((0, _lodash10.default)(value, (v, k) => /^[^$]/.test(k)), value => (0, _lodash2.default)(value, deep)) : undefined;
|
|
55
|
+
return (0, _lodash2.default)(result, deep);
|
|
54
56
|
}
|
|
55
57
|
|
|
56
58
|
/**
|
|
@@ -60,105 +62,129 @@ class JsonMap {
|
|
|
60
62
|
* @param {object} input - The current input node.
|
|
61
63
|
* @param {object} output - The current output node.
|
|
62
64
|
* @param {string} path - The path to the current node.
|
|
63
|
-
* @
|
|
65
|
+
* @return {object} - The transformed node.
|
|
64
66
|
* @private
|
|
65
67
|
*/
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
//
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
68
|
+
async #transform(node, input, output) {
|
|
69
|
+
let path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
|
|
70
|
+
// Checks if the current node is an object and has a '$' key
|
|
71
|
+
if ((0, _lodash7.default)(node) && (0, _lodash12.default)(node) === 1 && '$' in node) {
|
|
72
|
+
// Retrieves the transformations to be applied (can be an array or a single object)
|
|
73
|
+
const transformations = (0, _lodash.default)(node['$']);
|
|
74
|
+
|
|
75
|
+
// Array to store the results of the transformations
|
|
76
|
+
let results = [];
|
|
77
|
+
|
|
78
|
+
// Iterates over each transformation
|
|
79
|
+
for (const transformation of transformations) {
|
|
80
|
+
// Resolves the object path for the transformation
|
|
81
|
+
const {
|
|
82
|
+
obj: methodObj,
|
|
83
|
+
path: methodPath
|
|
84
|
+
} = this.#resolvePath(transformation.method, results);
|
|
85
|
+
|
|
86
|
+
// Resolves the parameter paths for the transformation
|
|
87
|
+
const params = await Promise.all((0, _lodash.default)(transformation.params).map(param => {
|
|
88
|
+
const {
|
|
89
|
+
obj: paramObj,
|
|
90
|
+
path: paramPath
|
|
91
|
+
} = this.#resolvePath(param, results);
|
|
92
|
+
return paramObj ? paramPath ? (0, _lodash3.default)(paramObj, paramPath) : paramObj : paramPath;
|
|
93
|
+
}));
|
|
94
|
+
|
|
95
|
+
// Calls the specified method on the resolved object with the resolved parameters
|
|
96
|
+
const result = await (0, _lodash4.default)(methodObj, methodPath, ...params);
|
|
97
|
+
|
|
98
|
+
// Stores the result of the transformation
|
|
99
|
+
results.unshift(result);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Sets the output at the specified path to the last result of the transformations & returns.
|
|
103
|
+
(0, _lodash11.default)(output, path, results[0]);
|
|
104
|
+
return results[0];
|
|
93
105
|
}
|
|
94
106
|
|
|
95
|
-
//
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}
|
|
107
|
+
// Checks if the current node is an object
|
|
108
|
+
if ((0, _lodash6.default)(node)) {
|
|
109
|
+
// Creates an empty array or object based on whether the current node is an array or not
|
|
110
|
+
const transformedNode = (0, _lodash5.default)(node) ? [] : {};
|
|
100
111
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
112
|
+
// Iterates over each key-value pair in the current node
|
|
113
|
+
for (const [key, value] of Object.entries(node)) {
|
|
114
|
+
// Constructs the current path by appending the current key to the previous path (if any)
|
|
115
|
+
const currentPath = path ? `${path}.${key}` : key;
|
|
105
116
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
117
|
+
// Recursively calls #transform with the current value, input, output, and path
|
|
118
|
+
// Assigns the transformed value to the corresponding key in the transformedNode
|
|
119
|
+
transformedNode[key] = await this.#transform(value, input, output, currentPath);
|
|
120
|
+
}
|
|
110
121
|
|
|
111
|
-
//
|
|
112
|
-
|
|
113
|
-
transformedNode
|
|
122
|
+
// Sets the output at the specified path to the transformedNode & returnsd.
|
|
123
|
+
(0, _lodash11.default)(output, path, transformedNode);
|
|
124
|
+
return transformedNode;
|
|
114
125
|
}
|
|
115
126
|
|
|
116
|
-
// Sets the output at the specified path to the
|
|
117
|
-
(0,
|
|
118
|
-
return
|
|
127
|
+
// Sets the output at the specified path to the current node & returns.
|
|
128
|
+
(0, _lodash11.default)(output, path, node);
|
|
129
|
+
return node;
|
|
119
130
|
}
|
|
120
131
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
if (!(0, _lodash6.default)(path)) {
|
|
128
|
-
return path;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// Defines special patterns and their corresponding values for resolution
|
|
132
|
-
const specialPatterns = {
|
|
133
|
-
'$.lib': this.lib,
|
|
134
|
-
'$.input': this.input,
|
|
135
|
-
'$.output': this.output
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
// Checks if the path matches the pattern for accessing previous results
|
|
139
|
-
const match = path.match(/^\$\[(\d+)\](.*)$/);
|
|
140
|
-
|
|
141
|
-
// Retrieves the value from the previous results based on the index and any remaining path
|
|
142
|
-
if (match) {
|
|
143
|
-
const [, index, rest] = match;
|
|
144
|
-
const value = (0, _lodash2.default)(results, [results.length - 1 - (0, _lodash8.default)(index), ...(rest.length ? rest.split('.') : [])]);
|
|
145
|
-
|
|
146
|
-
// Returns the value if it exists, otherwise returns the original path
|
|
147
|
-
return value != null ? value : path;
|
|
148
|
-
}
|
|
132
|
+
/**
|
|
133
|
+
* @typedef {object} PathResolution
|
|
134
|
+
* @property {object} obj - The object to be used for the transformation.
|
|
135
|
+
* @property {string} path - The path to the value to be used for the transformation.
|
|
136
|
+
* @private
|
|
137
|
+
*/
|
|
149
138
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
139
|
+
/**
|
|
140
|
+
* Resolves the method/params path for a transformation
|
|
141
|
+
*
|
|
142
|
+
* @param {string} path - The path to be resolved.
|
|
143
|
+
* @param {Array} results - The results from previous transformations.
|
|
144
|
+
* @return {PathResolution} - The resolved path.
|
|
145
|
+
* @private
|
|
146
|
+
*/
|
|
147
|
+
#resolvePath(path, results) {
|
|
148
|
+
// If the path is not a string, return it as is
|
|
149
|
+
if (!(0, _lodash8.default)(path)) {
|
|
150
|
+
return path;
|
|
151
|
+
}
|
|
154
152
|
|
|
155
|
-
//
|
|
156
|
-
const
|
|
153
|
+
// Defines special patterns and their corresponding resolution functions
|
|
154
|
+
const patterns = {
|
|
155
|
+
'^\\$\\.(?<obj>lib|input|output)\\.?(?<path>.*)': _ref => {
|
|
156
|
+
let {
|
|
157
|
+
obj,
|
|
158
|
+
path
|
|
159
|
+
} = _ref;
|
|
160
|
+
return {
|
|
161
|
+
obj: this[obj],
|
|
162
|
+
path
|
|
163
|
+
};
|
|
164
|
+
},
|
|
165
|
+
'^\\$(?<path>\\[\\d+\\].*)': _ref2 => {
|
|
166
|
+
let {
|
|
167
|
+
path
|
|
168
|
+
} = _ref2;
|
|
169
|
+
return {
|
|
170
|
+
obj: results,
|
|
171
|
+
path
|
|
172
|
+
};
|
|
173
|
+
}
|
|
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) return resolve(match.groups);
|
|
180
|
+
}
|
|
157
181
|
|
|
158
|
-
// Returns the
|
|
159
|
-
return
|
|
182
|
+
// Returns the path as is if it does not match any special patterns
|
|
183
|
+
return {
|
|
184
|
+
path
|
|
185
|
+
};
|
|
160
186
|
}
|
|
187
|
+
}
|
|
161
188
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
189
|
+
// Exports the JsonMap class as the default export of this module
|
|
190
|
+
exports.JsonMap = JsonMap;
|
package/lib/JsonMap/JsonMap.js
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
// npm imports
|
|
2
2
|
import castArray from 'lodash.castarray';
|
|
3
|
+
import cloneDeepWith from 'lodash.clonedeepwith';
|
|
3
4
|
import get from 'lodash.get';
|
|
4
|
-
import
|
|
5
|
+
import invoke from 'lodash.invoke';
|
|
5
6
|
import isArray from 'lodash.isarray';
|
|
6
7
|
import isObject from 'lodash.isobject';
|
|
8
|
+
import isPlainObject from 'lodash.isplainobject';
|
|
7
9
|
import isString from 'lodash.isstring';
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
+
import mapValues from 'lodash.mapvalues';
|
|
11
|
+
import pickBy from 'lodash.pickby';
|
|
10
12
|
import set from 'lodash.set';
|
|
13
|
+
import size from 'lodash.size';
|
|
11
14
|
|
|
12
15
|
/**
|
|
13
16
|
* JsonMap class to apply transformations to a JSON object
|
|
@@ -28,7 +31,7 @@ class JsonMap {
|
|
|
28
31
|
* Transforms the input data according to the map configuration.
|
|
29
32
|
*
|
|
30
33
|
* @param {object} input - The input data to be transformed.
|
|
31
|
-
* @
|
|
34
|
+
* @return {object} - The transformed data.
|
|
32
35
|
*/
|
|
33
36
|
async transform(input) {
|
|
34
37
|
// Sets the input data and initializes an empty output object
|
|
@@ -36,7 +39,18 @@ class JsonMap {
|
|
|
36
39
|
this.output = {};
|
|
37
40
|
|
|
38
41
|
// Calls the #transform method to perform the transformation
|
|
39
|
-
|
|
42
|
+
const result = await this.#transform(this.map, this.input, this.output);
|
|
43
|
+
|
|
44
|
+
// Recursively eliminate keys starting with $.
|
|
45
|
+
const deep = (value) =>
|
|
46
|
+
isPlainObject(value)
|
|
47
|
+
? mapValues(
|
|
48
|
+
pickBy(value, (v, k) => /^[^$]/.test(k)),
|
|
49
|
+
(value) => cloneDeepWith(value, deep)
|
|
50
|
+
)
|
|
51
|
+
: undefined;
|
|
52
|
+
|
|
53
|
+
return cloneDeepWith(result, deep);
|
|
40
54
|
}
|
|
41
55
|
|
|
42
56
|
/**
|
|
@@ -46,14 +60,14 @@ class JsonMap {
|
|
|
46
60
|
* @param {object} input - The current input node.
|
|
47
61
|
* @param {object} output - The current output node.
|
|
48
62
|
* @param {string} path - The path to the current node.
|
|
49
|
-
* @
|
|
63
|
+
* @return {object} - The transformed node.
|
|
50
64
|
* @private
|
|
51
65
|
*/
|
|
52
66
|
async #transform(node, input, output, path = '') {
|
|
53
67
|
// Checks if the current node is an object and has a '$' key
|
|
54
|
-
if (
|
|
68
|
+
if (isPlainObject(node) && size(node) === 1 && '$' in node) {
|
|
55
69
|
// Retrieves the transformations to be applied (can be an array or a single object)
|
|
56
|
-
const transformations =
|
|
70
|
+
const transformations = castArray(node['$']);
|
|
57
71
|
|
|
58
72
|
// Array to store the results of the transformations
|
|
59
73
|
let results = [];
|
|
@@ -61,26 +75,36 @@ class JsonMap {
|
|
|
61
75
|
// Iterates over each transformation
|
|
62
76
|
for (const transformation of transformations) {
|
|
63
77
|
// Resolves the object path for the transformation
|
|
64
|
-
const
|
|
78
|
+
const { obj: methodObj, path: methodPath } = this.#resolvePath(
|
|
79
|
+
transformation.method,
|
|
80
|
+
results
|
|
81
|
+
);
|
|
65
82
|
|
|
66
83
|
// Resolves the parameter paths for the transformation
|
|
67
84
|
const params = await Promise.all(
|
|
68
|
-
castArray(transformation.params).map((param) =>
|
|
69
|
-
this.#resolvePath(
|
|
70
|
-
|
|
85
|
+
castArray(transformation.params).map((param) => {
|
|
86
|
+
const { obj: paramObj, path: paramPath } = this.#resolvePath(
|
|
87
|
+
param,
|
|
88
|
+
results
|
|
89
|
+
);
|
|
90
|
+
return paramObj
|
|
91
|
+
? paramPath
|
|
92
|
+
? get(paramObj, paramPath)
|
|
93
|
+
: paramObj
|
|
94
|
+
: paramPath;
|
|
95
|
+
})
|
|
71
96
|
);
|
|
72
97
|
|
|
73
98
|
// Calls the specified method on the resolved object with the resolved parameters
|
|
74
|
-
const result = await
|
|
99
|
+
const result = await invoke(methodObj, methodPath, ...params);
|
|
75
100
|
|
|
76
101
|
// Stores the result of the transformation
|
|
77
|
-
results.
|
|
102
|
+
results.unshift(result);
|
|
78
103
|
}
|
|
79
104
|
|
|
80
105
|
// Sets the output at the specified path to the last result of the transformations & returns.
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
return lastResult;
|
|
106
|
+
set(output, path, results[0]);
|
|
107
|
+
return results[0];
|
|
84
108
|
}
|
|
85
109
|
|
|
86
110
|
// Checks if the current node is an object
|
|
@@ -114,11 +138,18 @@ class JsonMap {
|
|
|
114
138
|
}
|
|
115
139
|
|
|
116
140
|
/**
|
|
117
|
-
*
|
|
141
|
+
* @typedef {object} PathResolution
|
|
142
|
+
* @property {object} obj - The object to be used for the transformation.
|
|
143
|
+
* @property {string} path - The path to the value to be used for the transformation.
|
|
144
|
+
* @private
|
|
145
|
+
*/
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Resolves the method/params path for a transformation
|
|
118
149
|
*
|
|
119
150
|
* @param {string} path - The path to be resolved.
|
|
120
151
|
* @param {Array} results - The results from previous transformations.
|
|
121
|
-
* @return {
|
|
152
|
+
* @return {PathResolution} - The resolved path.
|
|
122
153
|
* @private
|
|
123
154
|
*/
|
|
124
155
|
#resolvePath(path, results) {
|
|
@@ -127,43 +158,26 @@ class JsonMap {
|
|
|
127
158
|
return path;
|
|
128
159
|
}
|
|
129
160
|
|
|
130
|
-
// Defines special patterns and their corresponding
|
|
131
|
-
const
|
|
132
|
-
'
|
|
133
|
-
|
|
134
|
-
|
|
161
|
+
// Defines special patterns and their corresponding resolution functions
|
|
162
|
+
const patterns = {
|
|
163
|
+
'^\\$\\.(?<obj>lib|input|output)\\.?(?<path>.*)': ({ obj, path }) => ({
|
|
164
|
+
obj: this[obj],
|
|
165
|
+
path,
|
|
166
|
+
}),
|
|
167
|
+
'^\\$(?<path>\\[\\d+\\].*)': ({ path }) => ({
|
|
168
|
+
obj: results,
|
|
169
|
+
path,
|
|
170
|
+
}),
|
|
135
171
|
};
|
|
136
172
|
|
|
137
|
-
// Checks if the path matches the pattern for accessing previous results
|
|
138
|
-
const match = path.match(/^\$\[(\d+)\](.*)$/);
|
|
139
|
-
|
|
140
|
-
// Retrieves the value from the previous results based on the index and any remaining path
|
|
141
|
-
if (match) {
|
|
142
|
-
const [, index, rest] = match;
|
|
143
|
-
const value = get(results, [
|
|
144
|
-
results.length - 1 - parseInt(index),
|
|
145
|
-
...(rest.length ? rest.split('.') : []),
|
|
146
|
-
]);
|
|
147
|
-
|
|
148
|
-
// Returns the value if it exists, otherwise returns the original path
|
|
149
|
-
return value != null ? value : path;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
173
|
// Iterates over the special patterns
|
|
153
|
-
for (const [pattern,
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
// Retrieves the value from the replacement object based on the remaining path
|
|
159
|
-
const value = p.length ? get(replacement, p) : replacement;
|
|
160
|
-
|
|
161
|
-
// Returns the value if it exists, otherwise returns the original path
|
|
162
|
-
return isNil(value) ? path : value;
|
|
163
|
-
}
|
|
174
|
+
for (const [pattern, resolve] of Object.entries(patterns)) {
|
|
175
|
+
const match = path.match(pattern);
|
|
176
|
+
if (match) return resolve(match.groups);
|
|
177
|
+
}
|
|
164
178
|
|
|
165
179
|
// Returns the path as is if it does not match any special patterns
|
|
166
|
-
return path;
|
|
180
|
+
return { path };
|
|
167
181
|
}
|
|
168
182
|
}
|
|
169
183
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@karmaniverous/jsonmap",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -23,36 +23,39 @@
|
|
|
23
23
|
"license": "BSD-3-Clause",
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"lodash.castarray": "^4.4.0",
|
|
26
|
+
"lodash.clonedeepwith": "^4.5.0",
|
|
26
27
|
"lodash.get": "^4.4.2",
|
|
28
|
+
"lodash.invoke": "^4.5.2",
|
|
27
29
|
"lodash.isarray": "^4.0.0",
|
|
28
|
-
"lodash.isnil": "^4.0.0",
|
|
29
30
|
"lodash.isobject": "^3.0.2",
|
|
31
|
+
"lodash.isplainobject": "^4.0.6",
|
|
30
32
|
"lodash.isstring": "^4.0.1",
|
|
31
|
-
"lodash.
|
|
32
|
-
"lodash.
|
|
33
|
-
"lodash.set": "^4.3.2"
|
|
33
|
+
"lodash.mapvalues": "^4.6.0",
|
|
34
|
+
"lodash.pickby": "^4.6.0",
|
|
35
|
+
"lodash.set": "^4.3.2",
|
|
36
|
+
"lodash.size": "^4.2.0"
|
|
34
37
|
},
|
|
35
38
|
"devDependencies": {
|
|
36
|
-
"@babel/cli": "^7.21.
|
|
37
|
-
"@babel/core": "^7.
|
|
38
|
-
"@babel/eslint-parser": "^7.21.
|
|
39
|
+
"@babel/cli": "^7.21.5",
|
|
40
|
+
"@babel/core": "^7.22.1",
|
|
41
|
+
"@babel/eslint-parser": "^7.21.8",
|
|
39
42
|
"@babel/plugin-syntax-import-assertions": "^7.20.0",
|
|
40
|
-
"@babel/preset-env": "^7.
|
|
43
|
+
"@babel/preset-env": "^7.22.4",
|
|
41
44
|
"@babel/register": "^7.21.0",
|
|
42
|
-
"@karmaniverous/get-dotenv": "^
|
|
43
|
-
"@types/node": "^
|
|
45
|
+
"@karmaniverous/get-dotenv": "^2.4.1",
|
|
46
|
+
"@types/node": "^20.2.5",
|
|
44
47
|
"chai": "^4.3.7",
|
|
45
48
|
"concat-md": "^0.5.1",
|
|
46
|
-
"eslint": "^8.
|
|
47
|
-
"eslint-config-standard": "^17.
|
|
48
|
-
"eslint-plugin-jsdoc": "^
|
|
49
|
+
"eslint": "^8.41.0",
|
|
50
|
+
"eslint-config-standard": "^17.1.0",
|
|
51
|
+
"eslint-plugin-jsdoc": "^46.1.0",
|
|
49
52
|
"eslint-plugin-mocha": "^10.1.0",
|
|
50
53
|
"jsdoc-to-markdown": "^8.0.0",
|
|
51
54
|
"lodash": "^4.17.21",
|
|
52
55
|
"mocha": "^10.2.0",
|
|
53
56
|
"numeral": "^2.0.6",
|
|
54
|
-
"prettier": "^2.8.
|
|
55
|
-
"release-it": "^15.
|
|
57
|
+
"prettier": "^2.8.8",
|
|
58
|
+
"release-it": "^15.10.3"
|
|
56
59
|
},
|
|
57
60
|
"exports": {
|
|
58
61
|
".": {
|
|
@@ -84,8 +87,8 @@
|
|
|
84
87
|
"build": "babel lib -d dist/default/lib --delete-dir-on-start --config-file ./dist/default/.babelrc",
|
|
85
88
|
"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",
|
|
86
89
|
"lint": "eslint lib/**",
|
|
87
|
-
"
|
|
88
|
-
"release": "
|
|
90
|
+
"prerelease": "npm run lint && npm run test && npm run build && npm run doc",
|
|
91
|
+
"release": "release-it",
|
|
89
92
|
"test": "getdotenv -c \"mocha\" -p ./ ./env -d dev -y ./env/dynamic.js"
|
|
90
93
|
},
|
|
91
94
|
"type": "module"
|