declarative-js
Version:
_declarative-js_ is modern JavaScript library, that helps to: - tackle array transformation with built in JavaScript array api (e.g. `array.filter(toBe.unique())`), - provide a type-level solution for representing optional values instead of null referen
69 lines (68 loc) • 2.74 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var sort_1 = require("../internal/sort");
/**
* Functions to be used in {@link Array.prototype.sort} as a callback.
* @see https://pavel-surinin.github.io/declarativejs/#/?id=sorters
*/
var Sort;
(function (Sort) {
function ascendingBy() {
var getters = [];
for (var _i = 0; _i < arguments.length; _i++) {
getters[_i] = arguments[_i];
}
var ascending = { true: 1, false: -1 };
var sort = typeof getters[0] === 'string'
? sort_1.sortByKeyValues(ascending).apply(void 0, getters) : sort_1.sortByGetters(ascending).apply(void 0, getters);
return function _ascendingBy(a, b) {
return sort(a, b);
};
}
Sort.ascendingBy = ascendingBy;
function descendingBy() {
var getters = [];
for (var _i = 0; _i < arguments.length; _i++) {
getters[_i] = arguments[_i];
}
return function _descendingBy(a, b) {
var descending = { true: -1, false: 1 };
var sort = typeof getters[0] === 'string'
? sort_1.sortByKeyValues(descending).apply(void 0, getters) : sort_1.sortByGetters(descending).apply(void 0, getters);
return sort(a, b);
};
}
Sort.descendingBy = descendingBy;
function by() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var sort = typeof args[0] === 'string'
? sort_1.sortByPropertyAndPriority(args[0], args[1])
: sort_1.sortByConditions.apply(void 0, args);
return function _by(a, b) {
return sort(a, b);
};
}
Sort.by = by;
/**
* Functions to be used in {@link Array.prototype.sort} as a callback.
* Function that will sort items in array, by provided order.
* It accepts as a parameter array of custom order rule.
* Element, that are not present in order array will be at he the end of the sorted list.
* @param order array of custom order of items that are being sorted.
* @returns comparator for Array.prototype.sort function.
* @see https://pavel-surinin.github.io/declarativejs/#/?id=orderedby
*/
function orderedBy(order) {
return function _orderedBy(a, b) {
var condition = {
toValue: function (x) { return x; },
order: order
};
return sort_1.sortByConditions.apply(void 0, [condition])(a, b);
};
}
Sort.orderedBy = orderedBy;
})(Sort = exports.Sort || (exports.Sort = {}));