chowdown
Version:
A JavaScript library that allows for the quick transformation of DOM documents into useful formats.
129 lines (102 loc) • 6.9 kB
JavaScript
;
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 Promise = require('bluebird');
var Query = require('./');
/**
* When executed, this query will return a promise resolving to
* an array of values such that each value is the result of an inner query
* executed on a child document.
*
* @class CollectionQuery
* @extends Query
*/
var CollectionQuery =
/*#__PURE__*/
function (_Query) {
_inherits(CollectionQuery, _Query);
/**
* Constructs a CollectionQuery given a selector for a list of child documents
* and an inner query that describes a value to pick from each child document.
*
* Also takes an object of additional configuration options.
*
* @param {string} selector The selector for the children in the document.
* @param {Query} inner A query representing what to pick from each child document.
* @param {object} [options] An object of further configuration options.
* @param {any} [options.default=[]] The default value this query will resolve to if no child documents are found.
* @param {function} [options.filter] A function used to filter the resulting array.
*/
function CollectionQuery(selector, inner) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
_classCallCheck(this, CollectionQuery);
options.inner = inner;
return _possibleConstructorReturn(this, _getPrototypeOf(CollectionQuery).call(this, selector, options));
}
/**
* Configures the CollectionQuery given an object of configuration options.
*
* By default, the default value a CollectionQuery will resolve to is an empty array ([]).
*
* The CollectionQuery supports a filter option. This is expected to be a function that
* is called for every item in the array and where this function does not return
* a truthy value, then the corresponding item is omitted.
*
* @param {object} options An object of further configuration options.
* @param {Query} options.inner The inner query representing what to pick from each child document.
* @param {any} [options.default=[]] The default value this query will resolve to if no child documents are found.
* @param {function} [options.filter] A function used to filter the resulting array.
*/
_createClass(CollectionQuery, [{
key: "configure",
value: function configure(options) {
var _this = this;
_get(_getPrototypeOf(CollectionQuery.prototype), "configure", this).call(this, options);
this.options.inner = Query.factory(this.options.inner);
if (!this.options.hasOwnProperty('default')) this.options["default"] = [];
if (this.options.hasOwnProperty('filter')) this.options.format.push(function (list) {
return list.filter(_this.options.filter);
});
}
/**
* Locates the array of child documents in the given document and attempts to
* execute the inner query on each one.
*
* @param {Document} document The document to locate the array of containers in.
* @return {Promise<any>[]} An array of promises resolving to inner querys picked from each child document.
*/
}, {
key: "find",
value: function find(document) {
var _this2 = this;
var children = document.children(this.options.selector);
if (children !== undefined) return children.map(function (child) {
return _this2.options.inner.on(child);
});
}
/**
* Given an array of promises, this method simply returns a promise that is
* fulfilled when all promises in the array have been fulfilled.
*
* @param {Promise<any>[]} array An array of inner query promises.
* @param {Document} document The document the children were retrieved from.
* @return {Promise} A promise that is fulfilled when all the items in the array are fulfilled.
*/
}, {
key: "build",
value: function build(array, document) {
return Promise.all(array);
}
}]);
return CollectionQuery;
}(Query);
module.exports = CollectionQuery;