axiodb
Version:
The Pure JavaScript Alternative to SQLite. Embedded NoSQL database for Node.js with MongoDB-style queries, zero native dependencies, built-in InMemoryCache, and web GUI. Perfect for desktop apps, CLI tools, and embedded systems. No compilation, no platfor
82 lines • 3.67 kB
JavaScript
;
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 });
/* eslint-disable @typescript-eslint/no-explicit-any */
/**
* Class representing a sorting utility.
*/
class Sorting {
/**
* Create a Sorting instance.
* @param arr - The array to be sorted.
* @param query - The query object containing the sorting key and order.
*/
constructor(arr, query) {
this.arr = arr;
this.query = query;
}
/**
* Sort the array based on the query.
* Optimized for performance using native comparison operators.
* @param arr - The array to be sorted.
* @param query - The query object containing the sorting key and order.
* @returns A promise that resolves to the sorted array.
*/
sort(aditionalField) {
return __awaiter(this, void 0, void 0, function* () {
const [key, order] = Object.entries(this.query)[0]; // Extract the field and order (1 for ascending, -1 for descending)
if (aditionalField) {
// Optimized sort with direct subtraction for numbers and localeCompare for strings
return [...this.arr].sort((a, b) => {
const aVal = a[aditionalField][key];
const bVal = b[aditionalField][key];
// Fast path for numbers
if (typeof aVal === "number" && typeof bVal === "number") {
return (aVal - bVal) * order;
}
// Fast path for strings
if (typeof aVal === "string" && typeof bVal === "string") {
return aVal.localeCompare(bVal) * order;
}
// Fallback for other types
if (aVal < bVal)
return -order;
if (aVal > bVal)
return order;
return 0;
});
}
else {
// Optimized sort with direct subtraction for numbers and localeCompare for strings
return [...this.arr].sort((a, b) => {
const aVal = a[key];
const bVal = b[key];
// Fast path for numbers
if (typeof aVal === "number" && typeof bVal === "number") {
return (aVal - bVal) * order;
}
// Fast path for strings
if (typeof aVal === "string" && typeof bVal === "string") {
return aVal.localeCompare(bVal) * order;
}
// Fallback for other types
if (aVal < bVal)
return -order;
if (aVal > bVal)
return order;
return 0;
});
}
});
}
}
exports.default = Sorting;
//# sourceMappingURL=SortData.utils.js.map