lodash.min_a21c3813.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. (function (global) {
  2. global['_'] = {};
  3. // A (possibly faster) way to get the current timestamp as an integer.
  4. _.now = Date.now || function () {
  5. return new Date().getTime();
  6. };
  7. _.throttle = function (func, wait, options) {
  8. var timeout, context, args, result;
  9. var previous = 0;
  10. if (!options) options = {};
  11. var later = function () {
  12. previous = options.leading === false ? 0 : _.now();
  13. timeout = null;
  14. result = func.apply(context, args);
  15. if (!timeout) context = args = null;
  16. };
  17. var throttled = function () {
  18. var now = _.now();
  19. if (!previous && options.leading === false) previous = now;
  20. var remaining = wait - (now - previous);
  21. context = this;
  22. args = arguments;
  23. if (remaining <= 0 || remaining > wait) {
  24. if (timeout) {
  25. clearTimeout(timeout);
  26. timeout = null;
  27. }
  28. previous = now;
  29. result = func.apply(context, args);
  30. if (!timeout) context = args = null;
  31. } else if (!timeout && options.trailing !== false) {
  32. timeout = setTimeout(later, remaining);
  33. }
  34. return result;
  35. };
  36. throttled.cancel = function () {
  37. clearTimeout(timeout);
  38. previous = 0;
  39. timeout = context = args = null;
  40. };
  41. return throttled;
  42. };
  43. _.shuffle = function (array) {
  44. const length = array == null ? 0 : array.length
  45. if (!length) {
  46. return []
  47. }
  48. let index = -1
  49. const lastIndex = length - 1
  50. const result = copyArray(array)
  51. while (++index < length) {
  52. const rand = index + Math.floor(Math.random() * (lastIndex - index + 1))
  53. const value = result[rand]
  54. result[rand] = result[index]
  55. result[index] = value
  56. }
  57. return result
  58. }
  59. })(window)