File: D:/HostingSpaces/SBogers10/somerenslust.komma.pro/node_modules/attempt-x/index.js
/**
* @file Invokes function, returning an object of the results.
* @version 1.1.1
* @author Xotic750 <Xotic750@gmail.com>
* @copyright Xotic750
* @license {@link <https://opensource.org/licenses/MIT> MIT}
* @module attempt-x
*/
'use strict';
var getArgs = function _getArgs(args) {
var length = args.length >>> 0;
var array = [];
var argLength = length - 1;
if (argLength < 1) {
return array;
}
array.length = argLength;
for (var index = 1; index < length; index += 1) {
array[index - 1] = args[index];
}
return array;
};
/**
* This method attempts to invoke the function, returning either the result or
* the caught error object. Any additional arguments are provided to the
* function when it's invoked.
*
* @param {Function} fn - The function to attempt.
* @param {...*} [args] - The arguments to invoke the function with.
* @returns {Object} Returns an object of the result.
* @example
* var attempt = require('attempt-x');
*
* function thrower() {
* throw new Error('Threw');
* }
*
* attempt(thrower, 1, 2);
* // {
* // threw: true,
* // value: // Error('Threw') object
* // }
*
* function sumArgs(a, b) {
* return a + b;
* }
*
* attempt(sumArgs, 1, 2);
* // {
* // threw: false,
* // value: 3
* // }
*
* var thisArg = [];
* function pusher(a, b) {
* return this.push(a, b);
* }
*
* attempt.call(thisArg, pusher, 1, 2);
* // {
* // threw: false,
* // value: 2
* // }
* // thisArg => [1, 2];
*/
module.exports = function attempt(fn) {
try {
return {
threw: false,
value: fn.apply(this, getArgs(arguments))
};
} catch (e) {
return {
threw: true,
value: e
};
}
};