-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Streamlined array.ts functions * Updated dependencies * Removed sleep.ts (use setTimeout from node:times/promises) * Reduced size of YouTube payloads
- Loading branch information
1 parent
252c88a
commit 60d6caf
Showing
11 changed files
with
789 additions
and
787 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,41 @@ | ||
import type Nullable from "./nullable"; | ||
// region Types | ||
type Order = "asc" | "desc"; | ||
|
||
import { isNullable } from "./nullable"; | ||
type Sort<T> = (a: T, b: T) => number; | ||
|
||
export const byDate = | ||
<T, U extends Date | Nullable | number | string>( | ||
property: (element: T) => U, | ||
order: "asc" | "desc" = "asc", | ||
) => | ||
type By = { | ||
<T, U>(property: (element: T) => U, order?: Order): Sort<T>; | ||
<T>(order?: Order): Sort<T>; | ||
}; | ||
// endregion | ||
|
||
export const by: By = | ||
<T, U>(property?: ((element: T) => U) | Order, order?: Order) => | ||
(a: T, b: T) => { | ||
const aValue = property(a); | ||
if (isNullable(aValue)) return 0; | ||
const aDate = new Date(aValue); | ||
const aTime = aDate.getTime(); | ||
|
||
const bValue = property(b); | ||
if (isNullable(bValue)) return 0; | ||
const bDate = new Date(bValue); | ||
const bTime = bDate.getTime(); | ||
|
||
switch (order) { | ||
case "asc": { | ||
return aTime - bTime; | ||
} | ||
case "desc": { | ||
return bTime - aTime; | ||
} | ||
if (typeof property === "string") { | ||
order = property; | ||
property = undefined; | ||
} | ||
|
||
property ??= (element: unknown) => element as U; | ||
const aProperty = property(a); | ||
const bProperty = property(b); | ||
|
||
if (aProperty === bProperty) return 0; | ||
const result = aProperty > bProperty ? 1 : -1; | ||
|
||
order ??= "asc"; | ||
return order === "asc" ? result : result * -1; | ||
}; | ||
|
||
export const isUnique = <T>(value: T, index: number, array: T[]) => | ||
array.indexOf(value) === index; | ||
export const unique = <T, U>(property?: (element: T) => U) => { | ||
const set = new Set<U>(); | ||
return (value: T) => { | ||
property ??= (element: unknown) => element as U; | ||
const valueProperty = property(value); | ||
|
||
if (set.has(valueProperty)) return false; | ||
set.add(valueProperty); | ||
return true; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters