fast-sort-lens
Version:
API wrapper around fast-sort
32 lines (31 loc) • 841 B
JavaScript
// src/index.ts
import { sort } from "fast-sort";
import { sort as sort2 } from "fast-sort";
function fastOrderBy(list, props, orders) {
if (props.length !== orders.length) {
throw new Error("props & orders length not match");
}
const _by = props.map((prop, index) => {
const order = orders[index];
if (order === "asc") return { asc: prop };
else if (order === "desc") return { desc: prop };
return {
asc: prop,
comparer: order
};
});
return sort2(list).by(_by);
}
function fastSortWithOrders(list, orders) {
const _by = orders.map(({ order, prop }) => {
if (order === "asc") return { asc: prop };
else if (order === "desc") return { desc: prop };
return { asc: prop, comparer: order };
});
return sort2(list).by(_by);
}
export {
fastOrderBy,
fastSortWithOrders,
sort
};