87 lines
2.5 KiB
JavaScript
87 lines
2.5 KiB
JavaScript
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.returnExports = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
|
|
/**
|
|
* @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
|
|
};
|
|
}
|
|
};
|
|
|
|
},{}]},{},[1])(1)
|
|
}); |