data-pattern
Format data in the specific pattern we wanted.
why use data-pattern?
The data we fetch from the server may not be what we expected. We need a simple way to unify the data format.
const dataPattern = ; ajax ;
Installation
npm install data-pattern
Usage
Expect data to be array:
;
Expect data to be object:
;
Format every item of array:
;;
Format value of object:
;
Pattern can be a function which accepts current data as first arguments:
;
Format function for array items:
;
Below is equal:
; // is equal to ;
Use strict mod to filter properties:
let data = name: 'lxjwlt' map: {}; dataPattern; /*{ name: 'lxjwlt', list: []}*/
A quick example:
const dataPattern = ; let data = children: null {} map: shouldBeKeep: 0 timestamp: 1487504955 // in seconds; let pattern = children: info: {} map: arr: { return timestamp * 1000; // in microseconds }; ; /*{ children: [ { info: {} }, { info: {} } ], map: { shouldBeKeep: 0, arr: [] }, timestamp: 1487504955000}*/