PageRenderTime 50ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/lodash.rest/index.js

https://gitlab.com/olinares/code-journey
JavaScript | 247 lines | 86 code | 19 blank | 142 comment | 20 complexity | 0d93737fae0fdd7c8bbe94bea672e5fe MD5 | raw file
  1. /**
  2. * lodash 4.0.1 (Custom Build) <https://lodash.com/>
  3. * Build: `lodash modularize exports="npm" -o ./`
  4. * Copyright 2012-2016 The Dojo Foundation <http://dojofoundation.org/>
  5. * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
  6. * Copyright 2009-2016 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  7. * Available under MIT license <https://lodash.com/license>
  8. */
  9. /** Used as the `TypeError` message for "Functions" methods. */
  10. var FUNC_ERROR_TEXT = 'Expected a function';
  11. /** Used as references for various `Number` constants. */
  12. var INFINITY = 1 / 0,
  13. MAX_INTEGER = 1.7976931348623157e+308,
  14. NAN = 0 / 0;
  15. /** `Object#toString` result references. */
  16. var funcTag = '[object Function]',
  17. genTag = '[object GeneratorFunction]';
  18. /** Used to match leading and trailing whitespace. */
  19. var reTrim = /^\s+|\s+$/g;
  20. /** Used to detect bad signed hexadecimal string values. */
  21. var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
  22. /** Used to detect binary string values. */
  23. var reIsBinary = /^0b[01]+$/i;
  24. /** Used to detect octal string values. */
  25. var reIsOctal = /^0o[0-7]+$/i;
  26. /** Built-in method references without a dependency on `root`. */
  27. var freeParseInt = parseInt;
  28. /**
  29. * A faster alternative to `Function#apply`, this function invokes `func`
  30. * with the `this` binding of `thisArg` and the arguments of `args`.
  31. *
  32. * @private
  33. * @param {Function} func The function to invoke.
  34. * @param {*} thisArg The `this` binding of `func`.
  35. * @param {...*} args The arguments to invoke `func` with.
  36. * @returns {*} Returns the result of `func`.
  37. */
  38. function apply(func, thisArg, args) {
  39. var length = args.length;
  40. switch (length) {
  41. case 0: return func.call(thisArg);
  42. case 1: return func.call(thisArg, args[0]);
  43. case 2: return func.call(thisArg, args[0], args[1]);
  44. case 3: return func.call(thisArg, args[0], args[1], args[2]);
  45. }
  46. return func.apply(thisArg, args);
  47. }
  48. /** Used for built-in method references. */
  49. var objectProto = Object.prototype;
  50. /**
  51. * Used to resolve the [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
  52. * of values.
  53. */
  54. var objectToString = objectProto.toString;
  55. /* Built-in method references for those with the same name as other `lodash` methods. */
  56. var nativeMax = Math.max;
  57. /**
  58. * Creates a function that invokes `func` with the `this` binding of the
  59. * created function and arguments from `start` and beyond provided as an array.
  60. *
  61. * **Note:** This method is based on the [rest parameter](https://mdn.io/rest_parameters).
  62. *
  63. * @static
  64. * @memberOf _
  65. * @category Function
  66. * @param {Function} func The function to apply a rest parameter to.
  67. * @param {number} [start=func.length-1] The start position of the rest parameter.
  68. * @returns {Function} Returns the new function.
  69. * @example
  70. *
  71. * var say = _.rest(function(what, names) {
  72. * return what + ' ' + _.initial(names).join(', ') +
  73. * (_.size(names) > 1 ? ', & ' : '') + _.last(names);
  74. * });
  75. *
  76. * say('hello', 'fred', 'barney', 'pebbles');
  77. * // => 'hello fred, barney, & pebbles'
  78. */
  79. function rest(func, start) {
  80. if (typeof func != 'function') {
  81. throw new TypeError(FUNC_ERROR_TEXT);
  82. }
  83. start = nativeMax(start === undefined ? (func.length - 1) : toInteger(start), 0);
  84. return function() {
  85. var args = arguments,
  86. index = -1,
  87. length = nativeMax(args.length - start, 0),
  88. array = Array(length);
  89. while (++index < length) {
  90. array[index] = args[start + index];
  91. }
  92. switch (start) {
  93. case 0: return func.call(this, array);
  94. case 1: return func.call(this, args[0], array);
  95. case 2: return func.call(this, args[0], args[1], array);
  96. }
  97. var otherArgs = Array(start + 1);
  98. index = -1;
  99. while (++index < start) {
  100. otherArgs[index] = args[index];
  101. }
  102. otherArgs[start] = array;
  103. return apply(func, this, otherArgs);
  104. };
  105. }
  106. /**
  107. * Checks if `value` is classified as a `Function` object.
  108. *
  109. * @static
  110. * @memberOf _
  111. * @category Lang
  112. * @param {*} value The value to check.
  113. * @returns {boolean} Returns `true` if `value` is correctly classified, else `false`.
  114. * @example
  115. *
  116. * _.isFunction(_);
  117. * // => true
  118. *
  119. * _.isFunction(/abc/);
  120. * // => false
  121. */
  122. function isFunction(value) {
  123. // The use of `Object#toString` avoids issues with the `typeof` operator
  124. // in Safari 8 which returns 'object' for typed array constructors, and
  125. // PhantomJS 1.9 which returns 'function' for `NodeList` instances.
  126. var tag = isObject(value) ? objectToString.call(value) : '';
  127. return tag == funcTag || tag == genTag;
  128. }
  129. /**
  130. * Checks if `value` is the [language type](https://es5.github.io/#x8) of `Object`.
  131. * (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  132. *
  133. * @static
  134. * @memberOf _
  135. * @category Lang
  136. * @param {*} value The value to check.
  137. * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  138. * @example
  139. *
  140. * _.isObject({});
  141. * // => true
  142. *
  143. * _.isObject([1, 2, 3]);
  144. * // => true
  145. *
  146. * _.isObject(_.noop);
  147. * // => true
  148. *
  149. * _.isObject(null);
  150. * // => false
  151. */
  152. function isObject(value) {
  153. var type = typeof value;
  154. return !!value && (type == 'object' || type == 'function');
  155. }
  156. /**
  157. * Converts `value` to an integer.
  158. *
  159. * **Note:** This function is loosely based on [`ToInteger`](http://www.ecma-international.org/ecma-262/6.0/#sec-tointeger).
  160. *
  161. * @static
  162. * @memberOf _
  163. * @category Lang
  164. * @param {*} value The value to convert.
  165. * @returns {number} Returns the converted integer.
  166. * @example
  167. *
  168. * _.toInteger(3);
  169. * // => 3
  170. *
  171. * _.toInteger(Number.MIN_VALUE);
  172. * // => 0
  173. *
  174. * _.toInteger(Infinity);
  175. * // => 1.7976931348623157e+308
  176. *
  177. * _.toInteger('3');
  178. * // => 3
  179. */
  180. function toInteger(value) {
  181. if (!value) {
  182. return value === 0 ? value : 0;
  183. }
  184. value = toNumber(value);
  185. if (value === INFINITY || value === -INFINITY) {
  186. var sign = (value < 0 ? -1 : 1);
  187. return sign * MAX_INTEGER;
  188. }
  189. var remainder = value % 1;
  190. return value === value ? (remainder ? value - remainder : value) : 0;
  191. }
  192. /**
  193. * Converts `value` to a number.
  194. *
  195. * @static
  196. * @memberOf _
  197. * @category Lang
  198. * @param {*} value The value to process.
  199. * @returns {number} Returns the number.
  200. * @example
  201. *
  202. * _.toNumber(3);
  203. * // => 3
  204. *
  205. * _.toNumber(Number.MIN_VALUE);
  206. * // => 5e-324
  207. *
  208. * _.toNumber(Infinity);
  209. * // => Infinity
  210. *
  211. * _.toNumber('3');
  212. * // => 3
  213. */
  214. function toNumber(value) {
  215. if (isObject(value)) {
  216. var other = isFunction(value.valueOf) ? value.valueOf() : value;
  217. value = isObject(other) ? (other + '') : other;
  218. }
  219. if (typeof value != 'string') {
  220. return value === 0 ? value : +value;
  221. }
  222. value = value.replace(reTrim, '');
  223. var isBinary = reIsBinary.test(value);
  224. return (isBinary || reIsOctal.test(value))
  225. ? freeParseInt(value.slice(2), isBinary ? 2 : 8)
  226. : (reIsBadHex.test(value) ? NAN : +value);
  227. }
  228. module.exports = rest;