@karmaniverous/jsonmap 0.0.4 → 0.0.6

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 CHANGED
@@ -48,7 +48,8 @@ const map = {
48
48
  static: 'another static value',
49
49
  // Value defined by a mapping rule expressing an array of transformation
50
50
  // objects. If there is only a single transformation object, no array is
51
- // necessary.
51
+ // necessary. The output of the last transformation step is returned as
52
+ // the mapped value.
52
53
  dynamic: {
53
54
  $: [
54
55
  // Each transformation object uses a special syntax to reference an
@@ -4,15 +4,15 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.JsonMap = void 0;
7
- var _castArray = _interopRequireDefault(require("lodash/castArray.js"));
8
- var _get = _interopRequireDefault(require("lodash/get.js"));
9
- var _isArray = _interopRequireDefault(require("lodash/isArray.js"));
10
- var _isNil = _interopRequireDefault(require("lodash/isNil.js"));
11
- var _isObject = _interopRequireDefault(require("lodash/isObject.js"));
12
- var _isString = _interopRequireDefault(require("lodash/isString.js"));
13
- var _last = _interopRequireDefault(require("lodash/last.js"));
14
- var _parseInt = _interopRequireDefault(require("lodash/parseInt.js"));
15
- var _set = _interopRequireDefault(require("lodash/set.js"));
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, _isObject.default)(node) && '$' in node) {
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, _isArray.default)(node['$']) ? node['$'] : [node['$']];
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, _castArray.default)(transformation.params).map(param => _classPrivateMethodGet(this, _resolvePath, _resolvePath2).call(this, param, results)));
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, _last.default)(results);
95
- (0, _set.default)(output, path, lastResult);
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, _isObject.default)(node)) {
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, _isArray.default)(node) ? [] : {};
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, _set.default)(output, path, transformedNode);
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, _set.default)(output, path, node);
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, _isString.default)(path)) {
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, _get.default)(results, [results.length - 1 - (0, _parseInt.default)(index), ...(rest.length ? rest.split('.') : [])]);
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, _get.default)(replacement, p) : replacement;
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, _isNil.default)(value) ? path : value;
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
@@ -1,13 +1,13 @@
1
1
  // npm imports
