A library of composable functions for the type-level! Transform your TypeScript types in any way you want using functions you already know.
npm install hotscriptA 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:
``ts`
npm install -D hotscript
HotScript is a work-in-progress library, so expect breaking changes in its API.
#### Transforming a list
Run this as a TypeScript Playground
`ts
import { Pipe, Tuples, Strings, Numbers } from "hotscript";
type res1 = Pipe<
// ^? 62
[1, 2, 3, 4],
[
Tuples.Map
Tuples.Join<".">, // "4.5.6.7"
Strings.Split<".">, // ["4", "5", "6", "7"]
Tuples.Map
Tuples.Map
Tuples.Sum // 62
]
>;
`
#### Defining a first-class function
Run this as a TypeScript Playground
`ts
import { Call, Fn, Tuples } from "hotscript";
// This is a type-level "lambda"!
interface Duplicate extends Fn {
return: [this["arg0"], this["arg0"]];
}
type result1 = Call
// ^? [[1, 1], [2, 2], [3, 3], [4, 4]]
type result2 = Call
// ^? [1, 1, 2, 2, 3, 3, 4, 4]
`
#### Transforming an object type
Run this as a TypeScript Playground
`ts
import { Pipe, Objects, Booleans } from "hotscript";
// Let's compose some functions to transform an object type:
type ToAPIPayload
T,
[
Objects.OmitBy
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;
};
`
#### Parsing a route path
Run this as a TypeScript Playground
https://user-images.githubusercontent.com/2315749/222081717-96217cd2-ac89-4e06-a942-17fbda717cd2.mp4
`ts
import { Pipe, Objects, Strings, ComposeLeft, Tuples, Match } from "hotscript";
type res5 = Pipe<
// ^? { id: string, index: number }
"/users/
[
Strings.Split<"/">,
Tuples.Filter
Tuples.Map
Tuples.ToUnion,
Objects.FromEntries,
Objects.MapValues<
Match<[Match.With<"string", string>, Match.With<"number", number>]>
>
]
>;
`
- [x] Core
- [x] PipePipeRight
- [x] Call
- [x] Apply
- [x] PartialApply
- [x] Compose
- [x] ComposeLeft
- [x] ReturnType
- [x] Function
- [x] Parameters
- [x] Parameter
- [x] MapReturnType
- [x] MapParameters
- [x] Create
- [ ] Tuples
- [x] Partition
- [x] IsEmpty
- [x] Zip<...Tuple[]>
- [x] ZipWith
- [x] Sort
- [x] Head
- [x] Tail
- [x] At
- [x] Last
- [x] FlatMap
- [x] Find
- [x] Drop
- [x] Take
- [x] TakeWhile
- [x] GroupBy
- [x] Join
- [x] Map
- [x] Filter
- [x] Reduce
- [x] ReduceRight
- [x] Reverse
- [x] Every
- [x] Some
- [x] SplitAt
- [x] ToUnion
- [x] ToIntersection
- [x] Prepend
- [x] Append
- [x] Concat
- [x] Min
- [x] Max
- [x] Sum
- [x] Readonly
- [ ] Object
- [x] Mutable
- [x] Required
- [x] Partial
- [x] ReadonlyDeep
- [x] MutableDeep
- [x] RequiredDeep
- [x] PartialDeep
- [x] Update
- [x] Record
- [x] Keys
- [x] Values
- [x] AllPaths
- [x] Create
- [x] Get
- [x] FromEntries<[Key, Value]>
- [x] Entries
- [x] MapValues
- [x] MapKeys
- [x] Assign<...Obj>
- [x] Pick
- [x] PickBy
- [x] Omit
- [x] OmitBy
- [x] CamelCase
- [x] CamelCaseDeep
- [x] SnakeCase
- [x] SnakeCaseDeep
- [x] KebabCase
- [x] KebabCaseDeep
- [x] Map
- [ ] Union
- [x] Extract
- [x] ExtractBy
- [x] Exclude
- [x] ExcludeBy
- [x] NonNullable
- [x] ToTuple
- [x] ToIntersection
- [x] Length
- [ ] String
- [x] TrimLeft
- [x] TrimRight
- [x] Trim
- [x] Join
- [x] Replace
- [x] Slice
- [x] Split
- [x] Repeat
- [x] StartsWith
- [x] EndsWith
- [x] ToTuple
- [x] ToNumber
- [x] ToString
- [x] Prepend
- [x] Append
- [x] Uppercase
- [x] Lowercase
- [x] Capitalize
- [x] Uncapitalize
- [x] SnakeCase
- [x] CamelCase
- [x] KebabCase
- [x] Compare
- [x] Equal
- [x] NotEqual
- [x] LessThan
- [x] LessThanOrEqual
- [x] GreaterThan
- [x] GreaterThanOrEqual
- [x] Add
- [ ] Number
- [x] Multiply
- [x] Subtract
- [x] Negate
- [x] Power
- [x] Div
- [x] Mod
- [x] Abs
- [x] Compare
- [x] GreaterThan
- [x] GreaterThanOrEqual
- [x] LessThan
- [x] LessThanOrEqual
- [x] And
- [ ] Boolean
- [x] Or
- [x] XOr
- [x] Not
- [x] Extends
- [x] Equals
- [x] DoesNotExtend`
- [x]