Skip to content

A library of composable functions for the type-level! Transform your TypeScript types in any way you want using functions you already know.

Notifications You must be signed in to change notification settings

KhraksMamtsov/hotscript

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Higher-Order TypeScript (HOTScript)

A library of composable functions for the type-level! Transform your TypeScript types in any way you want using functions you already know.

image

Features

  • 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 🚧

Installation

You can find HotScript on npm:

npm install -D hotscript

HotScript is a work-in-progress library, so expect breaking changes in its API.

Examples

Transforming a list

Run this as a TypeScript Playground

import { Pipe, Tuples, Strings, Numbers } from "hotscript";

// prettier-ignore
type res1 = Pipe<
  //  ^? 95
  [1, 2, 3, 4, 3, 4],
  [
    Tuples.Map<Numbers.Add<3>>,
    Tuples.Join<".">,
    Strings.Split<".">,
    Tuples.Map<Strings.ToNumber>,
    Tuples.Map<Numbers.Add<10>>,
    Tuples.Sum
  ]
>;

Defining a first-class function

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]

Transforming an object type

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;
};

Parsing a route path

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>]>
    >
  ]
>;

TODO

  • Core
    • Pipe
    • PipeRight
    • Call
    • Apply
    • PartialApply
    • Compose
    • ComposeLeft
  • Function
    • ReturnType
    • Parameters
    • Parameter n
  • Tuples
    • Create
    • Partition
    • IsEmpty
    • Zip
    • ZipWith
    • Sort
    • Head
    • At
    • Tail
    • Last
    • FlatMap
    • Find
    • Drop n
    • Take n
    • TakeWhile
    • GroupBy
    • Join separator
    • Map
    • Filter
    • Reduce
    • ReduceRight
    • Every
    • Some
    • ToUnion
    • ToIntersection
    • Prepend
    • Append
    • Concat
    • Min
    • Max
    • Sum
  • Object
    • Readonly
    • Mutable
    • Required
    • Partial
    • ReadonlyDeep
    • MutableDeep
    • RequiredDeep
    • PartialDeep
    • Update
    • Record
    • Keys
    • Values
    • AllPaths
    • Create
    • Get
    • FromEntries
    • Entries
    • MapValues
    • MapKeys
    • Assign
    • Pick
    • PickBy
    • Omit
    • OmitBy
    • CamelCase
    • CamelCaseDeep
    • SnakeCase
    • SnakeCaseDeep
    • KebabCase
    • KebabCaseDeep
  • Union
    • Map
    • Extract
    • ExtractBy
    • Exclude
    • ExcludeBy
    • NonNullable
    • ToTuple
    • ToIntersection
  • String
    • Length
    • TrimLeft
    • TrimRight
    • Trim
    • Join
    • Replace
    • Slice
    • Split
    • Repeat
    • StartsWith
    • EndsWith
    • ToTuple
    • ToNumber
    • ToString
    • Prepend
    • Append
    • Uppercase
    • Lowercase
    • Capitalize
    • Uncapitalize
    • SnakeCase
    • CamelCase
    • KebabCase
    • Compare
    • Equal
    • NotEqual
    • LessThan
    • LessThanOrEqual
    • GreaterThan
    • GreaterThanOrEqual
  • Number
    • Add
    • Multiply
    • Subtract
    • Negate
    • Power
    • Div
    • Mod
    • Abs
    • Compare
    • GreaterThan
    • GreaterThanOrEqual
    • LessThan
    • LessThanOrEqual
  • Boolean
    • And
    • Or
    • XOr
    • Not
    • Extends
    • Equals
    • DoesNotExtend

About

A library of composable functions for the type-level! Transform your TypeScript types in any way you want using functions you already know.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 97.8%
  • JavaScript 1.6%
  • CSS 0.6%