0.0.19 • Published 4 days ago

typeforge v0.0.19

Weekly downloads
-
License
MIT
Repository
-
Last release
4 days ago

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" ]>
0.0.19

4 days ago

0.0.16

13 days ago

0.0.17

13 days ago

0.0.18

13 days ago

0.0.15

21 days ago

0.0.13

29 days ago

0.0.14

29 days ago

0.0.10

1 month ago

0.0.11

1 month ago

0.0.12

1 month ago

0.0.3

1 month ago

0.0.9

1 month ago

0.0.8

1 month ago

0.0.5

1 month ago

0.0.4

1 month ago

0.0.7

1 month ago

0.0.6

1 month ago

0.0.2

6 months ago

0.0.1

6 months ago

0.0.0

6 months ago