Skip to content

Instantly share code, notes, and snippets.

View vitaly-t's full-sized avatar
🐈
Meow!

Vitaly Tomilov vitaly-t

🐈
Meow!
View GitHub Profile
@vitaly-t
vitaly-t / chain-arrays.ts
Last active October 2, 2024 13:45
Logically concatenates arrays
/**
* Iterable arrays chain, extended for "getLength" and "at" accessor.
*/
export interface IArraysChain<T> extends RelativeIndexable<T>, Iterable<T> {
/**
* Calculates total length of all input arrays combined.
*/
getLength(): number;
}
@vitaly-t
vitaly-t / retry-async.ts
Last active November 4, 2024 10:26
retry-async
/**
* Retry-status object type, for use with RetryCB.
*/
export type RetryStatus = {
/**
* Retry index, starting from 0.
*/
index: number,
/**
* Retry overall duration, in milliseconds.
@vitaly-t
vitaly-t / spike-detector.ts
Last active March 2, 2024 21:59
Value spike detection
import {Observable, switchMap} from 'rxjs';
/**
* Spike detection result.
*/
export interface ISpike {
/**
* Detected spike change, in percent.
*/
type basicType<T> = T extends 'string' ? string :
T extends 'number' ? number :
T extends 'boolean' ? boolean :
T extends 'bigint' ? bigint :
never;
function removeByType<T, A>(input: T[], t1: keyof A): Exclude<T, basicType<typeof t1>>[];
function removeByType<T, A, B>(input: T[], t1: keyof A, t2: keyof B): Exclude<T, basicType<typeof t1 | typeof t2>>[];
function removeByType<T, A, B, C>(input: T[], t1: keyof A, t2: keyof B, t3: keyof C): Exclude<T, basicType<typeof t1 | typeof t2 | typeof t3>>[];
function removeByType<T, A, B, C, D>(input: T[], t1: keyof A, t2: keyof B, t3: keyof C, t4: keyof D): Exclude<T, basicType<typeof t1 | typeof t2 | typeof t3 | typeof t4>>[];
import {Pool} from 'pg'; // or from 'pg-pool'
import {QueryIterable, QueryIterablePool} from 'pg-iterator';
import {from, finalize, Observable} from 'rxjs';
const pool = new Pool({/* connection details */});
// RxJs helper for completing queries safely:
function fromQuery<T>(qi: QueryIterable<T>, text: string, params?: any[]): Observable<T> {
return from(qi.query(text, params)).pipe(finalize(() => qi.release()));
}
import {aggregate, reduce, skip, take, distinct, map, Operation, tap} from 'iter-ops';
///////////////////////////////////////////////////////////////////////////////
// Collection of custom operators for "iter-ops", made from existing operators,
// as implementation examples + ideas (these are not part of the library).
///////////////////////////////////////////////////////////////////////////////
/**
* Logs values into the console.
*/
import {pipe, reduce} from 'iter-ops';
const m = new Map<string, number>();
m.set('one', 1);
m.set('two', 2);
m.set('three', 3);
const i = pipe(m.values(), reduce((a, c) => a + c, 5));
console.log(...i); //=> 11
import {pipe, reduce} from 'iter-ops';
const m = new Map<string, number>();
m.set('one', 1);
m.set('two', 2);
m.set('three', 3);
const i = pipe(m.values(), reduce((a, c) => a + c));
console.log(...i);
import {pipeAsync, map, waitRace, delay} from 'iter-ops';
const i = pipeAsync(
[1, 2, 3],
map(s => Promise.resolve(s * 10)),
delay(100),
waitRace(3)
);
(async function () {
//////////////////
// RUN THIS FIRST: npm install iter-ops@2.2.0-beta.0
//
// The example below crashes NodeJS (it runs out of memory)
// operator source: https://github.com/vitaly-t/iter-ops/blob/wait-cache/src/ops/async/wait-cache.ts
//////////////////
import {map, pipeAsync, waitCache} from 'iter-ops';