safe-utils
Version:
Defensive programming helper methods that will change your life
90 lines (77 loc) • 2.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.safeCatch = exports.safeJoin = exports.safeGet = void 0;
/** @function safeGet
* Access a property without having to check if it's parents are defined
*
* @param {func} func -- an arrow function that returns the property
* @param {any} defaultValue -- default value if the property can't be accessed
*
* @example
* safeGet(() => data.what.is.this)
*
*/
var safeGet = function safeGet(func, defaultValue) {
try {
var tmp = func.call();
return tmp !== undefined ? tmp : defaultValue;
} catch (e) {
return defaultValue;
}
};
/** @function safeJoin
* Join a list of strings or results of functions with separator inbetween.
* Params that loosely evaluate as false (==) are skipped.
*
* @example
* safeJoin([firstName, lastName], ' ')
* safeJoin([() => article.author.firstName, () => article.author.lastName], ' ')
*
*/
exports.safeGet = safeGet;
var safeJoin = function safeJoin(items, sep) {
var o = [];
for (var i = 0; i < items.length; i++) {
var fn = items[i];
if (typeof fn === 'function') {
try {
var t = fn();
if (t) {
o.push(t);
}
} catch (e) {// Do nothing
}
} else if (fn) {
o.push(fn);
}
}
return o.join(sep);
};
/** @function safeCatch
* Call a promise and return result and error in a Node inspired result object
* {err, res}
*
* @example
* const {err, res} = await safeCatch(myPromisFunc)(params, to, promiseFunc)
*
*/
exports.safeJoin = safeJoin;
var safeCatch = function safeCatch(promiseFunc) {
var _arguments = arguments;
return function () {
return promiseFunc.apply(promiseFunc, _arguments).then(function (res) {
return Promise.resolve({
err: undefined,
res: res
});
}).catch(function (err) {
return Promise.resolve({
err: err,
res: undefined
});
});
};
};
exports.safeCatch = safeCatch;