get-queries-for-element.js 1.8 KB

123456789101112131415161718192021222324252627282930
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getQueriesForElement = getQueriesForElement;
  6. var defaultQueries = _interopRequireWildcard(require("./queries"));
  7. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  8. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  9. /**
  10. * @typedef {{[key: string]: Function}} FuncMap
  11. */
  12. /**
  13. * @param {HTMLElement} element container
  14. * @param {FuncMap} queries object of functions
  15. * @param {Object} initialValue for reducer
  16. * @returns {FuncMap} returns object of functions bound to container
  17. */
  18. function getQueriesForElement(element, queries = defaultQueries, initialValue = {}) {
  19. return Object.keys(queries).reduce((helpers, key) => {
  20. const fn = queries[key];
  21. helpers[key] = fn.bind(null, element);
  22. return helpers;
  23. }, initialValue);
  24. }