custom-string-formatter
Version:
Customizable String Formatter
25 lines (24 loc) • 752 B
JavaScript
;
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(prop, obj) {
const names = prop.split('.').filter(a => a);
let exists = false, value = obj;
for (const [i, n] of names.entries()) {
if (!i && n === 'this') {
exists = true;
continue;
}
if (value === null || value === undefined || !(n in value)) {
return { exists: false };
}
exists = true;
value = value[n];
}
return exists ? { exists, value } : { exists };
}