5228c0bb72bf29ef3f12962f6628a4d288f23f2fe3fb9bff348175e432836dfb47fd588d755e6cad6530a8e4e7ce5022855b2229763818b781080af83fb1f5 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import {arrayEach, arrayReduce, arrayMap, arrayMax} from './../helpers/array';
  2. import {defineGetter} from './../helpers/object';
  3. import {rangeEach} from './../helpers/number';
  4. const MIXIN_NAME = 'arrayMapper';
  5. /**
  6. * @type {Object}
  7. */
  8. const arrayMapper = {
  9. _arrayMap: [],
  10. /**
  11. * Get value by map index.
  12. *
  13. * @param {Number} index Array index.
  14. * @return {*} Returns value mapped to passed index.
  15. */
  16. getValueByIndex(index) {
  17. let value;
  18. /* eslint-disable no-cond-assign */
  19. return (value = this._arrayMap[index]) === void 0 ? null : value;
  20. },
  21. /**
  22. * Get map index by its value.
  23. *
  24. * @param {*} value Value to search.
  25. * @returns {Number} Returns array index.
  26. */
  27. getIndexByValue(value) {
  28. let index;
  29. /* eslint-disable no-cond-assign */
  30. return (index = this._arrayMap.indexOf(value)) === -1 ? null : index;
  31. },
  32. /**
  33. * Insert new items to array mapper starting at passed index. New entries will be a continuation of last value in the array.
  34. *
  35. * @param {Number} index Array index.
  36. * @param {Number} [amount=1] Defines how many items will be created to an array.
  37. * @returns {Array} Returns added items.
  38. */
  39. insertItems(index, amount = 1) {
  40. let newIndex = arrayMax(this._arrayMap) + 1;
  41. let addedItems = [];
  42. rangeEach(amount - 1, (count) => {
  43. addedItems.push(this._arrayMap.splice(index + count, 0, newIndex + count));
  44. });
  45. return addedItems;
  46. },
  47. /**
  48. * Remove items from array mapper.
  49. *
  50. * @param {Number} index Array index.
  51. * @param {Number} [amount=1] Defines how many items will be created to an array.
  52. * @returns {Array} Returns removed items.
  53. */
  54. removeItems(index, amount = 1) {
  55. let removedItems = [];
  56. if (Array.isArray(index)) {
  57. let mapCopy = [].concat(this._arrayMap);
  58. // Sort descending
  59. index.sort((a, b) => b - a);
  60. removedItems = arrayReduce(index, (acc, item) => {
  61. this._arrayMap.splice(item, 1);
  62. return acc.concat(mapCopy.slice(item, item + 1));
  63. }, []);
  64. } else {
  65. removedItems = this._arrayMap.splice(index, amount);
  66. }
  67. return removedItems;
  68. },
  69. /**
  70. * Unshift items (remove and shift chunk of array to the left).
  71. *
  72. * @param {Number|Array} index Array index or Array of indexes to unshift.
  73. * @param {Number} [amount=1] Defines how many items will be removed from an array (when index is passed as number).
  74. */
  75. unshiftItems(index, amount = 1) {
  76. let removedItems = this.removeItems(index, amount);
  77. function countRowShift(logicalRow) {
  78. // Todo: compare perf between reduce vs sort->each->brake
  79. return arrayReduce(removedItems, (count, removedLogicalRow) => {
  80. if (logicalRow > removedLogicalRow) {
  81. count++;
  82. }
  83. return count;
  84. }, 0);
  85. }
  86. this._arrayMap = arrayMap(this._arrayMap, (logicalRow, physicalRow) => {
  87. let rowShift = countRowShift(logicalRow);
  88. if (rowShift) {
  89. logicalRow -= rowShift;
  90. }
  91. return logicalRow;
  92. });
  93. },
  94. /**
  95. * Shift (right shifting) items starting at passed index.
  96. *
  97. * @param {Number} index Array index.
  98. * @param {Number} [amount=1] Defines how many items will be created to an array.
  99. */
  100. shiftItems(index, amount = 1) {
  101. this._arrayMap = arrayMap(this._arrayMap, (row) => {
  102. if (row >= index) {
  103. row += amount;
  104. }
  105. return row;
  106. });
  107. rangeEach(amount - 1, (count) => {
  108. this._arrayMap.splice(index + count, 0, index + count);
  109. });
  110. },
  111. /**
  112. * Clear all stored index<->value information from an array.
  113. */
  114. clearMap() {
  115. this._arrayMap.length = 0;
  116. }
  117. };
  118. defineGetter(arrayMapper, 'MIXIN_NAME', MIXIN_NAME, {
  119. writable: false,
  120. enumerable: false,
  121. });
  122. export default arrayMapper;