reshape.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
  2. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  3. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  4. import { flatten } from '@algolia/autocomplete-shared';
  5. export function reshape(_ref) {
  6. var collections = _ref.collections,
  7. props = _ref.props,
  8. state = _ref.state;
  9. // Sources are grouped by `sourceId` to conveniently pick them via destructuring.
  10. // Example: `const { recentSearchesPlugin } = sourcesBySourceId`
  11. var sourcesBySourceId = collections.reduce(function (acc, collection) {
  12. return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, collection.source.sourceId, _objectSpread(_objectSpread({}, collection.source), {}, {
  13. getItems: function getItems() {
  14. // We provide the resolved items from the collection to the `reshape` prop.
  15. return flatten(collection.items);
  16. }
  17. })));
  18. }, {});
  19. var reshapeSources = props.reshape({
  20. sources: Object.values(sourcesBySourceId),
  21. sourcesBySourceId: sourcesBySourceId,
  22. state: state
  23. }); // We reconstruct the collections with the items modified by the `reshape` prop.
  24. return flatten(reshapeSources).filter(Boolean).map(function (source) {
  25. return {
  26. source: source,
  27. items: source.getItems()
  28. };
  29. });
  30. }