"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/lodash.rest/index.js" (8 Mar 2017, 8797 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /**
    2  * lodash (Custom Build) <https://lodash.com/>
    3  * Build: `lodash modularize exports="npm" -o ./`
    4  * Copyright jQuery Foundation and other contributors <https://jquery.org/>
    5  * Released under MIT license <https://lodash.com/license>
    6  * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
    7  * Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
    8  */
    9 
   10 /** Used as the `TypeError` message for "Functions" methods. */
   11 var FUNC_ERROR_TEXT = 'Expected a function';
   12 
   13 /** Used as references for various `Number` constants. */
   14 var INFINITY = 1 / 0,
   15     MAX_INTEGER = 1.7976931348623157e+308,
   16     NAN = 0 / 0;
   17 
   18 /** `Object#toString` result references. */
   19 var funcTag = '[object Function]',
   20     genTag = '[object GeneratorFunction]',
   21     symbolTag = '[object Symbol]';
   22 
   23 /** Used to match leading and trailing whitespace. */
   24 var reTrim = /^\s+|\s+$/g;
   25 
   26 /** Used to detect bad signed hexadecimal string values. */
   27 var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
   28 
   29 /** Used to detect binary string values. */
   30 var reIsBinary = /^0b[01]+$/i;
   31 
   32 /** Used to detect octal string values. */
   33 var reIsOctal = /^0o[0-7]+$/i;
   34 
   35 /** Built-in method references without a dependency on `root`. */
   36 var freeParseInt = parseInt;
   37 
   38 /**
   39  * A faster alternative to `Function#apply`, this function invokes `func`
   40  * with the `this` binding of `thisArg` and the arguments of `args`.
   41  *
   42  * @private
   43  * @param {Function} func The function to invoke.
   44  * @param {*} thisArg The `this` binding of `func`.
   45  * @param {Array} args The arguments to invoke `func` with.
   46  * @returns {*} Returns the result of `func`.
   47  */
   48 function apply(func, thisArg, args) {
   49   var length = args.length;
   50   switch (length) {
   51     case 0: return func.call(thisArg);
   52     case 1: return func.call(thisArg, args[0]);
   53     case 2: return func.call(thisArg, args[0], args[1]);
   54     case 3: return func.call(thisArg, args[0], args[1], args[2]);
   55   }
   56   return func.apply(thisArg, args);
   57 }
   58 
   59 /** Used for built-in method references. */
   60 var objectProto = Object.prototype;
   61 
   62 /**
   63  * Used to resolve the
   64  * [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
   65  * of values.
   66  */
   67 var objectToString = objectProto.toString;
   68 
   69 /* Built-in method references for those with the same name as other `lodash` methods. */
   70 var nativeMax = Math.max;
   71 
   72 /**
   73  * Creates a function that invokes `func` with the `this` binding of the
   74  * created function and arguments from `start` and beyond provided as
   75  * an array.
   76  *
   77  * **Note:** This method is based on the
   78  * [rest parameter](https://mdn.io/rest_parameters).
   79  *
   80  * @static
   81  * @memberOf _
   82  * @since 4.0.0
   83  * @category Function
   84  * @param {Function} func The function to apply a rest parameter to.
   85  * @param {number} [start=func.length-1] The start position of the rest parameter.
   86  * @returns {Function} Returns the new function.
   87  * @example
   88  *
   89  * var say = _.rest(function(what, names) {
   90  *   return what + ' ' + _.initial(names).join(', ') +
   91  *     (_.size(names) > 1 ? ', & ' : '') + _.last(names);
   92  * });
   93  *
   94  * say('hello', 'fred', 'barney', 'pebbles');
   95  * // => 'hello fred, barney, & pebbles'
   96  */
   97 function rest(func, start) {
   98   if (typeof func != 'function') {
   99     throw new TypeError(FUNC_ERROR_TEXT);
  100   }
  101   start = nativeMax(start === undefined ? (func.length - 1) : toInteger(start), 0);
  102   return function() {
  103     var args = arguments,
  104         index = -1,
  105         length = nativeMax(args.length - start, 0),
  106         array = Array(length);
  107 
  108     while (++index < length) {
  109       array[index] = args[start + index];
  110     }
  111     switch (start) {
  112       case 0: return func.call(this, array);
  113       case 1: return func.call(this, args[0], array);
  114       case 2: return func.call(this, args[0], args[1], array);
  115     }
  116     var otherArgs = Array(start + 1);
  117     index = -1;
  118     while (++index < start) {
  119       otherArgs[index] = args[index];
  120     }
  121     otherArgs[start] = array;
  122     return apply(func, this, otherArgs);
  123   };
  124 }
  125 
  126 /**
  127  * Checks if `value` is classified as a `Function` object.
  128  *
  129  * @static
  130  * @memberOf _
  131  * @since 0.1.0
  132  * @category Lang
  133  * @param {*} value The value to check.
  134  * @returns {boolean} Returns `true` if `value` is correctly classified,
  135  *  else `false`.
  136  * @example
  137  *
  138  * _.isFunction(_);
  139  * // => true
  140  *
  141  * _.isFunction(/abc/);
  142  * // => false
  143  */
  144 function isFunction(value) {
  145   // The use of `Object#toString` avoids issues with the `typeof` operator
  146   // in Safari 8 which returns 'object' for typed array and weak map constructors,
  147   // and PhantomJS 1.9 which returns 'function' for `NodeList` instances.
  148   var tag = isObject(value) ? objectToString.call(value) : '';
  149   return tag == funcTag || tag == genTag;
  150 }
  151 
  152 /**
  153  * Checks if `value` is the
  154  * [language type](http://www.ecma-international.org/ecma-262/6.0/#sec-ecmascript-language-types)
  155  * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  156  *
  157  * @static
  158  * @memberOf _
  159  * @since 0.1.0
  160  * @category Lang
  161  * @param {*} value The value to check.
  162  * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  163  * @example
  164  *
  165  * _.isObject({});
  166  * // => true
  167  *
  168  * _.isObject([1, 2, 3]);
  169  * // => true
  170  *
  171  * _.isObject(_.noop);
  172  * // => true
  173  *
  174  * _.isObject(null);
  175  * // => false
  176  */
  177 function isObject(value) {
  178   var type = typeof value;
  179   return !!value && (type == 'object' || type == 'function');
  180 }
  181 
  182 /**
  183  * Checks if `value` is object-like. A value is object-like if it's not `null`
  184  * and has a `typeof` result of "object".
  185  *
  186  * @static
  187  * @memberOf _
  188  * @since 4.0.0
  189  * @category Lang
  190  * @param {*} value The value to check.
  191  * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  192  * @example
  193  *
  194  * _.isObjectLike({});
  195  * // => true
  196  *
  197  * _.isObjectLike([1, 2, 3]);
  198  * // => true
  199  *
  200  * _.isObjectLike(_.noop);
  201  * // => false
  202  *
  203  * _.isObjectLike(null);
  204  * // => false
  205  */
  206 function isObjectLike(value) {
  207   return !!value && typeof value == 'object';
  208 }
  209 
  210 /**
  211  * Checks if `value` is classified as a `Symbol` primitive or object.
  212  *
  213  * @static
  214  * @memberOf _
  215  * @since 4.0.0
  216  * @category Lang
  217  * @param {*} value The value to check.
  218  * @returns {boolean} Returns `true` if `value` is correctly classified,
  219  *  else `false`.
  220  * @example
  221  *
  222  * _.isSymbol(Symbol.iterator);
  223  * // => true
  224  *
  225  * _.isSymbol('abc');
  226  * // => false
  227  */
  228 function isSymbol(value) {
  229   return typeof value == 'symbol' ||
  230     (isObjectLike(value) && objectToString.call(value) == symbolTag);
  231 }
  232 
  233 /**
  234  * Converts `value` to a finite number.
  235  *
  236  * @static
  237  * @memberOf _
  238  * @since 4.12.0
  239  * @category Lang
  240  * @param {*} value The value to convert.
  241  * @returns {number} Returns the converted number.
  242  * @example
  243  *
  244  * _.toFinite(3.2);
  245  * // => 3.2
  246  *
  247  * _.toFinite(Number.MIN_VALUE);
  248  * // => 5e-324
  249  *
  250  * _.toFinite(Infinity);
  251  * // => 1.7976931348623157e+308
  252  *
  253  * _.toFinite('3.2');
  254  * // => 3.2
  255  */
  256 function toFinite(value) {
  257   if (!value) {
  258     return value === 0 ? value : 0;
  259   }
  260   value = toNumber(value);
  261   if (value === INFINITY || value === -INFINITY) {
  262     var sign = (value < 0 ? -1 : 1);
  263     return sign * MAX_INTEGER;
  264   }
  265   return value === value ? value : 0;
  266 }
  267 
  268 /**
  269  * Converts `value` to an integer.
  270  *
  271  * **Note:** This function is loosely based on
  272  * [`ToInteger`](http://www.ecma-international.org/ecma-262/6.0/#sec-tointeger).
  273  *
  274  * @static
  275  * @memberOf _
  276  * @since 4.0.0
  277  * @category Lang
  278  * @param {*} value The value to convert.
  279  * @returns {number} Returns the converted integer.
  280  * @example
  281  *
  282  * _.toInteger(3.2);
  283  * // => 3
  284  *
  285  * _.toInteger(Number.MIN_VALUE);
  286  * // => 0
  287  *
  288  * _.toInteger(Infinity);
  289  * // => 1.7976931348623157e+308
  290  *
  291  * _.toInteger('3.2');
  292  * // => 3
  293  */
  294 function toInteger(value) {
  295   var result = toFinite(value),
  296       remainder = result % 1;
  297 
  298   return result === result ? (remainder ? result - remainder : result) : 0;
  299 }
  300 
  301 /**
  302  * Converts `value` to a number.
  303  *
  304  * @static
  305  * @memberOf _
  306  * @since 4.0.0
  307  * @category Lang
  308  * @param {*} value The value to process.
  309  * @returns {number} Returns the number.
  310  * @example
  311  *
  312  * _.toNumber(3.2);
  313  * // => 3.2
  314  *
  315  * _.toNumber(Number.MIN_VALUE);
  316  * // => 5e-324
  317  *
  318  * _.toNumber(Infinity);
  319  * // => Infinity
  320  *
  321  * _.toNumber('3.2');
  322  * // => 3.2
  323  */
  324 function toNumber(value) {
  325   if (typeof value == 'number') {
  326     return value;
  327   }
  328   if (isSymbol(value)) {
  329     return NAN;
  330   }
  331   if (isObject(value)) {
  332     var other = isFunction(value.valueOf) ? value.valueOf() : value;
  333     value = isObject(other) ? (other + '') : other;
  334   }
  335   if (typeof value != 'string') {
  336     return value === 0 ? value : +value;
  337   }
  338   value = value.replace(reTrim, '');
  339   var isBinary = reIsBinary.test(value);
  340   return (isBinary || reIsOctal.test(value))
  341     ? freeParseInt(value.slice(2), isBinary ? 2 : 8)
  342     : (reIsBadHex.test(value) ? NAN : +value);
  343 }
  344 
  345 module.exports = rest;