UNPKG

booru

Version:

Search (and do other things) on a bunch of different boorus!

136 lines 4.57 kB
"use strict"; /** * @packageDocumentation * @module Structures */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const Utils = __importStar(require("../Utils")); /** * Represents a page of search results, works like an array of {@link Post} * <p> Usable like an array and allows to easily get the next page * * @example * ``` * const Booru = require('booru') * // Safebooru * const sb = new Booru('sb') * * const imgs = await sb.search('cat') * * // Log the images from the first page, then from the second * imgs.forEach(i => console.log(i.postView)) * const imgs2 = await imgs.nextPage() * imgs2.forEach(i => console.log(i.postView)) * ``` */ class SearchResults extends Array { /** The booru used for this search */ booru; /** The page of this search */ page; /** The tags used for this search */ tags; /** The options used for this search */ options; /** The posts from this search result */ posts; /** @private */ constructor(posts, tags, options, booru) { super(posts.length); for (let i = 0; i < posts.length; i++) { this[i] = posts[i]; } this.posts = posts; this.tags = tags; this.options = options; this.booru = booru; this.page = options ? (options.page ?? 0) : 0; } /** * Get the first post in this result set * @return {Post} */ get first() { return this[0]; } /** * Get the last post in this result set * @return {Post} */ get last() { return this[this.length - 1]; } /** * Get the next page * <p>Works like <code>sb.search('cat', {page: 1}); sb.search('cat', {page: 2})</code> * @return {Promise<SearchResults>} */ nextPage() { const opts = this.options; opts.page = this.page + 1; return this.booru.search(this.tags, opts); } /** * Create a new SearchResults with just images with the matching tags * * @param {String[]|String} tags The tags (or tag) to search for * @param {Object} options The extra options for the search * @param {Boolean} [options.invert=false] If the results should be inverted and * return images *not* tagged * @return {SearchResults} */ tagged(tags, { invert = false } = {}) { const searchTags = Array.isArray(tags) ? tags : [tags]; const posts = []; for (const p of this) { const m = Utils.compareArrays(searchTags, p.tags).length; if ((!invert && m > 0) || (invert && m === 0)) { posts.push(p); } } return new SearchResults(posts, this.tags, this.options, this.booru); } /** * Returns a SearchResults with images *not* tagged with any of the specified tags (or tag) * @param {String[]|String} tags The tags (or tag) to blacklist * @return {SearchResults} The results without any images with the specified tags */ blacklist(tags) { return this.tagged(tags, { invert: true }); } } exports.default = SearchResults; //# sourceMappingURL=SearchResults.js.map