index-array-by.js 7.3 KB

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