String Utilities
Utilities for manipulating and analyzing string types in TypeScript. These types allow you to trim, join, search, replace, capitalize, and analyze strings at the type level.
String Utilities
Utilities for manipulating and analyzing string types in TypeScript. These types allow you to trim, join, search, replace, capitalize, and analyze strings at the type level using template literal types.
Installation
npm i -D @halvaradop/ts-utility-types
Usage
import type { Trim, Capitalize, Join } from "@halvaradop/ts-utility-types"
import type * as Strings from "@halvaradop/ts-utility-types"
type Utilities = Strings.- Absolute
- AllEquals
- AnyOf
- AppendKeyValue
- ArgsFunction
- ArrayToUnion
- Capitalize
- Chunk
- CompareArrayLength
- ConstructTuple
- DeepFilter
- DeepGet
- DeepKeys
- DeepMerge
- DeepMergeAll
- DeepMutable
- DeepNonNullable
- DeepNonNullish
- DeepNullable
- DeepOmit
- DeepPartial
- DeepPick
- DeepReadonly
- DeepReplace
- DeepRequired
- DeepSet
- DeepTruncate
- DeepUnion
- Discard
- DropChar
- EndsWith
- Equals
- ExcludePrivateKeys
- Expect
- Falsy
- Filter
- FilterNonNullish
- FindAll
- Flatten
- FlattenProperties
- Get
- GetOptional
- GetRequired
- HasDuplicates
- Includes
- IndexOf
- IndexOfString
- Intersection
- IsAny
- IsArray
- IsEven
- IsFunction
- IsNegative
- IsNever
- IsObject
- IsOdd
- IsPositive
- Join
- LastIndexOf
- LengthOfString
- LiteralUnion
- MapTypes
- Merge
- MergeAll
- Mutable
- Not
- Nullish
- NumberRange
- ObjectEntries
- OmitByType
- ParseUrlParams
- PartialByKeys
- PickByType
- Prettify
- Primitive
- PrimitiveNullish
- Properties
- Replace
- ReplaceKeys
- RequiredByKeys
- ReturnTypeOf
- Reverse
- Size
- StartsWith
- Take
- ToPrimitive
- ToUnion
- Trim
- TrimLeft
- TrimRight
- Trunc
- Uniques
- WhiteSpaces
- Zip
Text Manipulation
TrimLeft<Str, string>
Removes whitespace from the start of a string type.
Argument | Type | Description |
---|---|---|
Str | string | The string to trim from the left. |
import type { TrimLeft } from "@halvaradop/ts-utility-types"
type TrimmedLeft = TrimLeft<" hello world ">
type NoSpaces = TrimLeft<"hello world">
TrimRight<Str, string>
Removes whitespace from the end of a string type.
Argument | Type | Description |
---|---|---|
Str | string | The string to trim from the right. |
import type { TrimRight } from "@halvaradop/ts-utility-types"
type TrimmedRight = TrimRight<"hello world ">
type NoTrailing = TrimRight<"hello world">
Trim<Str, string>
Removes whitespace from both ends of a string type. Combines the functionality of TrimLeft
and TrimRight
.
Argument | Type | Description |
---|---|---|
Str | string | The string to trim. |
import type { Trim } from "@halvaradop/ts-utility-types"
// Remove whitespace from both ends
type Trimmed = Trim<" hello world ">
// Already trimmed string
type AlreadyTrimmed = Trim<"no spaces">
// Only leading spaces
type LeadingSpaces = Trim<" leading only">
Template Literal Types
String utilities leverage TypeScript's template literal types for compile-time string manipulation.
Capitalize<Str, string, FirstWord, boolean = true>
Capitalizes the first letter of a word and optionally lowercases the rest.
Argument | Type | Description |
---|---|---|
Str | string | The string to capitalize. |
FirstWord | boolean (default true ) | Whether to only capitalize the first word. |
import type { Capitalize } from "@halvaradop/ts-utility-types"
type Capitalized = Capitalize<"hello world">
type CapitalizeAll = Capitalize<"hello world", false>
Join<Array, unknown[], Separator, number | string>
Joins the values of a tuple into a string, using the given separator.
| Argument | Type | Description |
| ----------- | ----------- | -------------------------------- | -------------------------------------- |
| Array
| unknown[]
| The array to join into a string. |
| Separator
| number | string
| The separator to use between elements. |
import type { Join } from "@halvaradop/ts-utility-types"
type JoinedDash = Join<["a", "p", "p", "l", "e"], "-">
type JoinedSpace = Join<["Hello", "World"], " ">
type JoinedNumber = Join<[1, 2, 3], "|">
StartsWith<Str, string, Match, string>
Checks if a string type starts with the given value.
Argument | Type | Description |
---|---|---|
Str | string | The string to check. |
Match | string | The substring to check for at start. |
import type { StartsWith } from "@halvaradop/ts-utility-types"
type StartsWithAc = StartsWith<"abc", "ac">
type StartsWithAb = StartsWith<"abc", "ab">
type StartsWithHello = StartsWith<"hello world", "hello">
DropChar<Str, string, Match, string>
Removes all occurrences of a character from a string type.
Argument | Type | Description |
---|---|---|
Str | string | The string to remove chars from. |
Match | string | The character to remove. |
import type { DropChar } from "@halvaradop/ts-utility-types"
type NoSpaces = DropChar<" b u t t e r f l y ! ", " ">
type NoDashes = DropChar<"a-b-c-d", "-">
EndsWith<Str, string, Match, string>
Checks if a string type ends with the given value.
Argument | Type | Description |
---|---|---|
Str | string | The string to check. |
Match | string | The substring to check for at end. |
import type { EndsWith } from "@halvaradop/ts-utility-types"
type EndsWithBc = EndsWith<"abc", "bc">
type EndsWithAc = EndsWith<"abc", "ac">
type EndsWithWorld = EndsWith<"hello world", "world">
LengthOfString<Str, string>
Returns the length of a string type as a numeric literal type.
Argument | Type | Description |
---|---|---|
Str | string | The string to get the length of. |
import type { LengthOfString } from "@halvaradop/ts-utility-types"
type Length3 = LengthOfString<"foo">
type Length6 = LengthOfString<"foobar">
type EmptyLength = LengthOfString<"">
IndexOfString<Str, string, Match, string>
Returns the index of the first occurrence of a character in the string. Returns -1
if not found.
Argument | Type | Description |
---|---|---|
Str | string | The string to search in. |
Match | string | The character to search for. |
import type { IndexOfString } from "@halvaradop/ts-utility-types"
type IndexO = IndexOfString<"foo", "o">
type NotFound = IndexOfString<"foo", "z">
type FirstA = IndexOfString<"banana", "a">
Object Utilities
Utilities for manipulating, transforming, and composing object types in TypeScript. These types allow you to combine, filter, select, modify, and map object properties in a flexible and type-safe way.
Number Utilities
Utilities for manipulating and validating numeric types in TypeScript. These types allow you to check parity, sign, get absolute values, truncate decimals, and generate number ranges at the type level.