fastify-http-errors-enhanced
Version:
A error handling plugin for Fastify that uses enhanced HTTP errors.
20 lines (19 loc) • 695 B
JavaScript
export function upperFirst(source) {
if (typeof source !== 'string' || !source.length) {
return source;
}
return source[0].toUpperCase() + source.slice(1);
}
export function get(target, path) {
const tokens = path.split('.').map((t)=>t.trim());
for (const token of tokens){
if (typeof target === 'undefined' || target === null) {
// We're supposed to be still iterating, but the chain is over - Return undefined
target = undefined;
break;
}
const index = token.match(/^(\d+)|\[(\d+)]$/);
target = index ? target[Number.parseInt(index[1] ?? index[2], 10)] : target[token];
}
return target;
}