UNPKG

@azure/cosmos

Version:
118 lines (117 loc) 3.74 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 orderByComparator_exports = {}; __export(orderByComparator_exports, { OrderByComparator: () => OrderByComparator, compareOrderByItems: () => compareOrderByItems }); module.exports = __toCommonJS(orderByComparator_exports); function compareOrderByItems(orderByItemsA, orderByItemsB, sortOrders) { const comparator = new OrderByComparator(sortOrders); const docA = { orderByItems: orderByItemsA, payload: null }; const docB = { orderByItems: orderByItemsB, payload: null }; return comparator.compareItems(docA, docB); } const TYPEORDCOMPARATOR = Object.freeze({ NoValue: { ord: 0 }, undefined: { ord: 1 }, boolean: { ord: 2, compFunc: (a, b) => { return a === b ? 0 : a > b ? 1 : -1; } }, number: { ord: 4, compFunc: (a, b) => { return a === b ? 0 : a > b ? 1 : -1; } }, string: { ord: 5, compFunc: (a, b) => { return a === b ? 0 : a > b ? 1 : -1; } } }); class OrderByComparator { constructor(sortOrder) { this.sortOrder = sortOrder; } compareItems(a, b) { const orderByItemsRes1 = this.getOrderByItems(a); const orderByItemsRes2 = this.getOrderByItems(b); for (let i = 0; i < orderByItemsRes1.length; i++) { const compRes = this.compareOrderByItem(orderByItemsRes1[i], orderByItemsRes2[i]); if (compRes !== 0) { if (this.sortOrder[i] === "Descending") { return compRes; } else if (this.sortOrder[i] === "Ascending") { return -compRes; } } } return 0; } getOrderByItems(res) { return res["orderByItems"]; } compareOrderByItem(orderByItem1, orderByItem2) { const type1 = this.getType(orderByItem1); const type2 = this.getType(orderByItem2); return this.compareValue(orderByItem1["item"], type1, orderByItem2["item"], type2); } getType(orderByItem) { if (orderByItem === void 0 || orderByItem.item === void 0) { return "NoValue"; } const type = typeof orderByItem.item; if (TYPEORDCOMPARATOR[type] === void 0) { throw new Error(`unrecognizable type ${type}`); } return type; } compareValue(item1, type1, item2, type2) { if (type1 === "object" || type2 === "object") { throw new Error("Tried to compare an object type"); } const type1Ord = TYPEORDCOMPARATOR[type1].ord; const type2Ord = TYPEORDCOMPARATOR[type2].ord; const typeCmp = type1Ord - type2Ord; if (typeCmp !== 0) { return typeCmp; } if (type1Ord === TYPEORDCOMPARATOR["undefined"].ord || type1Ord === TYPEORDCOMPARATOR["NoValue"].ord) { return 0; } const compFunc = TYPEORDCOMPARATOR[type1].compFunc; if (typeof compFunc === "undefined") { throw new Error("Cannot find the comparison function"); } return compFunc(item1, item2); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { OrderByComparator, compareOrderByItems });