This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
76 additions
and
30 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
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function termwidth(stream: any): number { | ||
if (!stream.isTTY) { | ||
return 80 | ||
} | ||
const width = stream.getWindowSize()[0] | ||
if (width < 1) { | ||
return 80 | ||
} | ||
if (width < 40) { | ||
return 40 | ||
} | ||
return width | ||
} | ||
|
||
const columns: number | null = (global as any).columns | ||
|
||
export let stdtermwidth = columns || termwidth(process.stdout) | ||
export let errtermwidth = columns || termwidth(process.stderr) |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
export function pickBy<T>(obj: T, fn: (i: T[keyof T]) => boolean): Partial<T> { | ||
return Object.entries(obj) | ||
.reduce((o, [k, v]) => { | ||
if (fn(v)) o[k] = v | ||
return o | ||
}, {} as any) | ||
} | ||
|
||
export function maxBy<T>(arr: T[], fn: (i: T) => number): T | undefined { | ||
let max: {element: T, i: number} | undefined | ||
for (let cur of arr) { | ||
let i = fn(cur) | ||
if (!max || i > max.i) { | ||
max = {i, element: cur} | ||
} | ||
} | ||
return max && max.element | ||
} | ||
|
||
export type SortTypes = string | number | undefined | boolean | ||
|
||
export function sortBy<T>(arr: T[], fn: (i: T) => SortTypes | SortTypes[]): T[] { | ||
// function castType(t: SortTypes | SortTypes[]): string | number | SortTypes[] { | ||
// if (t === undefined) return 0 | ||
// if (t === false) return 1 | ||
// if (t === true) return -1 | ||
// return t | ||
// } | ||
|
||
function compare(a: SortTypes | SortTypes[], b: SortTypes | SortTypes[]): 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