-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
@nestia/e2e
to be much functional
- Loading branch information
Showing
8 changed files
with
134 additions
and
161 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,99 @@ | ||
/** | ||
* Utility functions for arrays. | ||
* | ||
* | ||
* @author Jeongho Nam - https://github.com/samchon | ||
*/ | ||
export namespace ArrayUtil { | ||
export async function asyncFilter<Input>( | ||
elements: readonly Input[], | ||
pred: ( | ||
elem: Input, | ||
index: number, | ||
array: readonly Input[], | ||
) => Promise<boolean>, | ||
): Promise<Input[]> { | ||
const ret: Input[] = []; | ||
await asyncForEach(elements, async (elem, index, array) => { | ||
const flag: boolean = await pred(elem, index, array); | ||
if (flag === true) ret.push(elem); | ||
}); | ||
return ret; | ||
} | ||
|
||
export async function asyncForEach<Input>( | ||
elements: readonly Input[], | ||
closure: ( | ||
elem: Input, | ||
index: number, | ||
array: readonly Input[], | ||
) => Promise<any>, | ||
): Promise<void> { | ||
await asyncRepeat(elements.length, (index) => | ||
closure(elements[index], index, elements), | ||
); | ||
} | ||
export const asyncFilter = | ||
<Input>(elements: readonly Input[]) => | ||
async ( | ||
pred: ( | ||
elem: Input, | ||
index: number, | ||
array: readonly Input[], | ||
) => Promise<boolean>, | ||
): Promise<Input[]> => { | ||
const ret: Input[] = []; | ||
await asyncForEach(elements)(async (elem, index, array) => { | ||
const flag: boolean = await pred(elem, index, array); | ||
if (flag === true) ret.push(elem); | ||
}); | ||
return ret; | ||
}; | ||
|
||
export async function asyncMap<Input, Output>( | ||
elements: readonly Input[], | ||
closure: ( | ||
elem: Input, | ||
index: number, | ||
array: readonly Input[], | ||
) => Promise<Output>, | ||
): Promise<Output[]> { | ||
const ret: Output[] = []; | ||
await asyncForEach(elements, async (elem, index, array) => { | ||
const output: Output = await closure(elem, index, array); | ||
ret.push(output); | ||
}); | ||
return ret; | ||
} | ||
export const asyncForEach = | ||
<Input>(elements: readonly Input[]) => | ||
async ( | ||
closure: ( | ||
elem: Input, | ||
index: number, | ||
array: readonly Input[], | ||
) => Promise<any>, | ||
): Promise<void> => { | ||
await asyncRepeat(elements.length)((index) => | ||
closure(elements[index], index, elements), | ||
); | ||
}; | ||
|
||
export async function asyncRepeat<T>( | ||
count: number, | ||
closure: (index: number) => Promise<T>, | ||
): Promise<T[]> { | ||
const indexes: number[] = new Array(count) | ||
.fill(1) | ||
.map((_, index) => index); | ||
export const asyncMap = | ||
<Input>(elements: readonly Input[]) => | ||
async <Output>( | ||
closure: ( | ||
elem: Input, | ||
index: number, | ||
array: readonly Input[], | ||
) => Promise<Output>, | ||
): Promise<Output[]> => { | ||
const ret: Output[] = []; | ||
await asyncForEach(elements)(async (elem, index, array) => { | ||
const output: Output = await closure(elem, index, array); | ||
ret.push(output); | ||
}); | ||
return ret; | ||
}; | ||
|
||
const output: T[] = []; | ||
for (const index of indexes) output.push(await closure(index)); | ||
export const asyncRepeat = | ||
(count: number) => | ||
async <T>(closure: (index: number) => Promise<T>): Promise<T[]> => { | ||
const indexes: number[] = new Array(count) | ||
.fill(1) | ||
.map((_, index) => index); | ||
|
||
return output; | ||
} | ||
const output: T[] = []; | ||
for (const index of indexes) output.push(await closure(index)); | ||
|
||
export function has<T>( | ||
elements: readonly T[], | ||
pred: (elem: T) => boolean, | ||
): boolean { | ||
return elements.find(pred) !== undefined; | ||
} | ||
return output; | ||
}; | ||
|
||
export function repeat<T>( | ||
count: number, | ||
closure: (index: number) => T, | ||
): T[] { | ||
return new Array(count).fill("").map((_, index) => closure(index)); | ||
} | ||
export const has = | ||
<T>(elements: readonly T[]) => | ||
(pred: (elem: T) => boolean): boolean => | ||
elements.find(pred) !== undefined; | ||
|
||
export function last<T>(array: T[]): T { | ||
return array[array.length - 1]; | ||
} | ||
export const repeat = | ||
(count: number) => | ||
<T>(closure: (index: number) => T): T[] => | ||
new Array(count).fill("").map((_, index) => closure(index)); | ||
|
||
export function flat<T>(matrix: T[][]): T[] { | ||
return ([] as T[]).concat(...matrix); | ||
} | ||
export const flat = <T>(matrix: T[][]): T[] => | ||
([] as T[]).concat(...matrix); | ||
|
||
export function subsets<T>(array: T[]): T[][] { | ||
export const subsets = <T>(array: T[]): T[][] => { | ||
const check: boolean[] = new Array(array.length).fill(false); | ||
const output: T[][] = []; | ||
const dfs = (depth: number) => { | ||
|
||
const dfs = (depth: number): void => { | ||
if (depth === check.length) | ||
output.push(array.filter((_v, idx) => check[idx])); | ||
else { | ||
check[depth] = true; | ||
dfs(depth + 1); | ||
|
||
check[depth] = false; | ||
dfs(depth + 1); | ||
} | ||
}; | ||
dfs(0); | ||
return output; | ||
} | ||
}; | ||
} |
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
Oops, something went wrong.