string-interpolation
Version:
Dynamic string manipulation
49 lines (36 loc) • 1.32 kB
JavaScript
;
require("core-js/modules/es7.symbol.async-iterator");
require("core-js/modules/es6.symbol");
require("core-js/modules/es6.regexp.split");
require("core-js/modules/es6.regexp.replace");
require("core-js/modules/es6.regexp.match");
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function getArrayKeyIndex(val) {
// Find values between []
var matchArrayIndex = val.match(/\[(.*?)\]/);
if (matchArrayIndex) {
return {
index: matchArrayIndex[1],
key: val.replace(matchArrayIndex[0], '')
};
}
return val;
}
module.exports = function (value, data) {
try {
var valuesToPull = value.split('.');
while (valuesToPull.length) {
var testValue = getArrayKeyIndex(valuesToPull.shift());
if (_typeof(testValue) === 'object') {
if (testValue.index && testValue.key) {
data = data[testValue.key][testValue.index];
}
} else {
data = data[testValue];
}
}
return data;
} catch (e) {
return '';
}
};