Comparing version 1.21.0 to 1.22.0
@@ -81,2 +81,3 @@ export * from './addProp'; | ||
export * from './setPath'; | ||
export * from './shuffle'; | ||
export * from './sliceString'; | ||
@@ -83,0 +84,0 @@ export * from './sort'; |
@@ -97,2 +97,3 @@ "use strict"; | ||
__exportStar(require("./setPath"), exports); | ||
__exportStar(require("./shuffle"), exports); | ||
__exportStar(require("./sliceString"), exports); | ||
@@ -99,0 +100,0 @@ __exportStar(require("./sort"), exports); |
@@ -1,4 +0,4 @@ | ||
import { IterableContainer } from './_types'; | ||
type Direction = 'asc' | 'desc'; | ||
type SortProjection<T> = (x: T) => Comparable; | ||
import type { IterableContainer, NonEmptyArray } from './_types'; | ||
declare const ALL_DIRECTIONS: readonly ["asc", "desc"]; | ||
type Direction = (typeof ALL_DIRECTIONS)[number]; | ||
type ComparablePrimitive = number | string | boolean; | ||
@@ -8,9 +8,13 @@ type Comparable = ComparablePrimitive | { | ||
}; | ||
type SortPair<T> = readonly [SortProjection<T>, Direction]; | ||
type SortProjection<T> = (x: T) => Comparable; | ||
type SortPair<T> = readonly [ | ||
projector: SortProjection<T>, | ||
direction: Direction | ||
]; | ||
type SortRule<T> = SortProjection<T> | SortPair<T>; | ||
export declare function sortBy<T>(sort: SortRule<T>, ...sorts: Array<SortRule<T>>): (array: ReadonlyArray<T>) => Array<T>; | ||
export declare function sortBy<T>(array: ReadonlyArray<T>, ...sorts: Array<SortRule<T>>): Array<T>; | ||
export declare function sortBy<T>(...sortRules: Readonly<NonEmptyArray<SortRule<T>>>): (array: ReadonlyArray<T>) => Array<T>; | ||
export declare function sortBy<T>(array: ReadonlyArray<T>, ...sortRules: Readonly<NonEmptyArray<SortRule<T>>>): Array<T>; | ||
interface Strict { | ||
<T extends IterableContainer>(sort: SortRule<T[number]>, ...sorts: Array<SortRule<T[number]>>): (data: T) => SortedBy<T>; | ||
<T extends IterableContainer>(data: T, ...sorts: Array<SortRule<T[number]>>): SortedBy<T>; | ||
<T extends IterableContainer>(...sortRules: Readonly<NonEmptyArray<SortRule<T[number]>>>): (array: T) => SortedBy<T>; | ||
<T extends IterableContainer>(array: T, ...sortRules: Readonly<NonEmptyArray<SortRule<T[number]>>>): SortedBy<T>; | ||
} | ||
@@ -17,0 +21,0 @@ type SortedBy<T extends IterableContainer> = { |
@@ -14,46 +14,57 @@ "use strict"; | ||
var purry_1 = require("./purry"); | ||
function sortBy(arrayOrSort) { | ||
var sorts = []; | ||
var ALL_DIRECTIONS = ['asc', 'desc']; | ||
var COMPARATOR = { | ||
asc: function (x, y) { return x > y; }, | ||
desc: function (x, y) { return x < y; }, | ||
}; | ||
function sortBy(arrayOrSortRule) { | ||
var sortRules = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
sorts[_i - 1] = arguments[_i]; | ||
sortRules[_i - 1] = arguments[_i]; | ||
} | ||
if (!isSortRule(arrayOrSort)) { | ||
return (0, purry_1.purry)(_sortBy, [arrayOrSort, sorts]); | ||
} | ||
return (0, purry_1.purry)(_sortBy, [__spreadArray([arrayOrSort], sorts, true)]); | ||
var args = isSortRule(arrayOrSortRule) | ||
? | ||
[__spreadArray([arrayOrSortRule], sortRules, true)] | ||
: | ||
[arrayOrSortRule, sortRules]; | ||
return (0, purry_1.purry)(_sortBy, args); | ||
} | ||
exports.sortBy = sortBy; | ||
function isSortRule(x) { | ||
if (typeof x == 'function') | ||
if (typeof x === 'function') { | ||
return true; | ||
if (x.length != 2) | ||
} | ||
var maybeProjection = x[0], maybeDirection = x[1], rest = x.slice(2); | ||
if (rest.length > 0) { | ||
return false; | ||
return typeof x[0] == 'function' && (x[1] === 'asc' || x[1] === 'desc'); | ||
} | ||
return (typeof maybeProjection === 'function' && | ||
ALL_DIRECTIONS.indexOf(maybeDirection) !== -1); | ||
} | ||
function _sortBy(array, sorts) { | ||
var sort = function (a, b, sortRule, sortRules) { | ||
var _sortBy = function (array, sorts) { | ||
return __spreadArray([], array, true).sort(comparer.apply(void 0, sorts)); | ||
}; | ||
function comparer(primaryRule, secondaryRule) { | ||
var otherRules = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
otherRules[_i - 2] = arguments[_i]; | ||
} | ||
var projector = typeof primaryRule === 'function' ? primaryRule : primaryRule[0]; | ||
var direction = typeof primaryRule === 'function' ? 'asc' : primaryRule[1]; | ||
var comparator = COMPARATOR[direction]; | ||
var nextComparer = secondaryRule === undefined | ||
? undefined | ||
: comparer.apply(void 0, __spreadArray([secondaryRule], otherRules, false)); | ||
return function (a, b) { | ||
var _a; | ||
var fn; | ||
var direction; | ||
if (Array.isArray(sortRule)) { | ||
_a = sortRule, fn = _a[0], direction = _a[1]; | ||
} | ||
else { | ||
direction = 'asc'; | ||
fn = sortRule; | ||
} | ||
var dir = direction !== 'desc' ? function (x, y) { return x > y; } : function (x, y) { return x < y; }; | ||
if (!fn) { | ||
return 0; | ||
} | ||
if (dir(fn(a), fn(b))) { | ||
var projectedA = projector(a); | ||
var projectedB = projector(b); | ||
if (comparator(projectedA, projectedB)) { | ||
return 1; | ||
} | ||
if (dir(fn(b), fn(a))) { | ||
if (comparator(projectedB, projectedA)) { | ||
return -1; | ||
} | ||
return sort(a, b, sortRules[0], sortRules.slice(1)); | ||
return (_a = nextComparer === null || nextComparer === void 0 ? void 0 : nextComparer(a, b)) !== null && _a !== void 0 ? _a : 0; | ||
}; | ||
var copied = __spreadArray([], array, true); | ||
return copied.sort(function (a, b) { return sort(a, b, sorts[0], sorts.slice(1)); }); | ||
} | ||
@@ -60,0 +71,0 @@ (function (sortBy) { |
@@ -81,2 +81,3 @@ export * from './addProp'; | ||
export * from './setPath'; | ||
export * from './shuffle'; | ||
export * from './sliceString'; | ||
@@ -83,0 +84,0 @@ export * from './sort'; |
@@ -81,2 +81,3 @@ export * from './addProp'; | ||
export * from './setPath'; | ||
export * from './shuffle'; | ||
export * from './sliceString'; | ||
@@ -83,0 +84,0 @@ export * from './sort'; |
@@ -1,4 +0,4 @@ | ||
import { IterableContainer } from './_types'; | ||
type Direction = 'asc' | 'desc'; | ||
type SortProjection<T> = (x: T) => Comparable; | ||
import type { IterableContainer, NonEmptyArray } from './_types'; | ||
declare const ALL_DIRECTIONS: readonly ["asc", "desc"]; | ||
type Direction = (typeof ALL_DIRECTIONS)[number]; | ||
type ComparablePrimitive = number | string | boolean; | ||
@@ -8,9 +8,13 @@ type Comparable = ComparablePrimitive | { | ||
}; | ||
type SortPair<T> = readonly [SortProjection<T>, Direction]; | ||
type SortProjection<T> = (x: T) => Comparable; | ||
type SortPair<T> = readonly [ | ||
projector: SortProjection<T>, | ||
direction: Direction | ||
]; | ||
type SortRule<T> = SortProjection<T> | SortPair<T>; | ||
export declare function sortBy<T>(sort: SortRule<T>, ...sorts: Array<SortRule<T>>): (array: ReadonlyArray<T>) => Array<T>; | ||
export declare function sortBy<T>(array: ReadonlyArray<T>, ...sorts: Array<SortRule<T>>): Array<T>; | ||
export declare function sortBy<T>(...sortRules: Readonly<NonEmptyArray<SortRule<T>>>): (array: ReadonlyArray<T>) => Array<T>; | ||
export declare function sortBy<T>(array: ReadonlyArray<T>, ...sortRules: Readonly<NonEmptyArray<SortRule<T>>>): Array<T>; | ||
interface Strict { | ||
<T extends IterableContainer>(sort: SortRule<T[number]>, ...sorts: Array<SortRule<T[number]>>): (data: T) => SortedBy<T>; | ||
<T extends IterableContainer>(data: T, ...sorts: Array<SortRule<T[number]>>): SortedBy<T>; | ||
<T extends IterableContainer>(...sortRules: Readonly<NonEmptyArray<SortRule<T[number]>>>): (array: T) => SortedBy<T>; | ||
<T extends IterableContainer>(array: T, ...sortRules: Readonly<NonEmptyArray<SortRule<T[number]>>>): SortedBy<T>; | ||
} | ||
@@ -17,0 +21,0 @@ type SortedBy<T extends IterableContainer> = { |
@@ -11,45 +11,56 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
import { purry } from './purry'; | ||
export function sortBy(arrayOrSort) { | ||
var sorts = []; | ||
var ALL_DIRECTIONS = ['asc', 'desc']; | ||
var COMPARATOR = { | ||
asc: function (x, y) { return x > y; }, | ||
desc: function (x, y) { return x < y; }, | ||
}; | ||
export function sortBy(arrayOrSortRule) { | ||
var sortRules = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
sorts[_i - 1] = arguments[_i]; | ||
sortRules[_i - 1] = arguments[_i]; | ||
} | ||
if (!isSortRule(arrayOrSort)) { | ||
return purry(_sortBy, [arrayOrSort, sorts]); | ||
} | ||
return purry(_sortBy, [__spreadArray([arrayOrSort], sorts, true)]); | ||
var args = isSortRule(arrayOrSortRule) | ||
? | ||
[__spreadArray([arrayOrSortRule], sortRules, true)] | ||
: | ||
[arrayOrSortRule, sortRules]; | ||
return purry(_sortBy, args); | ||
} | ||
function isSortRule(x) { | ||
if (typeof x == 'function') | ||
if (typeof x === 'function') { | ||
return true; | ||
if (x.length != 2) | ||
} | ||
var maybeProjection = x[0], maybeDirection = x[1], rest = x.slice(2); | ||
if (rest.length > 0) { | ||
return false; | ||
return typeof x[0] == 'function' && (x[1] === 'asc' || x[1] === 'desc'); | ||
} | ||
return (typeof maybeProjection === 'function' && | ||
ALL_DIRECTIONS.indexOf(maybeDirection) !== -1); | ||
} | ||
function _sortBy(array, sorts) { | ||
var sort = function (a, b, sortRule, sortRules) { | ||
var _sortBy = function (array, sorts) { | ||
return __spreadArray([], array, true).sort(comparer.apply(void 0, sorts)); | ||
}; | ||
function comparer(primaryRule, secondaryRule) { | ||
var otherRules = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
otherRules[_i - 2] = arguments[_i]; | ||
} | ||
var projector = typeof primaryRule === 'function' ? primaryRule : primaryRule[0]; | ||
var direction = typeof primaryRule === 'function' ? 'asc' : primaryRule[1]; | ||
var comparator = COMPARATOR[direction]; | ||
var nextComparer = secondaryRule === undefined | ||
? undefined | ||
: comparer.apply(void 0, __spreadArray([secondaryRule], otherRules, false)); | ||
return function (a, b) { | ||
var _a; | ||
var fn; | ||
var direction; | ||
if (Array.isArray(sortRule)) { | ||
_a = sortRule, fn = _a[0], direction = _a[1]; | ||
} | ||
else { | ||
direction = 'asc'; | ||
fn = sortRule; | ||
} | ||
var dir = direction !== 'desc' ? function (x, y) { return x > y; } : function (x, y) { return x < y; }; | ||
if (!fn) { | ||
return 0; | ||
} | ||
if (dir(fn(a), fn(b))) { | ||
var projectedA = projector(a); | ||
var projectedB = projector(b); | ||
if (comparator(projectedA, projectedB)) { | ||
return 1; | ||
} | ||
if (dir(fn(b), fn(a))) { | ||
if (comparator(projectedB, projectedA)) { | ||
return -1; | ||
} | ||
return sort(a, b, sortRules[0], sortRules.slice(1)); | ||
return (_a = nextComparer === null || nextComparer === void 0 ? void 0 : nextComparer(a, b)) !== null && _a !== void 0 ? _a : 0; | ||
}; | ||
var copied = __spreadArray([], array, true); | ||
return copied.sort(function (a, b) { return sort(a, b, sorts[0], sorts.slice(1)); }); | ||
} | ||
@@ -56,0 +67,0 @@ (function (sortBy) { |
{ | ||
"name": "remeda", | ||
"version": "1.21.0", | ||
"version": "1.22.0", | ||
"description": "A utility library for JavaScript and Typescript.", | ||
@@ -5,0 +5,0 @@ "main": "dist/commonjs/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
411350
645
5930