@synatic/parameter-parser
Advanced tools
Comparing version 0.0.1 to 0.1.0
@@ -1,2 +0,1 @@ | ||
module.exports=require('./lib') | ||
module.exports = require('./lib'); |
320
lib/index.js
'use strict'; | ||
const $check=require('check-types'); | ||
const $json=require('json-magic'); | ||
const $copy=require('deepcopy'); | ||
const $check = require('check-types'); | ||
const $json = require('json-magic'); | ||
const cloneDeep = require('clone-deep'); | ||
class ParameterParser { | ||
static parse(obj, parameters, options) { | ||
if (!parameters) return obj; | ||
if (!obj) return obj; | ||
if (!options) options = {}; | ||
options.idCharacter = options.idCharacter ? options.idCharacter : '@'; | ||
class ParameterParser{ | ||
static parse(obj,parameters,options){ | ||
if (!parameters)return obj; | ||
if (!obj)return obj; | ||
if (!options)options={}; | ||
options.idCharacter=options.idCharacter?options.idCharacter:'@'; | ||
function recurseObj(curObj) { | ||
for (let key in curObj) { | ||
if (!curObj.hasOwnProperty(key))continue; | ||
let objVal = curObj[key]; | ||
checkRecurse(objVal,curObj,key); | ||
for (const key in curObj) { | ||
if (!curObj.hasOwnProperty(key)) continue; | ||
const objVal = curObj[key]; | ||
checkRecurse(objVal, curObj, key); | ||
} | ||
} | ||
function recurseArr(curArr){ | ||
function recurseArr(curArr) { | ||
for (let i = 0; i < curArr.length; i++) { | ||
checkRecurse(curArr[i],curArr,i); | ||
checkRecurse(curArr[i], curArr, i); | ||
} | ||
} | ||
function checkRecurse(objVal,parentObj,parentKey){ | ||
function checkRecurse(objVal, parentObj, parentKey) { | ||
if ($check.array(objVal)) { | ||
@@ -34,16 +33,19 @@ recurseArr(objVal); | ||
} else if ($check.string(objVal)) { | ||
let strReplaceVal=objVal; | ||
let strReplaceVal = objVal; | ||
if (objVal.startsWith(options.idCharacter)) { | ||
let paramName = objVal.substring(1); | ||
try{ | ||
strReplaceVal=$json.get(parameters,paramName); | ||
}catch(exp){} | ||
}else{ | ||
strReplaceVal=ParameterParser.parseString(objVal,parameters,options.idCharacter); | ||
const paramName = objVal.substring(1); | ||
try { | ||
strReplaceVal = $json.get(parameters, paramName); | ||
} catch (exp) { | ||
// ignore error | ||
} | ||
} else { | ||
strReplaceVal = ParameterParser.parseString(objVal, parameters, options.idCharacter); | ||
} | ||
if (parentObj&&parentKey) | ||
parentObj[parentKey]=options.copy?$copy(strReplaceVal):strReplaceVal; | ||
else | ||
if (parentObj && parentKey) { | ||
parentObj[parentKey] = options.copy ? cloneDeep(strReplaceVal) : strReplaceVal; | ||
} else { | ||
return strReplaceVal; | ||
}else { | ||
} | ||
} else { | ||
return objVal; | ||
@@ -54,14 +56,12 @@ } | ||
let retVal=checkRecurse(obj); | ||
const retVal = checkRecurse(obj); | ||
return retVal; | ||
} | ||
static stripValues(obj,replaceChar) { | ||
if (!obj)return obj; | ||
static stripValues(obj, replaceChar) { | ||
if (!obj) return obj; | ||
if (!replaceChar) replaceChar = null; | ||
if (!$check.array(obj) && !$check.object(obj)) return replaceChar; | ||
if (!$check.array(obj)&&!$check.object(obj))return replaceChar; | ||
function checkRecurse(objVal, parentObj, parentKey) { | ||
@@ -80,6 +80,6 @@ if ($check.array(objVal)) { | ||
function recurseObj(curObj) { | ||
for (let key in curObj) { | ||
if (!curObj.hasOwnProperty(key))continue; | ||
for (const key in curObj) { | ||
if (!curObj.hasOwnProperty(key)) continue; | ||
let objVal = curObj[key]; | ||
const objVal = curObj[key]; | ||
@@ -90,3 +90,3 @@ checkRecurse(objVal, curObj, key); | ||
checkRecurse(obj,null,null); | ||
checkRecurse(obj, null, null); | ||
@@ -96,56 +96,55 @@ return obj; | ||
static getParameterPaths(obj,idCharacter){ | ||
let paramPaths=[]; | ||
if (!obj)return paramPaths; | ||
if (!idCharacter)idCharacter="@"; | ||
static getParameterPaths(obj, idCharacter) { | ||
const paramPaths = []; | ||
if (!obj) return paramPaths; | ||
if (!idCharacter) idCharacter = '@'; | ||
function checkRecurse(objVal,parentObj,parentKey,curPath){ | ||
function checkRecurse(objVal, parentObj, parentKey, curPath) { | ||
if ($check.array(objVal)) { | ||
for (let i = 0; i < objVal.length; i++) { | ||
checkRecurse(objVal[i],objVal,i,curPath + '/' + i); | ||
checkRecurse(objVal[i], objVal, i, curPath + '/' + i); | ||
} | ||
} else if ($check.object(objVal)) { | ||
recurseObj(objVal,curPath); | ||
recurseObj(objVal, curPath); | ||
} else if ($check.string(objVal)) { | ||
if (objVal.startsWith(idCharacter)) { | ||
let paramName = objVal.substring(1); | ||
const paramName = objVal.substring(1); | ||
paramPaths.push({ | ||
from:'/' + paramName.replace(/\./g,'/'), | ||
to:curPath, | ||
paramId:idCharacter + paramName | ||
from: '/' + paramName.replace(/\./g, '/'), | ||
to: curPath, | ||
paramId: idCharacter + paramName, | ||
}); | ||
} | ||
}else { | ||
//dont do anything | ||
} else { | ||
// dont do anything | ||
} | ||
} | ||
function recurseObj(curObj,curPath) { | ||
for (let key in curObj) { | ||
if (!curObj.hasOwnProperty(key))continue; | ||
function recurseObj(curObj, curPath) { | ||
for (const key in curObj) { | ||
if (!curObj.hasOwnProperty(key)) continue; | ||
let objVal = curObj[key]; | ||
const objVal = curObj[key]; | ||
checkRecurse(objVal,curObj,key,curPath + '/' + key); | ||
checkRecurse(objVal, curObj, key, curPath + '/' + key); | ||
} | ||
} | ||
checkRecurse(obj,null,null,''); | ||
checkRecurse(obj, null, null, ''); | ||
return paramPaths; | ||
} | ||
static getObjectParameters(obj,idCharacter){ | ||
let requiredParams={}; | ||
if (!obj)return requiredParams; | ||
if (!idCharacter)idCharacter="@"; | ||
static getObjectParameters(obj, idCharacter) { | ||
const requiredParams = {}; | ||
if (!obj) return requiredParams; | ||
if (!idCharacter) idCharacter = '@'; | ||
let paramPaths=[]; | ||
let regEx = new RegExp("\{" + idCharacter + "([^\}]+)\}","g"); | ||
const paramPaths = []; | ||
const regEx = new RegExp('{' + idCharacter + '([^}]+)}', 'g'); | ||
function checkRecurse(objVal,parentObj,parentKey){ | ||
function checkRecurse(objVal, parentObj, parentKey) { | ||
if ($check.array(objVal)) { | ||
for (let i = 0; i < objVal.length; i++) { | ||
checkRecurse(objVal[i],objVal,i); | ||
checkRecurse(objVal[i], objVal, i); | ||
} | ||
@@ -156,16 +155,15 @@ } else if ($check.object(objVal)) { | ||
if (objVal.startsWith(idCharacter)) { | ||
let paramName = objVal.substring(1); | ||
const paramName = objVal.substring(1); | ||
paramPaths.push(paramName); | ||
}else{ | ||
let matches=objVal.match(regEx); | ||
if (matches){ | ||
for (let match of matches){ | ||
let paramName = match.substring(2,match.length-1); | ||
} else { | ||
const matches = objVal.match(regEx); | ||
if (matches) { | ||
for (const match of matches) { | ||
const paramName = match.substring(2, match.length - 1); | ||
paramPaths.push(paramName); | ||
} | ||
} | ||
} | ||
}else { | ||
//dont do anything | ||
} else { | ||
// dont do anything | ||
} | ||
@@ -175,38 +173,36 @@ } | ||
function recurseObj(curObj) { | ||
for (let key in curObj) { | ||
if (!curObj.hasOwnProperty(key))continue; | ||
for (const key in curObj) { | ||
if (!curObj.hasOwnProperty(key)) continue; | ||
let objVal = curObj[key]; | ||
const objVal = curObj[key]; | ||
checkRecurse(objVal,curObj,key); | ||
checkRecurse(objVal, curObj, key); | ||
} | ||
} | ||
checkRecurse(obj,null,null); | ||
checkRecurse(obj, null, null); | ||
for (let paramPath of paramPaths){ | ||
let paramPathArr=paramPath.split('.'); | ||
let paramName=paramPathArr.shift(); | ||
if (!requiredParams[paramName])requiredParams[paramName]=[]; | ||
let pathForParam=paramPathArr.join('.'); | ||
if (requiredParams[paramName].indexOf(pathForParam)<0) | ||
requiredParams[paramName].push(pathForParam) | ||
for (const paramPath of paramPaths) { | ||
const paramPathArr = paramPath.split('.'); | ||
const paramName = paramPathArr.shift(); | ||
if (!requiredParams[paramName]) requiredParams[paramName] = []; | ||
const pathForParam = paramPathArr.join('.'); | ||
if (requiredParams[paramName].indexOf(pathForParam) < 0) requiredParams[paramName].push(pathForParam); | ||
} | ||
return requiredParams; | ||
} | ||
static getParamaterSchema(obj,idCharacter){ | ||
let paramSchema={type:"object",properties:{}}; | ||
if (!obj)return paramSchema; | ||
if (!idCharacter)idCharacter="@"; | ||
static getParamaterSchema(obj, idCharacter) { | ||
const paramSchema = {type: 'object', properties: {}}; | ||
if (!obj) return paramSchema; | ||
if (!idCharacter) idCharacter = '@'; | ||
let paramPaths=[]; | ||
let defaultType=["string","object","number","integer","boolean"]; | ||
const paramPaths = []; | ||
const defaultType = ['string', 'object', 'number', 'integer', 'boolean']; | ||
function checkRecurse(objVal,parentObj,parentKey){ | ||
function checkRecurse(objVal, parentObj, parentKey) { | ||
if ($check.array(objVal)) { | ||
for (let i = 0; i < objVal.length; i++) { | ||
checkRecurse(objVal[i],objVal,i); | ||
checkRecurse(objVal[i], objVal, i); | ||
} | ||
@@ -217,7 +213,7 @@ } else if ($check.object(objVal)) { | ||
if (objVal.startsWith(idCharacter)) { | ||
var paramName = objVal.substring(1); | ||
const paramName = objVal.substring(1); | ||
paramPaths.push(paramName); | ||
} | ||
}else { | ||
//dont do anything | ||
} else { | ||
// dont do anything | ||
} | ||
@@ -227,8 +223,8 @@ } | ||
function recurseObj(curObj) { | ||
for (let key in curObj) { | ||
if (!curObj.hasOwnProperty(key))continue; | ||
for (const key in curObj) { | ||
if (!curObj.hasOwnProperty(key)) continue; | ||
let objVal = curObj[key]; | ||
const objVal = curObj[key]; | ||
checkRecurse(objVal,curObj,key); | ||
checkRecurse(objVal, curObj, key); | ||
} | ||
@@ -239,68 +235,64 @@ } | ||
for (let paramPath of paramPaths){ | ||
let paramPathArr=paramPath.split('.'); | ||
let schemaPathArr=[]; | ||
for (let paramPathArrVal of paramPathArr){ | ||
$json.set(paramSchema,schemaPathArr.concat("type"),"object"); | ||
schemaPathArr.push("properties"); | ||
for (const paramPath of paramPaths) { | ||
const paramPathArr = paramPath.split('.'); | ||
const schemaPathArr = []; | ||
for (const paramPathArrVal of paramPathArr) { | ||
$json.set(paramSchema, schemaPathArr.concat('type'), 'object'); | ||
schemaPathArr.push('properties'); | ||
schemaPathArr.push(paramPathArrVal); | ||
} | ||
$json.set(paramSchema,schemaPathArr,{type:defaultType}); | ||
$json.set(paramSchema, schemaPathArr, {type: defaultType}); | ||
} | ||
return paramSchema; | ||
} | ||
static parseString(string,parameters,idCharacter){ | ||
if (!string)return string; | ||
static parseString(string, parameters, idCharacter) { | ||
if (!string) return string; | ||
let options={}; | ||
if (idCharacter&&$check.string(idCharacter)){ | ||
options.idCharacter=idCharacter; | ||
}else if (idCharacter&&$check.object(idCharacter)) { | ||
options=idCharacter; | ||
let options = {}; | ||
if (idCharacter && $check.string(idCharacter)) { | ||
options.idCharacter = idCharacter; | ||
} else if (idCharacter && $check.object(idCharacter)) { | ||
options = idCharacter; | ||
} | ||
if (!options.idCharacter)options.idCharacter="@"; | ||
if (!options.idCharacter) options.idCharacter = '@'; | ||
let regEx = new RegExp("\{\{|\}\}|\{" + options.idCharacter + "([^\}]+)\}","g"); | ||
const regEx = new RegExp('{{|}}|{' + options.idCharacter + '([^}]+)}', 'g'); | ||
return string.replace(regEx, function (m, n) { | ||
if (!n){return m;} | ||
if (!n) { | ||
return m; | ||
} | ||
if (m == '{{') { | ||
//return '{'; | ||
// return '{'; | ||
} | ||
if (m == '}}') { | ||
//return '}'; | ||
// return '}'; | ||
} | ||
let paramName = n; | ||
let paramVal=null; | ||
try{ | ||
paramVal=$json.get(parameters,paramName); | ||
}catch(exp){} | ||
const paramName = n; | ||
let paramVal = null; | ||
try { | ||
paramVal = $json.get(parameters, paramName); | ||
} catch (exp) { | ||
// ignore error | ||
} | ||
if ($check.assigned(paramVal)){ | ||
if (options.uriEncode) | ||
paramVal=encodeURIComponent(paramVal); | ||
if ($check.assigned(paramVal)) { | ||
if (options.uriEncode) paramVal = encodeURIComponent(paramVal); | ||
return paramVal; | ||
} | ||
else | ||
return m; | ||
} else return m; | ||
}); | ||
} | ||
static parseObject(obj,parameters,options) { | ||
if (!parameters)return obj; | ||
if (!obj)return obj; | ||
if(!options)options={}; | ||
if (!options.idCharacter)options.idCharacter="@"; | ||
static parseObject(obj, parameters, options) { | ||
if (!parameters) return obj; | ||
if (!obj) return obj; | ||
if (!options) options = {}; | ||
if (!options.idCharacter) options.idCharacter = '@'; | ||
function checkRecurse(objVal,parentObj,parentKey){ | ||
function checkRecurse(objVal, parentObj, parentKey) { | ||
if ($check.array(objVal)) { | ||
for (let i = 0; i < objVal.length; i++) { | ||
checkRecurse(objVal[i],objVal,i); | ||
for (let i = 0; i < objVal.length; i++) { | ||
checkRecurse(objVal[i], objVal, i); | ||
} | ||
@@ -311,18 +303,18 @@ } else if ($check.object(objVal)) { | ||
if (objVal.startsWith(options.idCharacter)) { | ||
let paramName = objVal.substring(1); | ||
let paramVal=null; | ||
try{ | ||
paramVal=$json.get(parameters,paramName); | ||
}catch(exp){} | ||
const paramName = objVal.substring(1); | ||
let paramVal = null; | ||
try { | ||
paramVal = $json.get(parameters, paramName); | ||
} catch (exp) { | ||
// ignore error | ||
} | ||
if ($check.assigned(paramVal)){ | ||
let reworkedVal=options.copy?$copy(paramVal):paramVal; | ||
if ($check.string(reworkedVal)&&options.uriEncode) | ||
reworkedVal=encodeURIComponent(reworkedVal); | ||
if ($check.assigned(paramVal)) { | ||
let reworkedVal = options.copy ? cloneDeep(paramVal) : paramVal; | ||
if ($check.string(reworkedVal) && options.uriEncode) reworkedVal = encodeURIComponent(reworkedVal); | ||
parentObj[parentKey] = reworkedVal; | ||
} | ||
} | ||
}else { | ||
//dont do anything | ||
} else { | ||
// dont do anything | ||
} | ||
@@ -332,8 +324,8 @@ } | ||
function recurseObj(curObj) { | ||
for (let key in curObj) { | ||
if (!curObj.hasOwnProperty(key))continue; | ||
for (const key in curObj) { | ||
if (!curObj.hasOwnProperty(key)) continue; | ||
let objVal = curObj[key]; | ||
const objVal = curObj[key]; | ||
checkRecurse(objVal,curObj,key); | ||
checkRecurse(objVal, curObj, key); | ||
} | ||
@@ -348,4 +340,2 @@ } | ||
module.exports=ParameterParser; | ||
module.exports = ParameterParser; |
{ | ||
"name": "@synatic/parameter-parser", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Parameter Parser", | ||
"main": "index.js", | ||
"scripts": { | ||
"lint": "eslint .", | ||
"prettier": "prettier . --write", | ||
"test": "mocha --reporter spec --bail --check-leaks test/", | ||
"test-ci": "nyc --reporter text-summary mocha -- --reporter spec --check-leaks test/", | ||
"test-cov": "nyc --reporter lcov --reporter text mocha -- --reporter dot --check-leaks test/" | ||
}, | ||
"files": [ | ||
"lib/", | ||
"index.js" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/synatic/parameter-parser.git" | ||
}, | ||
"keywords": [], | ||
"author": { | ||
"name": "Martin Naude", | ||
"email": "info@synatic.com" | ||
"name": "FilePounder Inc", | ||
"url": "https://synatic.com" | ||
}, | ||
"contributors": [ | ||
{ | ||
"name": "Martin Naude" | ||
}, | ||
{ | ||
"name": "Thiren Bunsee" | ||
} | ||
], | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/synatic/parameter-parser/issues" | ||
}, | ||
"homepage": "https://github.com/synatic/parameter-parser#readme", | ||
"engines": { | ||
"node": ">=12" | ||
}, | ||
"dependencies": { | ||
"json-magic": "0.0.10", | ||
"check-types": "10.0.0", | ||
"deepcopy": "2.0.0" | ||
"check-types": "11.1.2", | ||
"clone-deep": "4.0.1", | ||
"json-magic": "0.1.0" | ||
}, | ||
"engines": { | ||
"node": ">=6.0.0" | ||
"devDependencies": { | ||
"eslint": "^7.16.0", | ||
"eslint-config-google": "^0.14.0", | ||
"eslint-config-prettier": "^7.1.0", | ||
"mocha": "^8.2.1", | ||
"mongodb": "^3.6.3", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.2.1" | ||
} | ||
} |
# parameter-parser | ||
Parameter Parser |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
13462
5
277
0
1
4
0
7
1
1
+ Addedclone-deep@4.0.1
+ Addedcheck-types@11.1.2(transitive)
+ Addedclone-deep@4.0.1(transitive)
+ Addedis-plain-object@2.0.4(transitive)
+ Addedisobject@3.0.1(transitive)
+ Addedjson-magic@0.1.0(transitive)
+ Addedjson-pointer@0.6.1(transitive)
+ Addedkind-of@6.0.3(transitive)
+ Addedshallow-clone@3.0.1(transitive)
- Removeddeepcopy@2.0.0
- Removedcheck-types@10.0.08.0.2(transitive)
- Removeddeepcopy@2.0.0(transitive)
- Removedjson-magic@0.0.10(transitive)
- Removedjson-pointer@0.6.0(transitive)
- Removedtype-detect@4.1.0(transitive)
Updatedcheck-types@11.1.2
Updatedjson-magic@0.1.0