Skip to content

Instantly share code, notes, and snippets.

Avatar

Irakli Safareli safareli

View GitHub Profile
View kebabcase2camelcase.ts
const pascalCaseToKebabCase = (x: string) =>
x.replace(
/[A-Z]/g,
(match, offset) => (offset > 0 ? "-" : "") + match.toLowerCase()
);
const kebabCaseToPascalCase = (x: string) =>
x
.replace(/(^[a-z])|(-[a-z])/g, (match) => match.toUpperCase())
@safareli
safareli / transformBy.ts
Last active Jun 9, 2021
Transformer by async generator (this might not work)
View transformBy.ts
import { Duplex, ReadableOptions } from "stream";
export const createDeferredPromise = <T>() => {
const res = {
resolve: (_val: T) => {},
reject: (_reason?: unknown) => {},
promise: new Promise<T>((resolve, reject) => {
res.resolve = resolve;
res.reject = reject;
}),
};
@safareli
safareli / type-guard-composition.ts
Last active Jun 2, 2021
Composing TypeScript type guards
View type-guard-composition.ts
/**
* Type representing a guard function accepting Input and some other arguments
* while refining type of input as `Output`
*/
export type TypeGuard<
Input,
Output extends Input,
Args extends unknown[] = []
> = (value: Input, ...args: Args) => value is Output;
View number-enum-vs-string-enum.ts
enum Bool {True="t", False="f"}
function f(b:Bool) {
switch(b) {
case Bool.True: return 'true'
case Bool.False: return 'false'
// ERROR AS EXPECTED ✅
case "11": return 'oops'
// ERROR AS EXPECTED ✅
case 11: return 'oops'
View RecordEmpty.ts
let zz: {}
zz = {} // COMPILES
zz = 1 // COMPILES
zz = "1" // COMPILES
zz = true // COMPILES
zz = { ff:true } // COMPILES
zz = [11] // COMPILES
zz = [] // COMPILES
View interval.spec.ts
import * as fc from "fast-check";
import { pipe } from "fp-ts/lib/function";
import {
fromArray,
groupSort,
map,
NonEmptyArray,
} from "fp-ts/lib/NonEmptyArray";
import { Ord, ordDate, ordNumber } from "fp-ts/lib/Ord";
import {
View get.ts
import { Semaphore } from "async-mutex";
import util from "util";
import path from "path";
import id3 from "node-id3";
import http from "http";
import fs from "fs";
import { Tags } from "node-id3";
import { assert, assert_, notNullOrUndefined } from "../lib/assert";
const work = {
View MVar.ts
import { Mutex } from "async-mutex";
export type MVar<T> = {
take: () => Promise<T>;
read: () => Promise<T>;
put: (newVal: T) => Promise<void>;
};
export const newMVar = function <T>(initial: T): MVar<T> {
const val = { current: initial };
View church_encoded_if_boolean_maybe.ts
type _Boolean = <A>(onTrue: () => A, onFalse: () => A) => A
const _true: _Boolean = (onTrue) => onTrue()
const _false: _Boolean = (_, onFalse) => onFalse()
const _if = <A>(condition: _Boolean, onTrue: () => A, onFalse: () => A): A => {
return condition(onTrue, onFalse)
}
const _and = (a: _Boolean,b: _Boolean): _Boolean => {
return (onTrue: () => any, onFalse: () => any) => {
@safareli
safareli / parMap.ts
Last active Feb 26, 2021
Traverse record of promises in parallel and give result of the same shape. (similar to sequenceRecord from purescript)
View parMap.ts
const parMap = async <T, U>(array:T[], callback: (value: T) => U | PromiseLike<U>): Promise<U[]> => {
return Promise.all(array.map(callback))
}
const parEach = async <T, U>(array:T[], callback: (value: T) => void | PromiseLike<void>): Promise<void> => {
await Promise.all(array.map(callback))
}
async function main() {
const names = ["foo.txt","baz.txt"]