UNPKG

@progress/kendo-charts

Version:

Kendo UI platform-independent Charts library

28 lines (22 loc) 711 B
const FIELD_REGEX = /\[(?:(\d+)|['"](.*?)['"])\]|((?:(?!\[.*?\]|\.).)+)/g; const getterCache = {}; getterCache['undefined'] = function(obj) { return obj; }; export default function getter(field) { if (getterCache[field]) { return getterCache[field]; } const fields = []; field.replace(FIELD_REGEX, function(match, index, indexAccessor, field) { fields.push(index !== undefined ? index : (indexAccessor || field)); }); getterCache[field] = function(obj) { let result = obj; for (let idx = 0; idx < fields.length && result; idx++) { result = result[fields[idx]]; } return result; }; return getterCache[field]; }