dbccedd057922f0ad10fa786feb1b2d2b99835a1b18bba49b5b5abd208141f60d7430480cb98258233129fc30414dd7d89ff9970b1fa6d86b31e81adcc01a1 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. 'use strict';
  2. exports.__esModule = true;
  3. exports.isFunction = isFunction;
  4. exports.throttle = throttle;
  5. exports.throttleAfterHits = throttleAfterHits;
  6. exports.debounce = debounce;
  7. exports.pipe = pipe;
  8. exports.partial = partial;
  9. exports.curry = curry;
  10. exports.curryRight = curryRight;
  11. var _array = require('./array');
  12. /**
  13. * Checks if given variable is function.
  14. *
  15. * @param {*} func Variable to check.
  16. * @returns {Boolean}
  17. */
  18. function isFunction(func) {
  19. return typeof func === 'function';
  20. }
  21. /**
  22. * Creates throttle function that enforces a maximum number of times a function (`func`) can be called over time (`wait`).
  23. *
  24. * @param {Function} func Function to invoke.
  25. * @param {Number} wait Delay in miliseconds.
  26. * @returns {Function}
  27. */
  28. function throttle(func) {
  29. var wait = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 200;
  30. var lastCalled = 0;
  31. var result = {
  32. lastCallThrottled: true
  33. };
  34. var lastTimer = null;
  35. function _throttle() {
  36. var _this = this;
  37. var args = arguments;
  38. var stamp = Date.now();
  39. var needCall = false;
  40. result.lastCallThrottled = true;
  41. if (!lastCalled) {
  42. lastCalled = stamp;
  43. needCall = true;
  44. }
  45. var remaining = wait - (stamp - lastCalled);
  46. if (needCall) {
  47. result.lastCallThrottled = false;
  48. func.apply(this, args);
  49. } else {
  50. if (lastTimer) {
  51. clearTimeout(lastTimer);
  52. }
  53. lastTimer = setTimeout(function () {
  54. result.lastCallThrottled = false;
  55. func.apply(_this, args);
  56. lastCalled = 0;
  57. lastTimer = void 0;
  58. }, remaining);
  59. }
  60. return result;
  61. }
  62. return _throttle;
  63. }
  64. /**
  65. * Creates throttle function that enforces a maximum number of times a function (`func`) can be called over
  66. * time (`wait`) after specified hits.
  67. *
  68. * @param {Function} func Function to invoke.
  69. * @param {Number} wait Delay in miliseconds.
  70. * @param {Number} hits Number of hits after throttling will be applied.
  71. * @returns {Function}
  72. */
  73. function throttleAfterHits(func) {
  74. var wait = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 200;
  75. var hits = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 10;
  76. var funcThrottle = throttle(func, wait);
  77. var remainHits = hits;
  78. function _clearHits() {
  79. remainHits = hits;
  80. }
  81. function _throttleAfterHits() {
  82. if (remainHits) {
  83. remainHits--;
  84. return func.apply(this, arguments);
  85. }
  86. return funcThrottle.apply(this, arguments);
  87. }
  88. _throttleAfterHits.clearHits = _clearHits;
  89. return _throttleAfterHits;
  90. }
  91. /**
  92. * Creates debounce function that enforces a function (`func`) not be called again until a certain amount of time (`wait`)
  93. * has passed without it being called.
  94. *
  95. * @param {Function} func Function to invoke.
  96. * @param {Number} wait Delay in milliseconds.
  97. * @returns {Function}
  98. */
  99. function debounce(func) {
  100. var wait = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 200;
  101. var lastTimer = null;
  102. var result = void 0;
  103. function _debounce() {
  104. var _this2 = this;
  105. var args = arguments;
  106. if (lastTimer) {
  107. clearTimeout(lastTimer);
  108. }
  109. lastTimer = setTimeout(function () {
  110. result = func.apply(_this2, args);
  111. }, wait);
  112. return result;
  113. }
  114. return _debounce;
  115. }
  116. /**
  117. * Creates the function that returns the result of calling the given functions. Result of the first function is passed to
  118. * the second as an argument and so on. Only first function in the chain can handle multiple arguments.
  119. *
  120. * @param {Function} functions Functions to compose.
  121. * @returns {Function}
  122. */
  123. function pipe() {
  124. for (var _len = arguments.length, functions = Array(_len), _key = 0; _key < _len; _key++) {
  125. functions[_key] = arguments[_key];
  126. }
  127. var firstFunc = functions[0],
  128. restFunc = functions.slice(1);
  129. return function _pipe() {
  130. return (0, _array.arrayReduce)(restFunc, function (acc, fn) {
  131. return fn(acc);
  132. }, firstFunc.apply(this, arguments));
  133. };
  134. }
  135. /**
  136. * Creates the function that returns the function with cached arguments.
  137. *
  138. * @param {Function} func Function to partialization.
  139. * @param {Array} params Function arguments to cache.
  140. * @returns {Function}
  141. */
  142. function partial(func) {
  143. for (var _len2 = arguments.length, params = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
  144. params[_key2 - 1] = arguments[_key2];
  145. }
  146. return function _partial() {
  147. for (var _len3 = arguments.length, restParams = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
  148. restParams[_key3] = arguments[_key3];
  149. }
  150. return func.apply(this, params.concat(restParams));
  151. };
  152. }
  153. /**
  154. * Creates the functions that returns the function with cached arguments. If count if passed arguments will be matched
  155. * to the arguments defined in `func` then function will be invoked.
  156. * Arguments are added to the stack in direction from the left to the right.
  157. *
  158. * @example
  159. * ```
  160. * var replace = curry(function(find, replace, string) {
  161. * return string.replace(find, replace);
  162. * });
  163. *
  164. * // returns function with bounded first argument
  165. * var replace = replace('foo')
  166. *
  167. * // returns replaced string - all arguments was passed so function was invoked
  168. * replace('bar', 'Some test with foo...');
  169. *
  170. * ```
  171. *
  172. * @param {Function} func Function to currying.
  173. * @returns {Function}
  174. */
  175. function curry(func) {
  176. var argsLength = func.length;
  177. function given(argsSoFar) {
  178. return function _curry() {
  179. for (var _len4 = arguments.length, params = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
  180. params[_key4] = arguments[_key4];
  181. }
  182. var passedArgsSoFar = argsSoFar.concat(params);
  183. var result = void 0;
  184. if (passedArgsSoFar.length >= argsLength) {
  185. result = func.apply(this, passedArgsSoFar);
  186. } else {
  187. result = given(passedArgsSoFar);
  188. }
  189. return result;
  190. };
  191. }
  192. return given([]);
  193. }
  194. /**
  195. * Creates the functions that returns the function with cached arguments. If count if passed arguments will be matched
  196. * to the arguments defined in `func` then function will be invoked.
  197. * Arguments are added to the stack in direction from the right to the left.
  198. *
  199. * @example
  200. * ```
  201. * var replace = curry(function(find, replace, string) {
  202. * return string.replace(find, replace);
  203. * });
  204. *
  205. * // returns function with bounded first argument
  206. * var replace = replace('Some test with foo...')
  207. *
  208. * // returns replaced string - all arguments was passed so function was invoked
  209. * replace('bar', 'foo');
  210. *
  211. * ```
  212. *
  213. * @param {Function} func Function to currying.
  214. * @returns {Function}
  215. */
  216. function curryRight(func) {
  217. var argsLength = func.length;
  218. function given(argsSoFar) {
  219. return function _curry() {
  220. for (var _len5 = arguments.length, params = Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
  221. params[_key5] = arguments[_key5];
  222. }
  223. var passedArgsSoFar = argsSoFar.concat(params.reverse());
  224. var result = void 0;
  225. if (passedArgsSoFar.length >= argsLength) {
  226. result = func.apply(this, passedArgsSoFar);
  227. } else {
  228. result = given(passedArgsSoFar);
  229. }
  230. return result;
  231. };
  232. }
  233. return given([]);
  234. }