Skip to content

Instantly share code, notes, and snippets.

View safareli's full-sized avatar

Irakli Safareli safareli

View GitHub Profile
export {};
// https://gist.github.com/rattrayalex/b85d99d4428ee9c1f51661af324c767b
type SchemaPrimitive =
| {
type: "integer";
}
| {
type: "string";
};
@safareli
safareli / assertRecordIsEmpty.ts
Created May 4, 2022 07:45
Check that type of record is an empty record in typescript type level
declare const test1: {}
declare const test2: {foo: 1}
declare const test3: {foo: 1,asda:11}
declare const assertRecordIsEmpty:(input: Record<keyof any,never>) => void
assertRecordIsEmpty(test1)
assertRecordIsEmpty(test2)
assertRecordIsEmpty(test3)
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 June 9, 2021 17:03
Transformer by async generator (this might not work)
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 January 14, 2022 14:16
Composing TypeScript type guards
/**
* 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;
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'
let zz: {}
zz = {} // COMPILES
zz = 1 // COMPILES
zz = "1" // COMPILES
zz = true // COMPILES
zz = { ff:true } // COMPILES
zz = [11] // COMPILES
zz = [] // COMPILES
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 {
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 = {
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 };