File tree Expand file tree Collapse file tree 1 file changed +63
-0
lines changed
Expand file tree Collapse file tree 1 file changed +63
-0
lines changed Original file line number Diff line number Diff 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+
278340Enjoy!
279341
280342## Changelog
343+ 1.0.15 Add support for a context object that is passed through to the operate.run and each functions.
2813441.0.14 Add support for default values via "defaults" definition. Add support for removing attributes via the "remove" definition.
2823451.0.13 Update code examples.
2833461.0.12 Fixed readme formatting.
You can’t perform that action at this time.
0 commit comments