index-array-by.module.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. function _objectWithoutPropertiesLoose(source, excluded) {
  2. if (source == null) return {};
  3. var target = {};
  4. var sourceKeys = Object.keys(source);
  5. var key, i;
  6. for (i = 0; i < sourceKeys.length; i++) {
  7. key = sourceKeys[i];
  8. if (excluded.indexOf(key) >= 0) continue;
  9. target[key] = source[key];
  10. }
  11. return target;
  12. }
  13. function _objectWithoutProperties(source, excluded) {
  14. if (source == null) return {};
  15. var target = _objectWithoutPropertiesLoose(source, excluded);
  16. var key, i;
  17. if (Object.getOwnPropertySymbols) {
  18. var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
  19. for (i = 0; i < sourceSymbolKeys.length; i++) {
  20. key = sourceSymbolKeys[i];
  21. if (excluded.indexOf(key) >= 0) continue;
  22. if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
  23. target[key] = source[key];
  24. }
  25. }
  26. return target;
  27. }
  28. function _slicedToArray(arr, i) {
  29. return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
  30. }
  31. function _toConsumableArray(arr) {
  32. return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
  33. }
  34. function _arrayWithoutHoles(arr) {
  35. if (Array.isArray(arr)) return _arrayLikeToArray(arr);
  36. }
  37. function _arrayWithHoles(arr) {
  38. if (Array.isArray(arr)) return arr;
  39. }
  40. function _iterableToArray(iter) {
  41. if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
  42. }
  43. function _iterableToArrayLimit(arr, i) {
  44. var _i = arr && (typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]);
  45. if (_i == null) return;
  46. var _arr = [];
  47. var _n = true;
  48. var _d = false;
  49. var _s, _e;
  50. try {
  51. for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
  52. _arr.push(_s.value);
  53. if (i && _arr.length === i) break;
  54. }
  55. } catch (err) {
  56. _d = true;
  57. _e = err;
  58. } finally {
  59. try {
  60. if (!_n && _i["return"] != null) _i["return"]();
  61. } finally {
  62. if (_d) throw _e;
  63. }
  64. }
  65. return _arr;
  66. }
  67. function _unsupportedIterableToArray(o, minLen) {
  68. if (!o) return;
  69. if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  70. var n = Object.prototype.toString.call(o).slice(8, -1);
  71. if (n === "Object" && o.constructor) n = o.constructor.name;
  72. if (n === "Map" || n === "Set") return Array.from(o);
  73. if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
  74. }
  75. function _arrayLikeToArray(arr, len) {
  76. if (len == null || len > arr.length) len = arr.length;
  77. for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
  78. return arr2;
  79. }
  80. function _nonIterableSpread() {
  81. throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  82. }
  83. function _nonIterableRest() {
  84. throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
  85. }
  86. function _toPrimitive(input, hint) {
  87. if (typeof input !== "object" || input === null) return input;
  88. var prim = input[Symbol.toPrimitive];
  89. if (prim !== undefined) {
  90. var res = prim.call(input, hint || "default");
  91. if (typeof res !== "object") return res;
  92. throw new TypeError("@@toPrimitive must return a primitive value.");
  93. }
  94. return (hint === "string" ? String : Number)(input);
  95. }
  96. function _toPropertyKey(arg) {
  97. var key = _toPrimitive(arg, "string");
  98. return typeof key === "symbol" ? key : String(key);
  99. }
  100. var index = (function () {
  101. var list = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  102. var keyAccessors = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  103. var multiItem = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
  104. var flattenKeys = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  105. var keys = (keyAccessors instanceof Array ? keyAccessors.length ? keyAccessors : [undefined] : [keyAccessors]).map(function (key) {
  106. return {
  107. keyAccessor: key,
  108. isProp: !(key instanceof Function)
  109. };
  110. });
  111. var indexedResult = list.reduce(function (res, item) {
  112. var iterObj = res;
  113. var itemVal = item;
  114. keys.forEach(function (_ref, idx) {
  115. var keyAccessor = _ref.keyAccessor,
  116. isProp = _ref.isProp;
  117. var key;
  118. if (isProp) {
  119. var _itemVal = itemVal,
  120. propVal = _itemVal[keyAccessor],
  121. rest = _objectWithoutProperties(_itemVal, [keyAccessor].map(_toPropertyKey));
  122. key = propVal;
  123. itemVal = rest;
  124. } else {
  125. key = keyAccessor(itemVal, idx);
  126. }
  127. if (idx + 1 < keys.length) {
  128. if (!iterObj.hasOwnProperty(key)) {
  129. iterObj[key] = {};
  130. }
  131. iterObj = iterObj[key];
  132. } else {
  133. // Leaf key
  134. if (multiItem) {
  135. if (!iterObj.hasOwnProperty(key)) {
  136. iterObj[key] = [];
  137. }
  138. iterObj[key].push(itemVal);
  139. } else {
  140. iterObj[key] = itemVal;
  141. }
  142. }
  143. });
  144. return res;
  145. }, {});
  146. if (multiItem instanceof Function) {
  147. // Reduce leaf multiple values
  148. (function reduce(node) {
  149. var level = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  150. if (level === keys.length) {
  151. Object.keys(node).forEach(function (k) {
  152. return node[k] = multiItem(node[k]);
  153. });
  154. } else {
  155. Object.values(node).forEach(function (child) {
  156. return reduce(child, level + 1);
  157. });
  158. }
  159. })(indexedResult); // IIFE
  160. }
  161. var result = indexedResult;
  162. if (flattenKeys) {
  163. // flatten into array
  164. result = [];
  165. (function flatten(node) {
  166. var accKeys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  167. if (accKeys.length === keys.length) {
  168. result.push({
  169. keys: accKeys,
  170. vals: node
  171. });
  172. } else {
  173. Object.entries(node).forEach(function (_ref2) {
  174. var _ref3 = _slicedToArray(_ref2, 2),
  175. key = _ref3[0],
  176. val = _ref3[1];
  177. return flatten(val, [].concat(_toConsumableArray(accKeys), [key]));
  178. });
  179. }
  180. })(indexedResult); //IIFE
  181. if (keyAccessors instanceof Array && keyAccessors.length === 0 && result.length === 1) {
  182. // clear keys if there's no key accessors (single result)
  183. result[0].keys = [];
  184. }
  185. }
  186. return result;
  187. });
  188. export default index;