Skip to content

Commit 674f8d1

Browse files
committed
Docs: Update readme, keywords, and description
1 parent 187b2fd commit 674f8d1

2 files changed

Lines changed: 61 additions & 8 deletions

File tree

README.md

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,24 @@ Declarative Mapper for Node.js
44
[![Version](https://img.shields.io/npm/v/declarative-mapper.svg)](https://www.npmjs.com/package/declarative-mapper)
55
[![Coverage](https://coveralls.io/repos/github/snatalenko/declarative-mapper/badge.svg?branch=master)](https://coveralls.io/github/snatalenko/declarative-mapper?branch=master)
66
[![Downloads](https://img.shields.io/npm/dm/declarative-mapper.svg)](https://www.npmjs.com/package/declarative-mapper)
7-
[![License](https://img.shields.io/github/license/snatalenko/declarative-mapper.svg)](https://github.com/snatalenko/declarative-mapper)
7+
[![License](https://img.shields.io/github/license/snatalenko/declarative-mapper.svg?v=1.7.1)](https://github.com/snatalenko/declarative-mapper)
88
[![Tests/Audit](https://github.com/snatalenko/declarative-mapper/actions/workflows/ci.yml/badge.svg)](https://github.com/snatalenko/declarative-mapper/actions)
99

10+
## Table of Contents
11+
12+
- [Reasoning](#reasoning)
13+
- [Quick Start Example](#quick-start-example)
14+
- [Mapping Instructions](#mapping-instructions)
15+
- [Runtime Variables Quick Reference](#runtime-variables-quick-reference)
16+
- [Objects](#objects)
17+
- [Arrays](#arrays)
18+
- [String\[\] from Object\[\]](#string-from-object)
19+
- [String\[\] from String\[\]](#string-from-string)
20+
- [Tuple Arrays](#tuple-arrays)
21+
- [Runtime Variables ($input, $record, $index, $collection)](#runtime-variables-input-record-index-collection)
22+
- [Dynamic Output Keys](#dynamic-output-keys)
23+
- [Complex Mapping Example](#complex-mapping-example)
24+
1025
## Reasoning
1126

1227
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:
2136
- **Lightweight** - no dependencies
2237

2338

24-
A simple example:
39+
### Quick Start Example
2540

2641
```ts
2742
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
90105
}
91106
```
92107

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+
93117
### Objects
94118

95119
Mapping of an object with inner properties:
@@ -150,7 +174,10 @@ Result:
150174
}]
151175
```
152176

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).
154181

155182

156183
#### String[] from Object[]
@@ -200,7 +227,7 @@ Result:
200227
[2, 4, 6]
201228
```
202229

203-
### Array with predefined set of elements
230+
### Tuple Arrays
204231

205232
If the array should have a predefined set of elements, each element can be mapped by index:
206233

@@ -255,13 +282,28 @@ Or up in the source document:
255282
}
256283
```
257284

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:
259288

260289
- `$record` - current element of the array being iterated with `forEach`
261290
- `$index` - index of the current array element
262291
- `$collection` - entire collection of the elements being iterated
263292
- `$input` - entire document passed as mapping input
264293

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+
265307
### Dynamic Output Keys
266308

267309
You can build output property names dynamically with template-based keys on the left side.

package.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
{
22
"name": "declarative-mapper",
33
"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.",
55
"keywords": [
6-
"declarative",
7-
"mapping"
6+
"json-mapper",
7+
"object-mapper",
8+
"data-mapper",
9+
"declarative-mapping",
10+
"data-transformation",
11+
"object-transformation",
12+
"schema-mapping",
13+
"json-schema-mapping",
14+
"json-to-json",
15+
"config-driven",
16+
"etl",
17+
"nodejs",
18+
"typescript"
819
],
920
"type": "module",
1021
"main": "dist/cjs/index.js",

0 commit comments

Comments
 (0)