UNPKG

@house-agency/brewtils

Version:

The Brewery Node.js Utilities (brewtils)

55 lines (48 loc) 1.31 kB
/** * Returns value by type: * if value is a function, it executes the function, * if string - it returns a string, and so on.. * * @return {object} Whatever your value returns */ function return_by_type(value, error) { if (typeof value === 'function') { return value.call(null, error); } return value; } /** * Creates a matcher by match parameter. * The matcher matches your match parameter with the * callback's first parameter by what it contains, by type * or by calling if match is a function. * * @return {function} A match callback */ function create_match(match, value) { return error => { if ( (typeof match === 'string' && typeof error === 'string' && error === match) || (typeof match === 'string' && error.hasOwnProperty('message') && error.message === match) || (typeof match === 'function' && match.call(null, error, value)) ) { return return_by_type(value, error); } throw error; }; } /** * A tiny wrapper for a 'Not found' error * * @return {function} A match callback */ function not_found(value) { return create_match('Not found', value); } const wrapper = Object.assign( create_match, { not_found: not_found } ); module.exports = wrapper;