xl-infinite-tree
Version:
A browser-ready tree library that can efficiently display a large amount of data using infinite scrolling.
55 lines (42 loc) • 1.7 kB
JavaScript
;
exports.__esModule = true;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var trim = exports.trim = function trim(str) {
var chars = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ' \f\n\r\t\v';
while (chars.indexOf(str[0]) >= 0) {
str = str.slice(1);
}
while (chars.indexOf(str[str.length - 1]) >= 0) {
str = str.slice(0, -1);
}
return str;
};
var get = exports.get = function () {
var re = new RegExp(/[\w\-]+|\[[^\]]*\]+/g);
return function (object, path, defaultValue) {
if (!object || (typeof object === 'undefined' ? 'undefined' : _typeof(object)) !== 'object') {
return defaultValue;
}
path = '' + path;
var keys = path.match(re);
if (!keys) {
return defaultValue;
}
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
key = trim(key, ' \f\n\r\t\v');
if (key[0] === '[') {
key = trim(key.slice(1, -1), ' \f\n\r\t\v');
}
key = trim(key, '\'"');
if (object === undefined || object === null || (typeof object === 'undefined' ? 'undefined' : _typeof(object)) !== 'object') {
break;
}
object = object[key];
if (object === undefined) {
break;
}
}
return object !== undefined ? object : defaultValue;
};
}();