@karmaniverous/jsonmap 0.0.4 → 0.0.5
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.
|
@@ -4,15 +4,15 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.JsonMap = void 0;
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
var
|
|
10
|
-
var
|
|
11
|
-
var
|
|
12
|
-
var
|
|
13
|
-
var
|
|
14
|
-
var
|
|
15
|
-
var
|
|
7
|
+
var _lodash = _interopRequireDefault(require("lodash.castarray"));
|
|
8
|
+
var _lodash2 = _interopRequireDefault(require("lodash.get"));
|
|
9
|
+
var _lodash3 = _interopRequireDefault(require("lodash.isnil"));
|
|
10
|
+
var _lodash4 = _interopRequireDefault(require("lodash.isarray"));
|
|
11
|
+
var _lodash5 = _interopRequireDefault(require("lodash.isobject"));
|
|
12
|
+
var _lodash6 = _interopRequireDefault(require("lodash.isstring"));
|
|
13
|
+
var _lodash7 = _interopRequireDefault(require("lodash.last"));
|
|
14
|
+
var _lodash8 = _interopRequireDefault(require("lodash.parseint"));
|
|
15
|
+
var _lodash9 = _interopRequireDefault(require("lodash.set"));
|
|
16
16
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
17
|
function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); }
|
|
18
18
|
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
|
|
@@ -68,9 +68,9 @@ exports.JsonMap = JsonMap;
|
|
|
68
68
|
async function _transform2(node, input, output) {
|
|
69
69
|
let path = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
|
|
70
70
|
// Checks if the current node is an object and has a '$' key
|
|
71
|
-
if ((0,
|
|
71
|
+
if ((0, _lodash5.default)(node) && '$' in node) {
|
|
72
72
|
// Retrieves the transformations to be applied (can be an array or a single object)
|
|
73
|
-
const transformations = (0,
|
|
73
|
+
const transformations = (0, _lodash4.default)(node['$']) ? node['$'] : [node['$']];
|
|
74
74
|
|
|
75
75
|
// Array to store the results of the transformations
|
|
76
76
|
let results = [];
|
|
@@ -81,7 +81,7 @@ async function _transform2(node, input, output) {
|
|
|
81
81
|
const object = _classPrivateMethodGet(this, _resolvePath, _resolvePath2).call(this, transformation.object, results);
|
|
82
82
|
|
|
83
83
|
// Resolves the parameter paths for the transformation
|
|
84
|
-
const params = await Promise.all((0,
|
|
84
|
+
const params = await Promise.all((0, _lodash.default)(transformation.params).map(param => _classPrivateMethodGet(this, _resolvePath, _resolvePath2).call(this, param, results)));
|
|
85
85
|
|
|
86
86
|
// Calls the specified method on the resolved object with the resolved parameters
|
|
87
87
|
const result = await object[transformation.method](...params);
|
|
@@ -91,15 +91,15 @@ async function _transform2(node, input, output) {
|
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
// Sets the output at the specified path to the last result of the transformations & returns.
|
|
94
|
-
const lastResult = (0,
|
|
95
|
-
(0,
|
|
94
|
+
const lastResult = (0, _lodash7.default)(results);
|
|
95
|
+
(0, _lodash9.default)(output, path, lastResult);
|
|
96
96
|
return lastResult;
|
|
97
97
|
}
|
|
98
98
|
|
|
99
99
|
// Checks if the current node is an object
|
|
100
|
-
if ((0,
|
|
100
|
+
if ((0, _lodash5.default)(node)) {
|
|
101
101
|
// Creates an empty array or object based on whether the current node is an array or not
|
|
102
|
-
const transformedNode = (0,
|
|
102
|
+
const transformedNode = (0, _lodash4.default)(node) ? [] : {};
|
|
103
103
|
|
|
104
104
|
// Iterates over each key-value pair in the current node
|
|
105
105
|
for (const [key, value] of Object.entries(node)) {
|
|
@@ -112,17 +112,17 @@ async function _transform2(node, input, output) {
|
|
|
112
112
|
}
|
|
113
113
|
|
|
114
114
|
// Sets the output at the specified path to the transformedNode & returnsd.
|
|
115
|
-
(0,
|
|
115
|
+
(0, _lodash9.default)(output, path, transformedNode);
|
|
116
116
|
return transformedNode;
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
// Sets the output at the specified path to the current node & returns.
|
|
120
|
-
(0,
|
|
120
|
+
(0, _lodash9.default)(output, path, node);
|
|
121
121
|
return node;
|
|
122
122
|
}
|
|
123
123
|
function _resolvePath2(path, results) {
|
|
124
124
|
// If the path is not a string, return it as is
|
|
125
|
-
if (!(0,
|
|
125
|
+
if (!(0, _lodash6.default)(path)) {
|
|
126
126
|
return path;
|
|
127
127
|
}
|
|
128
128
|
|
|
@@ -139,7 +139,7 @@ function _resolvePath2(path, results) {
|
|
|
139
139
|
// Retrieves the value from the previous results based on the index and any remaining path
|
|
140
140
|
if (match) {
|
|
141
141
|
const [, index, rest] = match;
|
|
142
|
-
const value = (0,
|
|
142
|
+
const value = (0, _lodash2.default)(results, [results.length - 1 - (0, _lodash8.default)(index), ...(rest.length ? rest.split('.') : [])]);
|
|
143
143
|
|
|
144
144
|
// Returns the value if it exists, otherwise returns the original path
|
|
145
145
|
return value != null ? value : path;
|
|
@@ -151,10 +151,10 @@ function _resolvePath2(path, results) {
|
|
|
151
151
|
const p = path.slice(pattern.length + 1);
|
|
152
152
|
|
|
153
153
|
// Retrieves the value from the replacement object based on the remaining path
|
|
154
|
-
const value = p.length ? (0,
|
|
154
|
+
const value = p.length ? (0, _lodash2.default)(replacement, p) : replacement;
|
|
155
155
|
|
|
156
156
|
// Returns the value if it exists, otherwise returns the original path
|
|
157
|
-
return (0,
|
|
157
|
+
return (0, _lodash3.default)(value) ? path : value;
|
|
158
158
|
}
|
|
159
159
|
|
|
160
160
|
// Returns the path as is if it does not match any special patterns
|
package/lib/JsonMap/JsonMap.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
// npm imports
|
|
2
|
-
import castArray from 'lodash
|
|
3
|
-
import get from 'lodash
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import isObject from 'lodash
|
|
7
|
-
import isString from 'lodash
|
|
8
|
-
import last from 'lodash
|
|
9
|
-
import parseInt from 'lodash
|
|
10
|
-
import set from 'lodash
|
|
2
|
+
import castArray from 'lodash.castarray';
|
|
3
|
+
import get from 'lodash.get';
|
|
4
|
+
import isNil from 'lodash.isnil';
|
|
5
|
+
import isArray from 'lodash.isarray';
|
|
6
|
+
import isObject from 'lodash.isobject';
|
|
7
|
+
import isString from 'lodash.isstring';
|
|
8
|
+
import last from 'lodash.last';
|
|
9
|
+
import parseInt from 'lodash.parseint';
|
|
10
|
+
import set from 'lodash.set';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* JsonMap class to apply transformations to a JSON object
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@karmaniverous/jsonmap",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -22,7 +22,15 @@
|
|
|
22
22
|
],
|
|
23
23
|
"license": "BSD-3-Clause",
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"lodash": "^4.
|
|
25
|
+
"lodash.castarray": "^4.4.0",
|
|
26
|
+
"lodash.get": "^4.4.2",
|
|
27
|
+
"lodash.isarray": "^4.0.0",
|
|
28
|
+
"lodash.isnil": "^4.0.0",
|
|
29
|
+
"lodash.isobject": "^3.0.2",
|
|
30
|
+
"lodash.isstring": "^4.0.1",
|
|
31
|
+
"lodash.last": "^3.0.0",
|
|
32
|
+
"lodash.parseint": "^4.0.2",
|
|
33
|
+
"lodash.set": "^4.3.2"
|
|
26
34
|
},
|
|
27
35
|
"devDependencies": {
|
|
28
36
|
"@babel/cli": "^7.21.0",
|
|
@@ -33,7 +41,6 @@
|
|
|
33
41
|
"@babel/register": "^7.21.0",
|
|
34
42
|
"@karmaniverous/get-dotenv": "^1.0.0",
|
|
35
43
|
"@types/node": "^18.15.5",
|
|
36
|
-
"babel-plugin-lodash": "^3.3.4",
|
|
37
44
|
"chai": "^4.3.7",
|
|
38
45
|
"concat-md": "^0.5.1",
|
|
39
46
|
"eslint": "^8.36.0",
|
|
@@ -41,6 +48,7 @@
|
|
|
41
48
|
"eslint-plugin-jsdoc": "^40.1.0",
|
|
42
49
|
"eslint-plugin-mocha": "^10.1.0",
|
|
43
50
|
"jsdoc-to-markdown": "^8.0.0",
|
|
51
|
+
"lodash": "^4.17.21",
|
|
44
52
|
"mocha": "^10.2.0",
|
|
45
53
|
"numeral": "^2.0.6",
|
|
46
54
|
"prettier": "^2.8.5",
|