@karmaniverous/jsonmap 0.0.5 → 0.0.7
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 +8 -7
- package/dist/default/lib/JsonMap/JsonMap.js +6 -4
- package/lib/JsonMap/JsonMap.js +4 -4
- package/package.json +1 -1
- package/prompt.md +0 -227
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
|
|
@@ -102,8 +103,8 @@ Once a `JsonMap` instance is configured, it can be executed against any input. C
|
|
|
102
103
|
```js
|
|
103
104
|
import { JsonMap } from '@karmaniverous/jsonmap';
|
|
104
105
|
|
|
105
|
-
// Assumes
|
|
106
|
-
const jsonMap = new JsonMap(
|
|
106
|
+
// Assumes map & lib are already defined as above.
|
|
107
|
+
const jsonMap = new JsonMap(map, lib);
|
|
107
108
|
|
|
108
109
|
// Assumes some input data object is already defined.
|
|
109
110
|
const output = await jsonMap.transform(input);
|
|
@@ -121,19 +122,19 @@ JsonMap class to apply transformations to a JSON object
|
|
|
121
122
|
**Kind**: global class
|
|
122
123
|
|
|
123
124
|
* [JsonMap](#JsonMap)
|
|
124
|
-
* [new JsonMap(
|
|
125
|
+
* [new JsonMap([map], [lib])](#new_JsonMap_new)
|
|
125
126
|
* [.transform(input)](#JsonMap+transform) ⇒ <code>object</code>
|
|
126
127
|
|
|
127
128
|
<a name="new_JsonMap_new"></a>
|
|
128
129
|
|
|
129
|
-
### new JsonMap(
|
|
130
|
+
### new JsonMap([map], [lib])
|
|
130
131
|
Creates an instance of JsonMap.
|
|
131
132
|
|
|
132
133
|
|
|
133
134
|
| Param | Type | Description |
|
|
134
135
|
| --- | --- | --- |
|
|
135
|
-
|
|
|
136
|
-
|
|
|
136
|
+
| [map] | <code>object</code> | The data mapping configuration. |
|
|
137
|
+
| [lib] | <code>object</code> | A collection of function libraries. |
|
|
137
138
|
|
|
138
139
|
<a name="JsonMap+transform"></a>
|
|
139
140
|
|
|
@@ -26,14 +26,16 @@ class JsonMap {
|
|
|
26
26
|
/**
|
|
27
27
|
* Creates an instance of JsonMap.
|
|
28
28
|
*
|
|
29
|
-
* @param {object}
|
|
30
|
-
* @param {object}
|
|
29
|
+
* @param {object} [map] - The data mapping configuration.
|
|
30
|
+
* @param {object} [lib] - A collection of function libraries.
|
|
31
31
|
*/
|
|
32
|
-
constructor(
|
|
32
|
+
constructor() {
|
|
33
|
+
let map = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
34
|
+
let lib = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
33
35
|
_classPrivateMethodInitSpec(this, _resolvePath);
|
|
34
36
|
_classPrivateMethodInitSpec(this, _transform);
|
|
35
|
-
this.lib = lib;
|
|
36
37
|
this.map = map;
|
|
38
|
+
this.lib = lib;
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
/**
|
package/lib/JsonMap/JsonMap.js
CHANGED
|
@@ -16,12 +16,12 @@ class JsonMap {
|
|
|
16
16
|
/**
|
|
17
17
|
* Creates an instance of JsonMap.
|
|
18
18
|
*
|
|
19
|
-
* @param {object}
|
|
20
|
-
* @param {object}
|
|
19
|
+
* @param {object} [map] - The data mapping configuration.
|
|
20
|
+
* @param {object} [lib] - A collection of function libraries.
|
|
21
21
|
*/
|
|
22
|
-
constructor(
|
|
23
|
-
this.lib = lib;
|
|
22
|
+
constructor(map = {}, lib = {}) {
|
|
24
23
|
this.map = map;
|
|
24
|
+
this.lib = lib;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
/**
|
package/package.json
CHANGED
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.
|