This repository was archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use showCommandHelp to delegate topic help (#58)
- Loading branch information
Showing
3 changed files
with
40 additions
and
3 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
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,3 +1,33 @@ | ||
export function compact<T>(a: (T | undefined)[]): T[] { | ||
return a.filter((a): a is T => !!a) | ||
} | ||
|
||
export function uniqBy<T>(arr: T[], fn: (cur: T) => any): T[] { | ||
return arr.filter((a, i) => { | ||
let aVal = fn(a) | ||
return !arr.find((b, j) => j > i && fn(b) === aVal) | ||
}) | ||
} | ||
export namespace sort { | ||
export type Types = string | number | undefined | boolean | ||
} | ||
|
||
export function sortBy<T>(arr: T[], fn: (i: T) => sort.Types | sort.Types[]): T[] { | ||
function compare(a: sort.Types | sort.Types[], b: sort.Types | sort.Types[]): number { | ||
a = a === undefined ? 0 : a | ||
b = b === undefined ? 0 : b | ||
|
||
if (Array.isArray(a) && Array.isArray(b)) { | ||
if (a.length === 0 && b.length === 0) return 0 | ||
let diff = compare(a[0], b[0]) | ||
if (diff !== 0) return diff | ||
return compare(a.slice(1), b.slice(1)) | ||
} | ||
|
||
if (a < b) return -1 | ||
if (a > b) return 1 | ||
return 0 | ||
} | ||
|
||
return arr.sort((a, b) => compare(fn(a), fn(b))) | ||
} |
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