Skip to content

Instantly share code, notes, and snippets.

View TrejGun's full-sized avatar
🌴
On vacation

Oleg Gun TrejGun

🌴
On vacation
View GitHub Profile
@TrejGun
TrejGun / chunkBy.ts
Last active August 4, 2021 02:15
chunkBy
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;
};
@TrejGun
TrejGun / fisherYates.ts
Created August 4, 2021 02:16
fisherYates
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;
};
@TrejGun
TrejGun / mapSeries.ts
Last active September 30, 2021 02:42
mapSeries
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>));
};
@TrejGun
TrejGun / buffer-to-stream.ts.ts
Created December 10, 2021 11:45
buffer to stream
import { Readable } from "stream";
export const getReadableStream = (buffer: Buffer): Readable => {
const stream = new Readable();
stream.push(buffer);
stream.push(null);
return stream;
};
@TrejGun
TrejGun / stream-to-buffer.ts
Created December 10, 2021 11:47
Stream to buffer
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();
},
@TrejGun
TrejGun / elevenlabs1.cjs
Last active October 30, 2024 08:50
ElevenLabs - get voices
;(async () => {
const XI_API_KEY = 'sk_...'
const url = 'https://api.elevenlabs.io/v1/voices'
const headers = {
Accept: 'application/json',
'xi-api-key': XI_API_KEY,
'Content-Type': 'application/json',
}
@TrejGun
TrejGun / elevenlabs2.cjs
Last active October 30, 2024 08:50
ElevenLabs - hello world!
const { createWriteStream } = require("node:fs");
const { Readable } = require("node:stream");
;(async () => {
const XI_API_KEY = "sk_...";
const VOICE_ID = "9BWtsMINqrJLrRacOk9x"; // Aria
const TEXT_TO_SPEAK = "hello world!";
const OUTPUT_PATH = "output.mp3";
const url = `https://api.elevenlabs.io/v1/text-to-speech/${VOICE_ID}/stream`;
@TrejGun
TrejGun / scrapingbee.cjs
Last active November 12, 2024 07:25
ScrapingBee
const { ScrapingBeeClient } = require('scrapingbee')
async function get(url) {
const client = new ScrapingBeeClient("API_KEY")
return client.get({
url,
params: {
json_response: true,
screenshot_full_page: true,
block_ads: true,