@karmaniverous/jsonmap 0.0.5 → 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.
Files changed (3) hide show
  1. package/README.md +2 -1
  2. package/package.json +1 -1
  3. 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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@karmaniverous/jsonmap",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
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.