UNPKG

custom-string-formatter

Version:
27 lines (26 loc) 875 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveProperty = resolveProperty; /** * Parses a property and resolves its value from an object. * * It supports `this` as the first name to reference the object itself. */ function resolveProperty(path, obj) { const nameList = path.split('.').filter(a => a); let exists = false, value = obj; const ctx = { path, parent: undefined }; for (const [i, n] of nameList.entries()) { if (!i && n === 'this') { exists = true; continue; } if (value === null || value === undefined || !(n in value)) { return { exists: false, ctx: { path, parent: undefined } }; } exists = true; ctx.parent = value; value = value[n]; } return exists ? { exists, value, ctx } : { exists, ctx }; }