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
import { Writable } from "stream"; | |
export const streamAsPromise = (readable: Readable): Promise<Buffer> => { | |
const result: Array<Buffer> = []; | |
const w = new Writable({ | |
write(chunk, encoding, callback) { | |
result.push(chunk); | |
callback(); | |
}, |
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
import { Readable } from "stream"; | |
export const getReadableStream = (buffer: Buffer): Readable => { | |
const stream = new Readable(); | |
stream.push(buffer); | |
stream.push(null); | |
return stream; | |
}; |
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
export const mapSeries = <T>(tasks: Array<() => Promise<T>>): Promise<Array<T>> => { | |
return tasks.reduce((promiseChain, currentTask) => { | |
return promiseChain.then(chainResults => currentTask().then(currentResult => [...chainResults, currentResult])); | |
}, Promise.resolve([] as Array<T>)); | |
}; |
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
export const fisherYates = <T>(array: Array<T>): Array<T> => { | |
for (let i = array.length - 1; i > 0; i--) { | |
const j = Math.floor(Math.random() * (i + 1)); | |
[array[i], array[j]] = [array[j], array[i]]; | |
} | |
return array; | |
}; |
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
export const chunkBy = <T>(array: Array<T>, size = 1): Array<Array<T>> => { | |
const arrayChunks: Array<Array<T>> = []; | |
for (let pointer = 0; pointer < array.length; pointer += size) { | |
arrayChunks.push(array.slice(pointer, pointer + size)); | |
} | |
return arrayChunks; | |
}; |