node-red-viseo-helper
Advanced tools
Comparing version 0.1.19 to 0.1.20
@@ -0,0 +0,0 @@ # Contributor Covenant Code of Conduct |
@@ -0,0 +0,0 @@ # Contributing to the Project |
36
index.js
@@ -49,5 +49,18 @@ 'use strict'; | ||
const RXP_JSFIELD = /[a-zA-Z0-9\._]/ | ||
const getByStringFast = exports.getByStringFast = (obj, str, def) => { | ||
if (!str) return def; | ||
try { | ||
let tmp = obj; | ||
for (let field of str.split('.')){ | ||
if (tmp[field] === undefined) { return def; } | ||
tmp = tmp[field] | ||
} | ||
return tmp; | ||
} catch (err) { return def; } | ||
} | ||
const getByString = exports.getByString = (obj, str, def) => { | ||
if (!str) return def; | ||
if (RXP_JSFIELD.test(str)) { return getByStringFast(obj, str, def); } | ||
let ctxt = { "data": obj , "value": undefined }; | ||
@@ -63,4 +76,27 @@ const context = new vm.createContext(ctxt); | ||
const setByStringFast = exports.setByStringFast = (obj, str, value, error) => { | ||
if (!str) return def; | ||
try { | ||
let fields = str.split('.'); | ||
let tmp = obj; | ||
for (let i = 0 ; i < fields.length ; i++){ | ||
let field = fields[i]; | ||
if (i === (fields.length-1)){ | ||
tmp[field] = value; | ||
return; | ||
} | ||
if (tmp[field] === undefined) { | ||
return; // Should we create the object instead ? | ||
} | ||
tmp = tmp[field] | ||
} | ||
} catch (ex){ | ||
if (error) error(ex) | ||
else console.log(ex) | ||
} | ||
} | ||
const setByString = exports.setByString = (obj, str, value, error) => { | ||
if (!str) return; | ||
if (RXP_JSFIELD.test(str)) { return setByStringFast(obj, str, value, error); } | ||
let ctxt = { "data": obj, "value" : value }; | ||
@@ -67,0 +103,0 @@ const context = new vm.createContext(ctxt); |
@@ -0,0 +0,0 @@ const extend = require('extend'); |
{ | ||
"name" : "node-red-viseo-helper", | ||
"version" : "0.1.19", | ||
"version" : "0.1.20", | ||
"description" : "Utility libarary for Node-RED", | ||
@@ -5,0 +5,0 @@ "dependencies" : { }, |
@@ -0,0 +0,0 @@ # Node-RED |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
24144
9
170