typedash
Version:
modern, type-safe collection of utility functions
47 lines (42 loc) • 1.24 kB
JavaScript
;
// src/functions/isArray/isArray.ts
var isArray = Array.isArray;
// src/functions/castArrayIfDefined/castArrayIfDefined.ts
function castArrayIfDefined(value) {
if (value == null) {
return value;
}
if (isArray(value)) {
return value;
}
return [value];
}
// src/functions/castArray/castArray.ts
function castArray(value) {
return castArrayIfDefined(value ?? []);
}
// src/functions/orderBy/orderBy.ts
function orderBy(array, iterators, orders) {
if (array == null)
return [];
const normalizedIteratees = castArray(iterators).map(
(iteratee) => typeof iteratee === "function" ? iteratee : (value) => value[iteratee]
);
const normalizedOrders = castArray(orders);
return [...array].sort((a, b) => {
for (const [index, iteratee] of normalizedIteratees.entries()) {
const normalizedOrder = normalizedOrders[index] ?? "asc";
const order = normalizedOrder === "desc" ? -1 : 1;
const aValue = iteratee(a);
const bValue = iteratee(b);
if (aValue < bValue)
return -1 * order;
if (aValue > bValue)
return 1 * order;
}
return 0;
});
}
exports.orderBy = orderBy;
//# sourceMappingURL=out.js.map
//# sourceMappingURL=index.cjs.map