UNPKG

chowdown

Version:

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

90 lines (73 loc) 2.62 kB
"use strict"; 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; } var _require = require('lodash'), extendWith = _require.extendWith; /** * A class that wraps a document and allows for easy creation * and execution of different types of queries. * * @class Scope */ var Scope = /*#__PURE__*/ function () { /** * Constructs a Scope given a document to act as the context * for all queries. * * @param {Document} Document The document to wrap. */ function Scope(document) { _classCallCheck(this, Scope); this.options = {}; this.options.document = document; } /** * Returns the Document used by this scope. * * @return {Document} The Document used in this scope. */ _createClass(Scope, [{ key: "document", value: function document() { return this.options.document; } /** * Executes a given Query within the context of this scope. * * @param {Query} query The query to execute. * @return {Promise<any>} The result of the query. */ }, { key: "execute", value: function execute(query) { return query.on(this.options.document); } }]); return Scope; }(); module.exports = Scope; /** * A factory function that creates a Scope given a Document. * If a Scope is passed instead of a document, then that will * be returned instead. * * @param {Document|Scope} document The document to wrap. * @return {Scope} The created Scope. */ Scope.factory = function (document) { if (document instanceof Scope) return document; return new Scope(document); }; // Late load the query class to avoid circular dependency issues. var Query = require('./query'); /** * Create a method on the Scope class for each type of query that when called * will create and execute that query. */ extendWith(Scope.prototype, Query.factory, function (_, fn) { return function () { return this.execute(fn.apply(void 0, arguments)); }; });