UNPKG

@bitblit/ratchet-common

Version:

Common tools for general use

33 lines 1.25 kB
import { RequireRatchet } from './require-ratchet.js'; import { NumberRatchet } from './number-ratchet.js'; export class SortRatchet { constructor() { } static sortNullToTop(a, b, bothNonNullSort) { return SortRatchet.nullSafeSort(a, b, true, bothNonNullSort); } static sortNullToBottom(a, b, bothNonNullSort) { return SortRatchet.nullSafeSort(a, b, false, bothNonNullSort); } static nullSafeSort(a, b, nullsOnTop, bothNonNullSort) { let rval; if (RequireRatchet.isNullOrUndefined(a) && RequireRatchet.isNullOrUndefined(b)) { rval = 0; } else if (RequireRatchet.isNullOrUndefined(a)) { rval = nullsOnTop ? -Infinity : Infinity; } else if (RequireRatchet.isNullOrUndefined(b)) { rval = nullsOnTop ? Infinity : -Infinity; } else { rval = bothNonNullSort(a, b); } return rval; } static sortNumericStringsAsNumbers(a, b, sortNonNumbersToTop) { const an = NumberRatchet.safeNumber(a); const bn = NumberRatchet.safeNumber(b); return SortRatchet.nullSafeSort(an, bn, sortNonNumbersToTop, (ax, bx) => ax - bx); } } //# sourceMappingURL=sort-ratchet.js.map