2
- import castArray from 'lodash/castArray.js';
3
- import get from 'lodash/get.js';
4
- import isArray from 'lodash/isArray.js';
5
- import isNil from 'lodash/isNil.js';
6
- import isObject from 'lodash/isObject.js';
7
- import isString from 'lodash/isString.js';
8
- import last from 'lodash/last.js';
9
- import parseInt from 'lodash/parseInt.js';
10
- import set from 'lodash/set.js';
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.4",
3
+ "version": "0.0.6",
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.17.21"
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",
package/prompt.md DELETED
@@ -1,227 +0,0 @@
1
- Here is a usage example of a Javascript class called JsonMap that applies transformations to a JSON object. The class should exploit the lodash library where relevant.
2
-
3
- ```js
4
- import _ from 'lodash';
5
- import numeral from 'numeral';
6
- import { JsonMap } from 'json-map';
7
-
8
- const fetchData = async (entityToken, entityId) => {
9
- // This async code fetches data from an API.
10
- };
11
-
12
- // This object is a collection of function libraries.
13
- const lib = { _, fetchData, numeral, String };
14
-
15
- // This is the data mapping configuration.
16
- // The output will have the same structure as this object.
17
- const map = {
18
- txn: {
19
- txnId: {
20
- /*
21
- An object value with a single key of $ indicates a transformation.
22
-
23
- A transformation is composed of steps. Each step is an object with 3 keys: object, method, and params. Each transformation step runs a method of an object, optionally passing it a parameter array.
24
-
25
- If the $ value is an object, there is a single transformation step. Multiple steps can be collected into an array.
26
- */
27
- $: {
28
- /*
29
- The object property itentifies the object whose method will be run. The object is resolved using a special syntax:
30
-
31
- $.lib refers to the JsonMap instance lib property.
32
-
33
- $.input refers to the input data object.
34
-
35
- $.output refers to the output object. The transformation is progressive, so the value currently being processed can refer to previously processed values.
36
-
37
- $[i], where i is an integer, refers to the ith previous transformation step, so $[0] is the last step, $[1] is the one before that, and so on. This syntax should account for the fact that a previous step might return an object, in which case $[i] might be followed by path information, e.g. $[0].path.
38
-
39
- Beyond these base objects, lodash-style path syntax applies. If none of the patterns above fits, then object should be treated as a string.
40
- */
41
- object: '$.lib._',
42
-
43
- /*
44
- The method property identifies the method that will be executed on object. It should be a string.
45
- */
46
- method: 'get',
47
-
48
- /*
49
- The params property is an array of values to be passed to method. These values should be resolved using the same syntax as object. If none of the special patterns fits, then the value should be treated as a string.
50
- */
51
- params: ['$.input', 'dynamodb.NewImage.txnId.S'],
52
- },
53
- },
54
- roundup: {
55
- $: [
56
- {
57
- object: '$.lib._',
58
- method: 'get',
59
- params: ['$.input', 'dynamodb.NewImage.roundup.N'],
60
- },
61
- {
62
- object: '$.lib',
63
- method: 'numeral',
64
- // If there is only a single param, no array is necessary.
65
- params: '$[0]',
66
- },
67
- {
68
- object: '$[0]',
69
- method: 'format',
70
- params: ['$0,0.00'],
71
- },
72
- ],
73
- },
74
- // Static values & structures will simply be passed through to the output.
75
- foo: 'bar',
76
- baz: {
77
- object: '$.lib.String',
78
- method: 'toUpperCase',
79
- params: '$.foo',
80
- },
81
- fetch: {
82
- object: '$.lib',
83
- // This is an async function that returns an object!
84
- method: 'fetchData',
85
- // The second parameter leverages a previously processed value.
86
- params: ['txn', '$.output.txn.txnId'],
87
- },
88
- },
89
- };
90
-
91
- // This is some sample input data to be transformed.
92
- const input = {
93
- eventID: 'e560bbcaee919e16a6a8ce8dc3fe97ab',
94
- eventName: 'MODIFY',
95
- eventVersion: '1.1',
96
- eventSource: 'aws:dynamodb',
97
- awsRegion: 'us-east-1',
98
- dynamodb: {
99
- ApproximateCreationDateTime: 1685071094,
100
- Keys: {
101
- entityPK: {
102
- S: 'txn!',
103
- },
104
- entitySK: {
105
- S: 'txnId#_tQ72mu5Iy2PYJ33c497g',
106
- },
107
- },
108
- NewImage: {
109
- txnUserIdPK: {
110
- S: 'txn!|userId#Xmv51c7lYiiTIU22_UlCD',
111
- },
112
- created: {
113
- N: '1685071087846',
114
- },
115
- netValue: {
116
- N: '365.3',
117
- },
118
- methodId: {
119
- S: 'KDvC6leEzkAAf8-akh_vO',
120
- },
121
- entityPK: {
122
- S: 'txn!',
123
- },
124
- userId: {
125
- S: 'Xmv51c7lYiiTIU22_UlCD',
126
- },
127
- entitySK: {
128
- S: 'txnId#_tQ72mu5Iy2PYJ33c497g',
129
- },
130
- updater: {
131
- S: 'api-txn-v0-bali',
132
- },
133
- merchantId: {
134
- S: 'eQMZ2ikPmUd3cqrptbpna',
135
- },
136
- roundup: {
137
- N: '0.7',
138
- },
139
- txnMerchantIdPK: {
140
- S: 'txn!|merchantId#eQMZ2ikPmUd3cqrptbpna',
141
- },
142
- updated: {
143
- N: '1685071094685',
144
- },
145
- txnId: {
146
- S: '_tQ72mu5Iy2PYJ33c497g',
147
- },
148
- txnMethodIdPK: {
149
- S: 'txn!|methodId#KDvC6leEzkAAf8-akh_vO',
150
- },
151
- },
152
- OldImage: {
153
- txnUserIdPK: {
154
- S: 'txn!|userId#Xmv51c7lYiiTIU22_UlCD',
155
- },
156
- created: {
157
- N: '1685071087846',
158
- },
159
- netValue: {
160
- N: '429.74',
161
- },
162
- methodId: {
163
- S: 'KDvC6leEzkAAf8-akh_vO',
164
- },
165
- entityPK: {
166
- S: 'txn!',
167
- },
168
- userId: {
169
- S: 'Xmv51c7lYiiTIU22_UlCD',
170
- },
171
- entitySK: {
172
- S: 'txnId#_tQ72mu5Iy2PYJ33c497g',
173
- },
174
- updater: {
175
- S: 'api-txn-v0-bali',
176
- },
177
- merchantId: {
178
- S: 'eQMZ2ikPmUd3cqrptbpna',
179
- },
180
- roundup: {
181
- N: '0.26',
182
- },
183
- txnMerchantIdPK: {
184
- S: 'txn!|merchantId#eQMZ2ikPmUd3cqrptbpna',
185
- },
186
- updated: {
187
- N: '1685071087846',
188
- },
189
- txnId: {
190
- S: '_tQ72mu5Iy2PYJ33c497g',
191
- },
192
- txnMethodIdPK: {
193
- S: 'txn!|methodId#KDvC6leEzkAAf8-akh_vO',
194
- },
195
- },
196
- SequenceNumber: '31120900000000039175922358',
197
- SizeBytes: 801,
198
- StreamViewType: 'NEW_AND_OLD_IMAGES',
199
- },
200
- eventSourceARN:
201
- 'arn:aws:dynamodb:us-east-1:546652796775:table/api-txn-v0-bali/stream/2023-05-19T12:57:41.682',
202
- };
203
-
204
- // This initializes a JsonMap instance.
205
- const jsonMap = new JsonMap(lib, map);
206
-
207
- // This transforms the sample input data.
208
- const result = jsonMap.transform(input);
209
-
210
- // This is the result!
211
- // {
212
- // txn: {
213
- // txnId: '_tQ72mu5Iy2PYJ33c497g',
214
- // roundup: '$0.70',
215
- // },
216
- // foo: 'bar',
217
- // baz: 'BAR',
218
- // fetch: {
219
- // statusCode: 200,
220
- // message: 'asynchronously fetched data!'
221
- // }
222
- // }
223
- ```
224
-
225
- The class should accommodate deep placement of transformations within the map object. It should also recursively account for transformation objects that resolve into transformation objects.
226
-
227
- Fully implement the JsonMap class with jsdoc comments.