twing
Version:
First-class Twig engine for Node.js
60 lines (59 loc) • 1.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.asortSynchronously = exports.asort = void 0;
const sort_1 = require("./sort");
/**
* Sort a map and maintain index association.
*
* @param map
* @param compareFunction
* @returns
*/
const asort = async (map, compareFunction) => {
const sortedMap = new Map();
const keys = [].fill(null, 0, map.size);
const values = [...map.values()];
let sortedValues;
if (compareFunction) {
sortedValues = await (0, sort_1.sortAsynchronously)(values, compareFunction);
}
else {
sortedValues = values.sort();
}
for (const [key, value] of map) {
const index = sortedValues.indexOf(value);
keys[index] = key;
}
for (const key of keys) {
sortedMap.set(key, map.get(key));
}
map.clear();
for (const [key, value] of sortedMap) {
map.set(key, value);
}
};
exports.asort = asort;
const asortSynchronously = (map, compareFunction) => {
const sortedMap = new Map();
const keys = [].fill(null, 0, map.size);
const values = [...map.values()];
let sortedValues;
if (compareFunction) {
sortedValues = values.sort(compareFunction);
}
else {
sortedValues = values.sort();
}
for (const [key, value] of map) {
const index = sortedValues.indexOf(value);
keys[index] = key;
}
for (const key of keys) {
sortedMap.set(key, map.get(key));
}
map.clear();
for (const [key, value] of sortedMap) {
map.set(key, value);
}
};
exports.asortSynchronously = asortSynchronously;