Skip to content

Commit b09bff0

Browse files
Add readme example for transform context
1 parent 091efaf commit b09bff0

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,72 @@ The expected output.
275275
]
276276
```
277277

278+
### Context Example
279+
280+
```javascript
281+
var DataTransform = require("node-json-transform").DataTransform;
282+
```
283+
284+
First we need some data.
285+
286+
```javascript
287+
var data = {
288+
posts : [
289+
{
290+
title : "title1",
291+
description: "description1"
292+
}
293+
]
294+
};
295+
```
296+
297+
The map defines how the output will be structured and which operations to run.
298+
299+
```javascript
300+
var map = {
301+
list : 'posts',
302+
item: {
303+
name: "title",
304+
info: "description"
305+
},
306+
operate: [
307+
{
308+
run: function(val, context) { return val + " more info for" + context.type},
309+
on: "info"
310+
}
311+
],
312+
each: function(item, index, collection, context){
313+
// make changes
314+
item.type = context.type;
315+
return item;
316+
}
317+
};
318+
```
319+
320+
Run it
321+
```javascript
322+
var dataTransform = DataTransform(data, map);
323+
var context = { type: 'my-type' };
324+
var result = dataTransform.transform(context);
325+
console.log(result);
326+
```
327+
328+
The expected output.
329+
```javascript
330+
[
331+
{
332+
name : "title1",
333+
info: "description1 more info for my-type",
334+
type: 'my-type'
335+
}
336+
]
337+
```
338+
339+
278340
Enjoy!
279341

280342
## Changelog
343+
1.0.15 Add support for a context object that is passed through to the operate.run and each functions.
281344
1.0.14 Add support for default values via "defaults" definition. Add support for removing attributes via the "remove" definition.
282345
1.0.13 Update code examples.
283346
1.0.12 Fixed readme formatting.

0 commit comments

Comments
 (0)