UNPKG

@colyseus/redis-driver

Version:

<div align="center"> <a href="https://github.com/colyseus/colyseus"> <img src="media/logo.svg?raw=true" width="60%" height="300" /> </a> <br> <br> <a href="https://npmjs.com/package/colyseus"> <img src="https://img.shields.io/npm/dm/coly

77 lines (76 loc) 2.44 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var Query_exports = {}; __export(Query_exports, { Query: () => Query }); module.exports = __toCommonJS(Query_exports); class Query { constructor(rooms, conditions) { this.order = /* @__PURE__ */ new Map(); this.conditions = conditions; this.rooms = rooms; } sort(options) { this.order.clear(); const fields = Object.entries(options); if (fields.length) { for (const [field, direction] of fields) { if (direction === 1 || direction === "asc" || direction === "ascending") { this.order.set(field, 1); } else { this.order.set(field, -1); } } } return this; } then(resolve, reject) { return this.rooms.then((rooms) => { if (this.order.size) { rooms.sort((room1, room2) => { for (const [field, direction] of this.order) { if (direction === 1) { if (room1[field] > room2[field]) return 1; if (room1[field] < room2[field]) return -1; } else { if (room1[field] > room2[field]) return -1; if (room1[field] < room2[field]) return 1; } } }); } let conditions = Object.entries(this.conditions); let withConditions = conditions.length > 0; return resolve(rooms.find((room) => { if (withConditions) { for (let [field, value] of conditions) { if (room[field] !== value) { return false; } } } return true; })); }); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Query });