supermapper
Advanced tools
Comparing version 1.4.2 to 1.5.0
43
index.js
@@ -8,8 +8,7 @@ "use strict"; | ||
const writePointerData = (instructions, outData, params) => { | ||
const writePointerData = (instruction, outData, params) => { | ||
let cursor = outData; | ||
let compiled = instructions.outPath; | ||
let i = 0; | ||
while(i < compiled.length){ | ||
let obj = compiled[i]; | ||
while(i < instruction.outPath.length){ | ||
let obj = instruction.outPath[i]; | ||
let key = obj.key; | ||
@@ -25,18 +24,22 @@ | ||
if(i >= compiled.length - 1){ | ||
if(i >= instruction.outPath.length - 1){ | ||
let dataToWrite = params.data; | ||
if(instruction.each && Array.isArray(dataToWrite)){ | ||
dataToWrite = supermap(dataToWrite, instruction.each, instruction.eachOpts) | ||
} | ||
if(instructions.valueMap){ | ||
dataToWrite = mapValue(instructions.valueMap, dataToWrite); | ||
if(instruction.valueMap){ | ||
dataToWrite = mapValue(instruction.valueMap, dataToWrite); | ||
} | ||
cursor[key] = instructions.fn ? instructions.fn(instructions.in, dataToWrite, params.all) : dataToWrite; | ||
cursor[key] = instruction.fn ? instruction.fn(instruction.in, dataToWrite, params.all) : dataToWrite; | ||
if(!exists(cursor[key])){ | ||
cursor[key] = exists(instructions.default) ? instructions.default : null; | ||
cursor[key] = exists(instruction.default) ? instruction.default : null; | ||
} | ||
if(params.callback){ | ||
cursor[key] = params.callback(instructions.in, instructions.out, cursor[key]); | ||
cursor[key] = params.callback(instruction.in, instruction.out, cursor[key]); | ||
} | ||
@@ -46,3 +49,3 @@ break; | ||
let wantsArrayNext = (!isNaN(compiled[i + 1].key) || compiled[i + 1].key == "+"); | ||
let wantsArrayNext = (!isNaN(instruction.outPath[i + 1].key) || instruction.outPath[i + 1].key == "+"); | ||
@@ -92,13 +95,4 @@ if(!cursor.hasOwnProperty(key)){ | ||
const readInstructions = (mapping, opts) => { | ||
var instructionSet = new Supermapping(); | ||
for(let key in mapping){ | ||
let instructions = new Instructions(key, mapping[key], opts); | ||
instructionSet.push(instructions); | ||
for(let additionalKey of instructions.additional){ | ||
let copy = instructions.copyTo(additionalKey); | ||
instructionSet.push(copy); | ||
} | ||
} | ||
return instructionSet; | ||
const readInstructions = (mapping, opts) => { | ||
return new Supermapping(mapping, opts); | ||
}; | ||
@@ -115,4 +109,4 @@ | ||
for(let i in instructionSet){ | ||
let instructions = instructionSet[i]; | ||
outData = createMap(readPointerData(instructions.inPath, data), instructions, outData, data, opts); | ||
let instruction = instructionSet[i]; | ||
outData = createMap(readPointerData(instruction.inPath, data), instruction, outData, data, opts); | ||
} | ||
@@ -123,3 +117,2 @@ return outData; | ||
const supermap = (data, mapping, opts) => { | ||
//placeholder, TODO need flexible strategy for handling arrays / iteration | ||
if(!opts){ | ||
@@ -126,0 +119,0 @@ opts = {}; |
@@ -6,3 +6,3 @@ "use strict"; | ||
class Instructions{ | ||
class Instruction{ | ||
constructor(target, destination, opts = {}){ | ||
@@ -36,2 +36,10 @@ this.in = ""; | ||
} | ||
if(destination.each && typeof destination.each == "object" && destination.each.map){ | ||
this.each = new Supermapping(destination.each.map, destination.each.opts); | ||
this.eachOpts = destination.each.opts; | ||
} else { | ||
this.each = null; | ||
this.eachOpts = null; | ||
} | ||
} | ||
@@ -47,6 +55,7 @@ | ||
copyTo(destination){ | ||
let c = new Instructions(this.in, destination); | ||
let c = new Instruction(this.in, destination); | ||
c.fn = this.fn; | ||
c.default = this.default; | ||
c.valueMap = this.valueMap; | ||
c.each = this.each; | ||
return c; | ||
@@ -56,4 +65,16 @@ } | ||
class Supermapping extends Array {} | ||
class Supermapping extends Array { | ||
constructor(mapping, opts){ | ||
super(); | ||
for(const key in mapping){ | ||
let instruction = new Instruction(key, mapping[key], opts); | ||
this.push(instruction); | ||
for(let additionalKey of instruction.additional){ | ||
let copy = instruction.copyTo(additionalKey); | ||
this.push(copy); | ||
} | ||
} | ||
} | ||
} | ||
module.exports = { Instructions, Supermapping }; | ||
module.exports = { Instruction, Supermapping }; |
{ | ||
"name": "supermapper", | ||
"version": "1.4.2", | ||
"version": "1.5.0", | ||
"description": "map any object to any schema.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -297,4 +297,55 @@ # supermap | ||
## arrays | ||
## map each in an array | ||
the `each` parameter will iterate over objects in an array and map them | ||
```javascript | ||
let dataToMap = { | ||
company: { | ||
resources: [ | ||
{ | ||
firstName: "Greg", | ||
role: "Programmer" | ||
}, | ||
{ | ||
firstName: "Adam", | ||
role: "Admin" | ||
} | ||
] | ||
} | ||
} | ||
let keyMapping = { | ||
"company.resources" : { | ||
key: "people.employees", | ||
each: { | ||
map: { | ||
firstName: "name", | ||
role: "job" | ||
} | ||
} | ||
} | ||
} | ||
supermap(dataToMap, keyMapping); | ||
// result | ||
// { | ||
// "people": { | ||
// "employees": [ | ||
// { | ||
// "name": "Greg", | ||
// "job": "Programmer" | ||
// }, | ||
// { | ||
// "name": "Adam", | ||
// "job": "Admin" | ||
// } | ||
// ] | ||
// } | ||
// } | ||
``` | ||
## push to array | ||
passing a number says to treat the target like an array. passing a `+` symbol is like passing a number, but it will append to the array. | ||
@@ -301,0 +352,0 @@ |
26484
791
410