node-red-contrib-home-assistant-websocket
Version:
Node-RED integration with Home Assistant through websocket and REST API
60 lines (59 loc) • 2.11 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.byPropertiesOf = byPropertiesOf;
exports.sort = sort;
/**
* Returns a comparator for objects of type T that can be used by sort
* functions, were T objects are compared by the specified T properties.
*
* @param sortBy - the names of the properties to sort by, in precedence order.
* Prefix any name with `-` to sort it in descending order.
*/
function byPropertiesOf(sortBy) {
function compareByProperty(arg) {
let key;
let sortOrder = 1;
if (typeof arg === 'string' && arg.startsWith('-')) {
sortOrder = -1;
// Typescript is not yet smart enough to infer that substring is keyof T
key = arg.substring(1);
}
else {
// Likewise it is not yet smart enough to infer that arg is not keyof T
key = arg;
}
return function (a, b) {
const aValue = a[key];
const bValue = b[key];
const strings = typeof aValue === 'string' && typeof bValue === 'string';
const result = strings
? aValue.localeCompare(bValue)
: aValue < bValue
? -1
: aValue > bValue
? 1
: 0;
return result * sortOrder;
};
}
return function (obj1, obj2) {
let i = 0;
let result = 0;
const numberOfProperties = sortBy === null || sortBy === void 0 ? void 0 : sortBy.length;
while (result === 0 && i < numberOfProperties) {
result = compareByProperty(sortBy[i])(obj1, obj2);
i++;
}
return result;
};
}
/**
* Sorts an array of T by the specified properties of T.
*
* @param arr - the array to be sorted, all of the same type T
* @param sortBy - the names of the properties to sort by, in precedence order.
* Prefix any name with `-` to sort it in descending order.
*/
function sort(arr, ...sortBy) {
arr.sort(byPropertiesOf(sortBy));
}
;