A library of composable functions for the type level!
Transform your TypeScript types in any way you want using functions you already know.
- Type-level higher-order functions (
Tuples.Map
,Tuples.Filter
,Objects.MapValues
, etc). - Type-level pattern matching with
Match
. - Performant math operations (
Numbers.Add
,Numbers.Sub
,Numbers.Mul
,Numbers.Div
, etc). - Custom "lambda" functions.
🚧 work in progress 🚧
You can find HotScript on npm:
npm install -D hotscript
HotScript is a work-in-progress library, so expect breaking changes in its API.
Run this as a TypeScript Playground
import { Pipe, Tuples, Strings, Numbers } from "hotscript";
type res1 = Pipe<
// ^? 62
[1, 2, 3, 4],
[
Tuples.Map<Numbers.Add<3>>, // [4, 5, 6, 7]
Tuples.Join<".">, // "4.5.6.7"
Strings.Split<".">, // ["4", "5", "6", "7"]
Tuples.Map<Strings.Prepend<"1">>, // ["14", "15", "16", "17"]
Tuples.Map<Strings.ToNumber>, // [14, 15, 16, 17]
Tuples.Sum // 62
]
>;
Run this as a TypeScript Playground
import { Call, Fn, Tuples } from "hotscript";
// This is a type-level "lambda"!
interface Duplicate extends Fn {
return: [this["arg0"], this["arg0"]];
}
type result1 = Call<Tuples.Map<Duplicate>, [1, 2, 3, 4]>;
// ^? [[1, 1], [2, 2], [3, 3], [4, 4]]
type result2 = Call<Tuples.FlatMap<Duplicate>, [1, 2, 3, 4]>;
// ^? [1, 1, 2, 2, 3, 3, 4, 4]
Run this as a TypeScript Playground
import { Pipe, Objects, Booleans } from "hotscript";
// Let's compose some functions to transform an object type:
type ToAPIPayload<T> = Pipe<
T,
[
Objects.OmitBy<Booleans.Equals<symbol>>,
Objects.Assign<{ metadata: { newUser: true } }>,
Objects.SnakeCaseDeep,
Objects.Assign<{ id: string }>
]
>;
type T = ToAPIPayload<{
id: symbol;
firstName: string;
lastName: string;
}>;
// Returns:
type T = {
id: string;
metadata: { new_user: true };
first_name: string;
last_name: string;
};
Run this as a TypeScript Playground
hotscript.mp4
import { Pipe, Objects, Strings, ComposeLeft, Tuples, Match } from "hotscript";
type res5 = Pipe<
// ^? { id: string, index: number }
"/users/<id:string>/posts/<index:number>",
[
Strings.Split<"/">,
Tuples.Filter<Strings.StartsWith<"<">>,
Tuples.Map<ComposeLeft<[Strings.Trim<"<" | ">">, Strings.Split<":">]>>,
Tuples.ToUnion,
Objects.FromEntries,
Objects.MapValues<
Match<[Match.With<"string", string>, Match.With<"number", number>]>
>
]
>;
- Core
-
Pipe<Input, Fn[]>
-
PipeRight<Fn[], Input>
-
Call<Fn, ...Arg>
-
Apply<Fn, Arg[]>
-
PartialApply<Fn, Arg[]>
-
Compose<Fn[]>
-
ComposeLeft<Fn[]>
-
- Function
-
ReturnType<Fn>
-
Parameters<Fn>
-
Parameter<N, Fn>
-
- Tuples
-
Create<X> -> [X]
-
Partition<Tuple>
-
IsEmpty<Tuple>
-
Zip<...Tuple[]>
-
ZipWith<Fn, ...Tuple[]>
-
Sort<Tuple>
-
Head<Tuple>
-
Tail<Tuple>
-
At<N, Tuple>
-
Last<Tuple>
-
FlatMap<Fn, Tuple>
-
Find<Fn, Tuple>
-
Drop<N, Tuple>
-
Take<N, Tuple>
-
TakeWhile<Fn, Tuple>
-
GroupBy<Fn, Tuple>
-
Join<Str, Tuple>
-
Map<Fn, Tuple>
-
Filter<Fn, Tuple>
-
Reduce<Fn, Init, Tuple>
-
ReduceRight<Fn, Init, Tuple>
-
Every<Fn, Tuple>
-
Some<Fn, Tuple>
-
ToUnion<Tuple>
-
ToIntersection<Tuple>
-
Prepend<X, Tuple>
-
Append<X, Tuple>
-
Concat<T1, T2>
-
Min<Tuple>
-
Max<Tuple>
-
Sum<Tuple>
-
- Object
-
Readonly<Obj>
-
Mutable<Obj>
-
Required<Obj>
-
Partial<Obj>
-
ReadonlyDeep<Obj>
-
MutableDeep<Obj>
-
RequiredDeep<Obj>
-
PartialDeep<Obj>
-
Update<Path, Fn | V, Obj>
-
Record<Key, Value>
-
Keys<Obj>
-
Values<Obj>
-
AllPaths<Obj>
-
Create<Pattern, X>
-
Get<Path, Obj>
-
FromEntries<[Key, Value]>
-
Entries<Obj>
-
MapValues<Fn, Obj>
-
MapKeys<Fn, Obj>
-
Assign<...Obj>
-
Pick<Key, Obj>
-
PickBy<Fn, Obj>
-
Omit<Key, Obj>
-
OmitBy<Fn, Obj>
-
CamelCase<Obj>
-
CamelCaseDeep<Obj>
-
SnakeCase<Obj>
-
SnakeCaseDeep<Obj>
-
KebabCase<Obj>
-
KebabCaseDeep<Obj>
-
- Union
-
Map<Fn, U>
-
Extract<T, U>
-
ExtractBy<Fn, U>
-
Exclude<T, U>
-
ExcludeBy<Fn, U>
-
NonNullable<U>
-
ToTuple<U>
-
ToIntersection<U>
-
- String
-
Length<Str>
-
TrimLeft<Str>
-
TrimRight<Str>
-
Trim<Str>
-
Join<Sep, Str>
-
Replace<From, To, Str>
-
Slice<Start, End, Str>
-
Split<Sep, Str>
-
Repeat<N, Str>
-
StartsWith<S, Str>
-
EndsWith<E, Str>
-
ToTuple<Str>
-
ToNumber<Str>
-
ToString<Str>
-
Prepend<Start, Str>
-
Append<End, Str>
-
Uppercase<Str>
-
Lowercase<Str>
-
Capitalize<Str>
-
Uncapitalize<Str>
-
SnakeCase<Str>
-
CamelCase<Str>
-
KebabCase<Str>
-
Compare<Str, Str>
-
Equal<Str, Str>
-
NotEqual<Str, Str>
-
LessThan<Str, Str>
-
LessThanOrEqual<Str, Str>
-
GreaterThan<Str, Str>
-
GreaterThanOrEqual<Str, Str>
-
- Number
-
Add<N, M>
-
Multiply<N, M>
-
Subtract<N, M>
-
Negate<N>
-
Power<N, M>
-
Div<N, M>
-
Mod<N, M>
-
Abs<N>
-
Compare<N, M>
-
GreaterThan<N, M>
-
GreaterThanOrEqual<N, M>
-
LessThan<N, M>
-
LessThanOrEqual<N, M>
-
- Boolean
-
And<Bool, Bool>
-
Or<Bool, Bool>
-
XOr<Bool, Bool>
-
Not<Bool>
-
Extends<A, B>
-
Equals<A, B>
-
DoesNotExtend<A, B>
-