npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typeforge

v0.0.23

Published

Useful utility types for typescript

Downloads

189

Readme

Typeforge

Typeforge contains useful utility types for typescript!


Strings

StringStartsWith<"Hello", "H"> // true

StringEndsWith<"Hello", "baz"> // false

StringRemoveFirstChar<"Hello"> // ello

StringRemoveLastChar<"World"> // Worl

StringSplit<
  "Hello_World_Foo_Bar", // The string to split.
  "_",                   // The seperator to split at.
  2                      // The maximum amount of splits to make.
> // ["Hello", "World", "Foo_Bar"]

StringSplitAll<
  "Hello_World_Foo_Bar", // The string to split.
  "_",                   // The seperator to split at.
>  // ["Hello", "World", "Foo", "Bar"]

StringSplitOnce<
  "Hello_World_Foo_Bar", // The string to split.
  "_",                   // The seperator to split at.
> // ["Hello", "_World_Foo_Bar"]

StringReplace<
  "Hello World World", // The string to replace from.
  "World",             // The target characters to replace.
  "There",             // The replacement characters.
  1                    // The maximum amount of times to replace.
> // "Hello There World"

// Slightly more performant than `StringReplace` if you are replacing all occurrences.
StringReplaceAll<
  "Hello World World", // The string to replace from.
  "World",             // The target characters to replace.
  "There"              // The replacement characters.
> // "Hello There There"

StringLooseAutocomplete<"FooBar">

StringCondenseDuplicates<
  "Hello____World__Foo_________Bar", // The string to condense duplicates for.
  "_"                                // The target characters to condense.
> // "Hello_World_Foo_Bar"

StringIsLiteral<"Hello"> // true

StringContains<"Hello", "lo"> // true

Unions

UnionPrettify<"Hello" | "World" | "Foo" | "Baz"> // "Hello" | "World" | "Foo" | "Baz"

IsUnion<"Hello" | "World" | "Foo" | "Baz"> // true

UnionToArray<"Hello" | "World" | "Foo" | "Baz"> // ["Hello", "World", "Foo", "Baz"]

Arrays

ArrayPrettify<[ "Hello", "World" ]> // ["Hello", "World"]

ArrayToUnion<["Hello", "World", "Foo", "Baz"]> // "Hello" | "World" | "Foo" | "Baz"

ArrayConcat<
  [ "One", "Two", "Three", "Four" ] // The array to concatenate.
  ", "                              // The characters to concatenate with.
> // "One, Two, Three, Four"

ArrayRemoveTypes<
  [ true, "carrot", 55, "tomato", "onion", 66 ], // The array to remove from.
  string | number                                // The type of elements to be removed.
> // [true]

// Only keeps elements of specified types in an array.
ArrayKeepTypes<
  [ true, "carrot", 55, "tomato", "onion", 66 ], // The array to remove from.
  string | number                                // The type of elements to be kept.
> // ["carrot", 55, "tomato", "onion", 66]

ArrayRemoveLastItem<[ "one", "two", "three" ]> // ["one", "two"]

ArrayLastItem<[ "one", "two", "three" ]> // "three"

// Used to ensure that an array type is not empty (e.g. [] extends ArrayNonEmpty<string>)/
ArrayNonEmpty<string> // [string, ...string[]]

Object

ObjectPrettifyDeep<
  { hello: "world" } & { foo: "bar" }
  & { baz: { hello: "world" } & { foo: "bar" } }
> /* {
  hello: "world";
  foo: "bar";
  baz: {
      hello: "world";
      foo: "bar";
  };
} */

ObjectPrettify<
  { hello: "world" } & { foo: "bar" }
  & { baz: { hello: "world" } & { foo: "bar" } }
> /* {
  hello: "world";
  foo: "bar";
  baz: {
      hello: "world";
  } & {
      foo: "bar";
  };
} */

ObjectDifferentKeys<
  { hello: "world", bar: "foo" },
  { hello: "world", baz: "foo" }
> // { bar: "foo"; baz: "foo"; }

ObjectSameKeys<
  { hello: "world", bar: "foo" },
  { hello: "world", baz: "foo" }
> // { hello: "world"; }

ObjectDeepMerge<
  { Bob: { Age: 41 }, Dave: { Age: 32, EyeColor: "Green" } },
  { Bob: { EyeColor: "Blue" }, Dave: { EyeColor: "Brown" } }
> // { Bob: { Age: 41, EyeColor: "Blue" }, Dave: { Age: 32, EyeColor: "Brown" } } 

ObjectRemoveKeys<
  { hello: "world", foo: "bar", baz: "foo" },
  "hello" | "baz"
> // { foo: "bar" }

// Only keep specified keys inside of an object.
ObjectKeepKeys<
  { hello: "world", foo: "bar", baz: "foo" },
  "hello" | "baz"
> // { hello: "world"; baz: "foo"; }

ObjectOverwrite<
  { foo: { bar: "baz", hello: "world" } },
  { foo: { bar: "fooBar" } }
> // { foo: { bar: "fooBar"; hello: "world"; }; }

ObjectShallowOverwrite<
  { foo: { bar: "baz", hello: "world" } },
  { foo: { bar: "fooBar" } }
> // { foo: { bar: "fooBar"; }; }

ISO

// Utility types to ensure a string is an ISO string.

ISOYear        
ISOMonth       
ISODay         
ISOHours       
ISOMinutes     
ISOSeconds     
ISOMilliseconds
ISOTimeZoneOffset
ISODate
ISOTime
ISODateTime

Prettify

// Automatically prettifies Objects, Unions and Arrays.

Prettify<{ hello: "world" }>
Prettify<"Hello" | "World">
Prettify<[ "Hello", "World" ]>

PrettifyDeep<{ hello: "world" }>
PrettifyDeep<"Hello" | "World">
PrettifyDeep<[ "Hello", "World" ]>