UNPKG

chowdown

Version:

A JavaScript library that allows for the quick transformation of DOM documents into useful formats.

139 lines (113 loc) 6.87 kB
"use strict"; function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); } function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } var FollowQuery = require('./follow'); var _require = require('lodash'), flatten = _require.flatten, last = _require.last, isFunction = _require.isFunction; /** * When executed, this query will return a promise resolving to * the result of the inner query executed on multiple pages * reachable from the original document. * * @class PaginateQuery * @extends Query */ var PaginateQuery = /*#__PURE__*/ function (_FollowQuery) { _inherits(PaginateQuery, _FollowQuery); /** * Constructs a PaginateQuery given an inner query to execute on each document, * a URI query to find the uri of the next document and a function that * determines when to stop paginating. * * Also takes an additional object of configuration options. * * @param {Query} inner A query pointing to a URI for a different document. * @param {Query} uri The query to execute on the other document. * @param {object} [options] An object of additional configuration options. */ function PaginateQuery(inner, uri, max) { var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; _classCallCheck(this, PaginateQuery); options.max = max; return _possibleConstructorReturn(this, _getPrototypeOf(PaginateQuery).call(this, uri, inner, options)); } /** * Configures the PaginateQuery given an object of configuration options. * * If the inner query and URI query are not already Query objects, * then Query objects will be created from them. * * If the given max value is NOT a function but a number, * a function will be created that will return false (to stop) * after max pages have been perused. * * If no merge function is provided, the lodash flatten function will * be used to merge results. * * @param {object} options An object of configuration options. * @param {Query} options.uri A query pointing to a URI for a different document. * @param {Query} options.inner The query to execute on the other document. * @param {function|number} [options.max] The function used to determine when to stop. * @param {function} [options.merge] The function used to merge the results. */ _createClass(PaginateQuery, [{ key: "configure", value: function configure(options) { _get(_getPrototypeOf(PaginateQuery.prototype), "configure", this).call(this, options); this.options.merge = this.options.merge || flatten; if (!isFunction(this.options.max)) this.options.max = function (count) { return count < (options.max || Infinity); }; } /** * Executes the inner query on a document and recursively calls itself on the next document * located at the uri found in the current document. * * @param {Document} document The document containing the URI linking to the other document. * @param {any[]} [pages] The results accumulator. * @return {Promise<any>} A promise containing the paginated results. */ }, { key: "find", value: function find(document) { var _this = this; var pages = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; return this.options.inner.on(document).then(function (result) { return pages.push(result); }).then(function (count) { return _this.options.uri.on(document); }).then(function (uri) { return uri && _this.options.max(pages.length, pages) ? _this.next(uri).then(function (document) { return _this.find(document, pages); }) : pages; }); } /** * Merges the results using the merge function provided * at query creation. * * @param {any[]} pages An array of the page results. * @return {any} The merged page results. */ }, { key: "build", value: function build(pages) { return this.options.merge(pages); } }]); return PaginateQuery; }(FollowQuery); module.exports = PaginateQuery;