UnderscoreExt.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2017, 2018
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['underscore'], function (_) {
  8. /**
  9. * Get the difference between 2 array of obejcts based on the search key.
  10. * @param aObject1 array of object 1
  11. * @param aObject2 array of object 2
  12. * @param searchkey used to identify entries
  13. * @return an array of object from aObject1 but doesn't present the same value in aObject2.
  14. * e.g.
  15. * aObject1 = [
  16. * {'displayValue': 'a', 'useValue': 'a'},
  17. * {'displayValue': 'b', 'useValue': 'b'},
  18. * {'displayValue': 'c', 'useValue': 'c'},
  19. * {'displayValue': 'd', 'useValue': 'd'}
  20. * ];
  21. * aObject2 = [
  22. * {'displayValue': 'a', 'useValue': 'a'},
  23. * {'displayValue': 'b', 'useValue': 'b'},
  24. * {'displayValue': 'c', 'useValue': 'c'}
  25. * ]
  26. *
  27. * The result will be [{'displayValue': 'd', 'useValue': 'd'}]
  28. */
  29. _.objDifference = function (aObject1, aObject2, searchkey) {
  30. return aObject1.filter(function (current1) {
  31. return aObject2.filter(function (current2) {
  32. return current1[searchkey] === current2[searchkey];
  33. }).length === 0;
  34. });
  35. };
  36. /* implementation of debounce that calls the provided function a maximum of once per 'time' ms
  37. * 'fn' will be called immediately and then after 'time' of not getting called.
  38. *
  39. * if wrappedFn is called once, fn is called once immediately.
  40. * if wrappedFn is called more once in rapid succession (each call within 'time' ms of the last, fn is called twice:
  41. * once immediately.
  42. * a second time 'time' ms after the last invocation.
  43. *
  44. * this differs from the standard underscore version in the fact that fn is called first.
  45. */
  46. _.doubleDebounce = function (fn, time) {
  47. var timeout = null;
  48. var lastCall = 0;
  49. var later = function later(args) {
  50. timeout = null;
  51. lastCall = Date.now();
  52. fn(args);
  53. };
  54. var wrappedFn = function wrappedFn(args) {
  55. // more than 'time' since last call and nothing scheduled, so just call fn
  56. if (Date.now() - lastCall > time && !timeout) {
  57. lastCall = Date.now();
  58. fn(args);
  59. } else {
  60. // less than 'time' since last call or 'later' is already scheduled.
  61. // push the callback to the future.
  62. clearTimeout(timeout);
  63. timeout = setTimeout(later, time, args);
  64. }
  65. };
  66. wrappedFn.cancel = function () {
  67. return clearTimeout(timeout);
  68. };
  69. return wrappedFn;
  70. };
  71. });
  72. //# sourceMappingURL=UnderscoreExt.js.map