You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
On several projects, I needed a library that could convert one JSON format to another (for example, an invoice from one system into another). It had to support **declarative mapping** instructions so users could configure mappings from a UI. It also had to be **flexible** enough for complex requirements, **secure** against JS injection, and **fast** enough to process streams with millions of records.
@@ -21,7 +36,7 @@ That is where Declarative Mapper came in:
21
36
-**Lightweight** - no dependencies
22
37
23
38
24
-
A simple example:
39
+
### Quick Start Example
25
40
26
41
```ts
27
42
import { createMapper } from'declarative-mapper';
@@ -90,6 +105,15 @@ The right side is either a string with a valid JS expression or an object with m
90
105
}
91
106
```
92
107
108
+
### Runtime Variables Quick Reference
109
+
110
+
| Variable | Description | Available in |
111
+
| --- | --- | --- |
112
+
|`$input`| Entire source document passed to the mapper. | All mapping contexts |
113
+
|`$record`| Current element in a `forEach` iteration. |`forEach` → `map`|
114
+
|`$index`| Current element index in a `forEach` iteration. |`forEach` → `map`|
115
+
|`$collection`| Entire source array selected by `forEach`. |`forEach` → `map`|
116
+
93
117
### Objects
94
118
95
119
Mapping of an object with inner properties:
@@ -150,7 +174,10 @@ Result:
150
174
}]
151
175
```
152
176
153
-
In this mapping, the execution context shifts to each input object, so inner properties can be referenced directly as `arrayInnerProperty` instead of `inputArray[index].arrayInnerProperty`. More on that in [Context Switching](#context-switching).
177
+
In this mapping, the execution context shifts to each input object, so inner properties can be referenced directly as `arrayInnerProperty` instead of `inputArray[index].arrayInnerProperty`.
178
+
179
+
Inside `forEach` mappings, `$record`, `$index`, `$collection`, and `$input` are also available.
180
+
More on that in [Runtime Variables](#context-switching).
154
181
155
182
156
183
#### String[] from Object[]
@@ -200,7 +227,7 @@ Result:
200
227
[2, 4, 6]
201
228
```
202
229
203
-
### Array with predefined set of elements
230
+
### Tuple Arrays
204
231
205
232
If the array should have a predefined set of elements, each element can be mapped by index:
206
233
@@ -255,13 +282,28 @@ Or up in the source document:
255
282
}
256
283
```
257
284
258
-
These special variables are useful for context switching:
285
+
Inside `from` mappings, you can still reference root-level fields through `$input`.
286
+
287
+
Runtime variables:
259
288
260
289
-`$record` - current element of the array being iterated with `forEach`
261
290
-`$index` - index of the current array element
262
291
-`$collection` - entire collection of the elements being iterated
263
292
-`$input` - entire document passed as mapping input
264
293
294
+
Combined example (`forEach` + root reference):
295
+
296
+
```json
297
+
{
298
+
"forEach": "LINE_ITEMS",
299
+
"map": {
300
+
"lineNo": "$index + 1",
301
+
"sourceId": "$input.id",
302
+
"raw": "$record"
303
+
}
304
+
}
305
+
```
306
+
265
307
### Dynamic Output Keys
266
308
267
309
You can build output property names dynamically with template-based keys on the left side.
Copy file name to clipboardExpand all lines: package.json
+14-3Lines changed: 14 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,21 @@
1
1
{
2
2
"name": "declarative-mapper",
3
3
"version": "1.7.0",
4
-
"description": "Declarative object mapper for NodeJS",
4
+
"description": "Declarative JSON and object mapper for Node.js/TypeScript: transform data with mapping templates, schema-based helpers, and safe VM execution.",
0 commit comments