rm-infinity
Advanced tools
+3
-2
| { | ||
| "name": "rm-infinity", | ||
| "version": "0.0.7", | ||
| "version": "0.0.8", | ||
| "description": "Sorted data from multiple paginated sources", | ||
@@ -9,6 +9,7 @@ "main": "lib/index.js", | ||
| "test": "npm run build && jest --config jestconfig.json", | ||
| "clean:build": "rm -rf ./lib && npm run build", | ||
| "build": "tsc", | ||
| "format": "prettier --write \"src/**/*.ts\"", | ||
| "lint": "tslint -p tsconfig.json", | ||
| "prepare": "npm run build", | ||
| "prepare": "npm run clean:build", | ||
| "prepublishOnly": "npm test && npm run lint", | ||
@@ -15,0 +16,0 @@ "preversion": "npm run lint", |
| import { PaginationEngineConfig, PaginationConfig, PaginationResult } from './types'; | ||
| export declare class PaginationEngine { | ||
| private config; | ||
| constructor(config?: PaginationEngineConfig); | ||
| getNext(configs: PaginationConfig<any>[]): Promise<PaginationResult>; | ||
| createMomoizedNext(configs: PaginationConfig<any>[]): () => Promise<PaginationResult>; | ||
| } |
| "use strict"; | ||
| var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
| function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
| return new (P || (P = Promise))(function (resolve, reject) { | ||
| function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
| function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
| function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
| step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
| }); | ||
| }; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const minmax_1 = require("./minmax"); | ||
| const DEFAULT_CONFIG = { ascending: false }; | ||
| class PaginationEngine { | ||
| constructor(config) { | ||
| this.config = Object.assign(Object.assign({}, DEFAULT_CONFIG), config); | ||
| } | ||
| getNext(configs) { | ||
| return __awaiter(this, void 0, void 0, function* () { | ||
| const queries = configs.map((config) => config.query(config.offset).then((data) => ({ config, data }))); | ||
| const fetchedQueries = yield Promise.all(queries); | ||
| const min = minmax_1.getMin(fetchedQueries, this.config.ascending); | ||
| const max = minmax_1.getMax(fetchedQueries, this.config.ascending); | ||
| const returnObjects = []; | ||
| const newOffsets = []; | ||
| for (const fetchedQuery of fetchedQueries) { | ||
| let offsetAddition = 0; | ||
| for (const queryData of fetchedQuery.data) { | ||
| if (fetchedQuery.config.sortOn(queryData) <= max && fetchedQuery.config.sortOn(queryData) >= min) { | ||
| returnObjects.push({ | ||
| sortValue: fetchedQuery.config.sortOn(queryData), | ||
| data: queryData, | ||
| }); | ||
| offsetAddition++; | ||
| } | ||
| } | ||
| newOffsets.push({ | ||
| name: fetchedQuery.config.name, | ||
| value: fetchedQuery.config.offset + offsetAddition, | ||
| }); | ||
| } | ||
| returnObjects.sort((o1, o2) => o2.sortValue - o1.sortValue); | ||
| const response = returnObjects.map((returnObject) => returnObject.data); | ||
| return { | ||
| data: this.config.ascending ? response.reverse() : response, | ||
| newOffsets, | ||
| }; | ||
| }); | ||
| } | ||
| createMomoizedNext(configs) { | ||
| const nextConfigs = configs; | ||
| return () => __awaiter(this, void 0, void 0, function* () { | ||
| const result = yield this.getNext(nextConfigs); | ||
| result.newOffsets.forEach((offset) => (nextConfigs.find((oldConfig) => oldConfig.name === offset.name).offset = offset.value)); | ||
| return result; | ||
| }); | ||
| } | ||
| } | ||
| exports.PaginationEngine = PaginationEngine; |
15088
-17.84%13
-13.33%223
-22.84%