index-array-by.common.js 6.5 KB

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