supermapper
Advanced tools
Comparing version 1.1.0 to 1.2.0
200
index.js
"use strict"; | ||
const path = require('path'); | ||
const splitPointer = require(path.normalize(__dirname + "/split.js")); | ||
const capture = require(path.normalize(__dirname + "/capture.js")); | ||
const exists = require(path.normalize(__dirname + "/exists.js")); | ||
var captureTokens = { | ||
"[": { | ||
start: "[", | ||
end: "]", | ||
run: function(key, data){ | ||
return key; | ||
} | ||
}, | ||
"%": { | ||
start: "%", | ||
end: "%", | ||
run: function(key, data){ | ||
return readPointerData(splitPointer(key), data); | ||
} | ||
} | ||
} | ||
class Supermapping extends Array {}; | ||
var createMap = (inData, instructions, outData, allData, opts) => { | ||
if(!opts.writeNull && !exists(instructions.default) && !exists(inData)){ | ||
return outData; | ||
} | ||
return writePointerData(instructions, outData, { | ||
data: inData, | ||
all: allData, | ||
}); | ||
} | ||
var readPointerData = (compiled, data) => { | ||
let i = 0; | ||
let cursor = null; | ||
while(i < compiled.length){ | ||
let obj = compiled[i]; | ||
let key = obj.key; | ||
if(obj.capture){ | ||
key = captureTokens[obj.capture].run(obj.key, data); | ||
} | ||
if(i == 0){ | ||
cursor = exists(data[key]) ? data[key] : null; | ||
} else { | ||
cursor = exists(cursor[key]) ? cursor[key] : null; | ||
} | ||
if(!cursor){ | ||
break; | ||
} | ||
i++; | ||
} | ||
return cursor; | ||
} | ||
var writePointerData = (instructions, outData, params) => { | ||
const writePointerData = (instructions, outData, params) => { | ||
let cursor = outData; | ||
@@ -62,3 +18,3 @@ let compiled = instructions.compiledOut; | ||
if(obj.capture){ | ||
key = captureTokens[obj.capture].run(key, params.all); | ||
key = capture.tokens[obj.capture].run(key, params.all); | ||
} | ||
@@ -90,73 +46,43 @@ | ||
var splitPointer = (pointer) => { | ||
let i = 0; | ||
let keys = []; | ||
let key = ""; | ||
while(i < pointer.length){ | ||
if(pointer[i] == "."){ | ||
if(key.length > 0){ | ||
keys.push(newKeyObject(key)); | ||
} | ||
key = ""; | ||
} else if(pointer[i] in captureTokens){ | ||
var capture = captureTokens[pointer[i]]; | ||
if(key.length > 0){ | ||
keys.push(newKeyObject(key)); | ||
} | ||
key = ""; | ||
i += 1; | ||
while(pointer[i] != capture.end){ | ||
key += pointer[i]; | ||
if(i >= pointer.length - 1){ | ||
break; | ||
} | ||
i += 1; | ||
} | ||
if(key.length > 0){ | ||
keys.push(newKeyObject(key, capture.start)); | ||
} | ||
key = ""; | ||
} else { | ||
key += pointer[i] | ||
} | ||
i += 1; | ||
const createMap = (inData, instructions, outData, allData, opts) => { | ||
if(!opts.writeNull && !exists(instructions.default) && !exists(inData)){ | ||
return outData; | ||
} | ||
if(key.length > 0){ | ||
keys.push(newKeyObject(key)); | ||
} | ||
return keys; | ||
} | ||
var newKeyObject = (key, capture) => { | ||
return { | ||
key: key, | ||
capture: capture | ||
} | ||
return writePointerData(instructions, outData, { | ||
data: inData, | ||
all: allData, | ||
}); | ||
} | ||
var mapObject = (data, mapping, opts) => { | ||
let outData = opts.base || {}; | ||
if(mapping instanceof Supermapping){ | ||
var instructionSet = mapping; | ||
} else { | ||
var instructionSet = readInstructions(mapping, opts); | ||
const readPointerData = (compiled, data) => { | ||
let i = 0; | ||
let cursor = null; | ||
while(i < compiled.length){ | ||
let obj = compiled[i]; | ||
let key = obj.key; | ||
if(obj.capture){ | ||
key = capture.tokens[obj.capture].run(obj.key, data); | ||
} | ||
if(i == 0){ | ||
cursor = exists(data[key]) ? data[key] : null; | ||
} else { | ||
cursor = exists(cursor[key]) ? cursor[key] : null; | ||
} | ||
if(!cursor){ | ||
break; | ||
} | ||
i++; | ||
} | ||
for(let i in instructionSet){ | ||
let instructions = instructionSet[i]; | ||
outData = createMap(readPointerData(instructions.compiledIn, data), instructions, outData, data, opts); | ||
} | ||
return outData; | ||
return cursor; | ||
} | ||
var exists = (item) => { | ||
if(item == null || item == undefined){ | ||
return false; | ||
} | ||
return true; | ||
} | ||
var readInstructions = (mapping, opts) => { | ||
const readInstructions = (mapping, opts) => { | ||
var instructionSet = new Supermapping(); | ||
for(let key in mapping){ | ||
let instructions = {}; | ||
let additionalKeys = []; | ||
if(typeof mapping[key] == "string"){ | ||
@@ -174,2 +100,6 @@ instructions.in = !opts.flip ? key : mapping[key]; | ||
if(mapping[key].additional){ | ||
additionalKeys = mapping[key].additional.filter(item => { return typeof item == "string" }); | ||
} | ||
} | ||
@@ -180,5 +110,13 @@ | ||
} | ||
instructions.compiledIn = splitPointer(instructions.in); | ||
instructions.compiledOut = splitPointer(instructions.out); | ||
instructions.compiledIn = splitPointer(instructions.in, capture.tokens); | ||
instructions.compiledOut = splitPointer(instructions.out, capture.tokens); | ||
instructionSet.push(instructions); | ||
if(additionalKeys.length > 0){ | ||
for(let i in additionalKeys){ | ||
let copy = Object.assign({}, instructions); | ||
copy.compiledOut = splitPointer(additionalKeys[i], capture.tokens); | ||
instructionSet.push(copy); | ||
} | ||
} | ||
} | ||
@@ -188,3 +126,17 @@ return instructionSet; | ||
var supermap = (data, mapping, opts) => { | ||
const mapObject = (data, mapping, opts) => { | ||
let outData = opts.base || {}; | ||
if(mapping instanceof Supermapping){ | ||
var instructionSet = mapping; | ||
} else { | ||
var instructionSet = readInstructions(mapping, opts); | ||
} | ||
for(let i in instructionSet){ | ||
let instructions = instructionSet[i]; | ||
outData = createMap(readPointerData(instructions.compiledIn, data), instructions, outData, data, opts); | ||
} | ||
return outData; | ||
} | ||
const supermap = (data, mapping, opts) => { | ||
//placeholder, TODO need flexible strategy for handling arrays / iteration | ||
@@ -206,10 +158,2 @@ if(!opts){ | ||
supermap.capture = function(start, end, cb){ | ||
captureTokens[start] = { | ||
start: start, | ||
end: end, | ||
run: cb | ||
} | ||
} | ||
supermap.compile = (mapping, compileOpts) => { | ||
@@ -230,6 +174,12 @@ if(!compileOpts){ | ||
class Supermapping extends Array { | ||
supermap.capture = capture.set; | ||
} | ||
supermap.capture("%", "%", function(key, data){ | ||
return readPointerData(splitPointer(key, capture.tokens), data); | ||
}) | ||
supermap.capture("{", "}", function(key, data){ | ||
return readPointerData(splitPointer(key, capture.tokens), data); | ||
}) | ||
module.exports = supermap; |
{ | ||
"name": "supermapper", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "map any object to any schema.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -126,5 +126,42 @@ # supermap | ||
## copying computed value to additional keys | ||
```javascript | ||
var dataToMap = { | ||
name: "greg" | ||
} | ||
var keyMapping = { | ||
"name": { | ||
key: "person.name", | ||
compose: (_, val) => { | ||
return val.replace(/^\w/, c => c.toUpperCase()) //capitalize first name | ||
}, | ||
additional: [ | ||
"person.firstName", | ||
"person.givenName" | ||
] | ||
}, | ||
} | ||
supermap(dataToMap, keyMapping); | ||
//result | ||
// { | ||
// "person": { | ||
// "name": "Greg", | ||
// "firstName": "Greg", | ||
// "givenName": "Greg" | ||
// } | ||
// } | ||
``` | ||
## value interpolation | ||
enclose the json pointer in `%` symbols to interpolate the value of the incoming data and use as a key. | ||
enclose the json pointer in `{brackets}` to interpolate the value of the incoming data and use as a key. | ||
@@ -141,3 +178,3 @@ ```javascript | ||
var keyMapping = { | ||
"job.responsibilities": "%job.type%.skills" | ||
"job.responsibilities": "{job.type}.skills" | ||
} | ||
@@ -201,3 +238,2 @@ | ||
## runtime opts | ||
@@ -204,0 +240,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
14695
8
405
257
3
1