tedb
Version:
TypeScript Embedded Database
89 lines • 3.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Get the type of element that will be sorted as `[object ${TYPE}]`
*
* Examples:
* ~~~
* getSortType([{a: 1}], "a"); // [object Number]
* getSortType([{a: "b"}], "a"); // [object String]
* getSortType([{a: new Date()}], "a"); // [object Date]
* getSortType([{a: []}], "a"); // [object Array]
* getSortType([{a: {}}], "a"); // [object Object]
* ~~~
* @param arr - Array to check for type, will not send back a type if null or undefined
* @param field - field name to check against
* @returns {string}
*/
exports.getSortType = (arr, field) => {
let type = "";
for (const doc of arr) {
if (doc.hasOwnProperty(field) && doc[field] !== null && doc[field] !== undefined) {
type = Object.prototype.toString.call(doc[field]);
break;
}
}
return type;
};
const merge = (left, right, sortField, sort, type) => {
const result = [];
const leftLength = left.length;
const rightLength = right.length;
let l = 0;
let r = 0;
// get the type of field. make sure to get the type from a document that has a filled result
if (type === "[object Date]" || type === "[object Number]" || type === "[object String]") {
if (sort === -1) {
while (l < leftLength && r < rightLength) {
if (left[l][sortField] > right[r][sortField]) {
result.push(left[l++]);
}
else {
result.push(right[r++]);
}
}
}
else if (sort === 1) {
while (l < leftLength && r < rightLength) {
if (left[l][sortField] < right[r][sortField]) {
result.push(left[l++]);
}
else {
result.push(right[r++]);
}
}
}
}
else {
throw new Error(`Sortable types are [object Date], [object String], and [object Number], this is not a sortable field, ${Object.prototype.toString.call(type)}`);
}
return result.concat(left.slice(l)).concat(right.slice(r));
};
/**
* Sort array of documents using MergeSort
*
* Examples:
* ~~~
* let docs = [{n: 1}, {n: 6}, {n: 5}];
* let sort = {n: -1};
* let key = Object.keys(sort)[0]; // "n"
* let val = sort[key]; // -1
* let type = getSortType(docs, key); // "[object Number]"
* mergeSort(docs, key, val, type); // [{n: 6}, {n: 5}, {n: 1}]
* ~~~
* @param toSort - array of documents to sort
* @param sortField - field name as string from documents
* @param sortParam - numeric -1 for descending and 1 for ascending sort order
* @param type - one of the results of Object.prototype.toString.call(obj[field])
*/
exports.mergeSort = (toSort, sortField, sortParam, type) => {
const len = toSort.length;
if (len < 2) {
return toSort;
}
const mid = Math.floor(len / 2);
const left = toSort.slice(0, mid);
const right = toSort.slice(mid);
return merge(exports.mergeSort(left, sortField, sortParam, type), exports.mergeSort(right, sortField, sortParam, type), sortField, sortParam, type);
};
//# sourceMappingURL=misc.js.map