Skip to content

Repository files navigation

string

MinifiedMinzipped

Functional utilities for strings.

Documentation

Installation

npm install @monstermann/string
pnpm add @monstermann/string
yarn add @monstermann/string
bun add @monstermann/string

Tree-shaking

Installation

npm install -D @monstermann/unplugin-string
pnpm -D add @monstermann/unplugin-string
yarn -D add @monstermann/unplugin-string
bun -D add @monstermann/unplugin-string

Usage

// vite.config.tsimportstringfrom"@monstermann/unplugin-string/vite";exportdefaultdefineConfig({plugins: [string()],});
// rollup.config.jsimportstringfrom"@monstermann/unplugin-string/rollup";exportdefault{plugins: [string()],};
// rolldown.config.jsimportstringfrom"@monstermann/unplugin-string/rolldown";exportdefault{plugins: [string()],};
// webpack.config.jsconststring=require("@monstermann/unplugin-string/webpack");module.exports={plugins: [string()],};
// rspack.config.jsconststring=require("@monstermann/unplugin-string/rspack");module.exports={plugins: [string()],};
// esbuild.config.jsimport{build}from"esbuild";importstringfrom"@monstermann/unplugin-string/esbuild";build({plugins: [string()],});

String

append

functionString.append(target: string,source: Iterable<string>,): string

Appends source or strings from source iterable to the end of target string.

Example

import{String}from"@monstermann/string";String.append("hello"," world");// "hello world"String.append("hello",[" ","world"]);// "hello world"
import{String}from"@monstermann/string";pipe("hello",String.append(" world"));// "hello world"pipe("hello",String.append([" ","world"]));// "hello world"

camelCase

functionString.camelCase<Textendsstring>(target: T,): CamelCase<T>

Converts target string to camelCase format.

Example

import{String}from"@monstermann/string";String.camelCase("hello world");// "helloWorld"String.camelCase("hello-world");// "helloWorld"
import{String}from"@monstermann/string";pipe("hello world",String.camelCase());// "helloWorld"pipe("hello-world",String.camelCase());// "helloWorld"

capitalize

functionString.capitalize<Textendsstring>(target: T,): Capitalize<T>

Capitalizes the first letter of target string.

Example

import{String}from"@monstermann/string";String.capitalize("hello world");// "Hello world"String.capitalize("hello");// "Hello"
import{String}from"@monstermann/string";pipe("hello world",String.capitalize());// "Hello world"pipe("hello",String.capitalize());// "Hello"

constantCase

functionString.constantCase<Textendsstring>(target: T,): ConstantCase<T>

Converts target string to CONSTANT_CASE format.

Example

import{String}from"@monstermann/string";String.constantCase("hello world");// "HELLO_WORLD"String.constantCase("helloWorld");// "HELLO_WORLD"
import{String}from"@monstermann/string";pipe("hello world",String.constantCase());// "HELLO_WORLD"pipe("helloWorld",String.constantCase());// "HELLO_WORLD"

create

functionString.create(value?: any): string

An alias for String(target).

Example

import{String}from"@monstermann/string";String.create(10);// "10"

deburr

functionString.deburr(target: string): string

Removes diacritical marks from target string and converts special characters to their ASCII equivalents.

The function normalizes the string, strips combining diacritical marks, and replaces special characters like ligatures (Æ, œ) and extended Latin characters (ø, ß, þ) with their closest ASCII representations.

Example

import{String}from"@monstermann/string";String.deburr("café");// "cafe"String.deburr("naïve résumé");// "naive resume"String.deburr("Æsop's Œuvres");// "Aesop's Oeuvres"String.deburr("Ørsted");// "Orsted"
import{String}from"@monstermann/string";pipe("café",String.deburr());// "cafe"pipe("naïve résumé",String.deburr());// "naive resume"pipe("Æsop's Œuvres",String.deburr());// "Aesop's Oeuvres"pipe("Ørsted",String.deburr());// "Orsted"

drop

functionString.drop(target: string,amount: number): string

Removes the first amount characters from target string.

Example

import{String}from"@monstermann/string";String.drop("hello world",6);// "world"
import{String}from"@monstermann/string";pipe("hello world",String.drop(6));// "world"

dropLast

functionString.dropLast(target: string,amount: number): string

Removes the last amount characters from target string.

Example

import{String}from"@monstermann/string";String.dropLast("hello world",6);// "hello"
import{String}from"@monstermann/string";pipe("hello world",String.dropLast(6));// "hello"

endsWith

functionString.endsWith(target: string,source: string): boolean

Checks if target string ends with source string.

Example

import{String}from"@monstermann/string";String.endsWith("hello world","world");// true
import{String}from"@monstermann/string";pipe("hello world",String.endsWith("world"));// true

graphemes

functionString.graphemes(target: string): Generator<string>

Returns a generator that yields individual grapheme clusters from target string.

A grapheme cluster represents a single user-perceived character, which may consist of multiple Unicode code points (e.g., emojis with modifiers, base characters with combining diacriticals).

Example

import{String}from"@monstermann/string";[...String.graphemes("hello")];// ["h", "e", "l", "l", "o"][...String.graphemes("👨‍👩‍👧‍👦")];// ["👨‍👩‍👧‍👦"][...String.graphemes("café")];// ["c", "a", "f", "é"][...String.graphemes("👋🏽")];// ["👋🏽"]// Using in a loopfor(constgraphemeofString.graphemes("hello👋")){console.log(grapheme);// "h", "e", "l", "l", "o", "👋"}
import{String}from"@monstermann/string";[...pipe("hello",String.graphemes())];// ["h", "e", "l", "l", "o"][...pipe("👨‍👩‍👧‍👦",String.graphemes())];// ["👨‍👩‍👧‍👦"][...pipe("café",String.graphemes())];// ["c", "a", "f", "é"][...pipe("👋🏽",String.graphemes())];// ["👋🏽"]// Using in a loopfor(constgraphemeofpipe("hello👋",String.graphemes())){console.log(grapheme);// "h", "e", "l", "l", "o", "👋"}

has

functionString.has(target: string,source: string): boolean

Checks if target string contains source string.

Example

import{String}from"@monstermann/string";String.has("hello world","world");// true
import{String}from"@monstermann/string";pipe("hello world",String.has("world"));// true

hasAll

functionString.hasAll(target: string,source: Iterable<string>,): boolean

Checks if target string contains all strings from the source iterable.

Example

import{String}from"@monstermann/string";String.hasAll("hello world",["hello","world"]);// true
import{String}from"@monstermann/string";pipe("hello world",String.hasAll(["hello","world"]));// true

hasAny

functionString.hasAny(target: string,source: Iterable<string>,): boolean

Checks if target string contains any of the strings from the source iterable.

Example

import{String}from"@monstermann/string";String.hasAny("hello world",["foo","world"]);// true
import{String}from"@monstermann/string";pipe("hello world",String.hasAny(["foo","world"]));// true

hasNone

functionString.hasNone(target: string,source: Iterable<string>,): boolean

Checks if target string contains none of the strings from the source iterable.

Example

import{String}from"@monstermann/string";String.hasNone("hello world",["foo","bar"]);// true
import{String}from"@monstermann/string";pipe("hello world",String.hasNone(["foo","bar"]));// true

indexOf

functionString.indexOf(target: string,source: string): number

Returns the index of the first occurrence of source string in target string, or -1 if not found.

Example

import{String}from"@monstermann/string";String.indexOf("hello world","world");// 6
import{String}from"@monstermann/string";pipe("hello world",String.indexOf("world"));// 6

indexOfOr

functionString.indexOfOr<T>(target: string,source: string,or: T,): number|T

Returns the index of the first occurrence of source string in target string, or the or value if not found.

Example

import{String}from"@monstermann/string";String.indexOfOr("hello world","world",-1);// 6String.indexOfOr("hello world","foo",-1);// -1
import{String}from"@monstermann/string";pipe("hello world",String.indexOfOr("world",-1));// 6pipe("hello world",String.indexOfOr("foo",-1));// -1

indexOfOrElse

functionString.indexOfOrElse<T>(target: string,source: string,orElse: (target: string)=>T,): number|T

Returns the index of the first occurrence of source string in target string, or the result of calling orElse function with target if not found.

Example

import{String}from"@monstermann/string";String.indexOfOrElse("hello world","world",()=>-1);// 6String.indexOfOrElse("hello world","foo",(str)=>str.length);// 11
import{String}from"@monstermann/string";pipe("hello world",String.indexOfOrElse("world",()=>-1),);// 6pipe("hello world",String.indexOfOrElse("foo",(str)=>str.length),);// 11

indexOfOrThrow

functionString.indexOfOrThrow(target: string,source: string,): number

Returns the index of the first occurrence of source string in target string, or throws an error if not found.

Example

import{String}from"@monstermann/string";String.indexOfOrThrow("hello world","world");// 6String.indexOfOrThrow("hello world","foo");// throws FnError
import{String}from"@monstermann/string";pipe("hello world",String.indexOfOrThrow("world"));// 6pipe("hello world",String.indexOfOrThrow("foo"));// throws FnError

is

functionString.is(target: unknown): target is string

Checks if target is a string.

Example

import{String}from"@monstermann/string";String.is("hello");// trueString.is(123);// falseString.is(null);// false
import{String}from"@monstermann/string";pipe("hello",String.is());// truepipe(123,String.is());// falsepipe(null,String.is());// false

isASCII

functionString.isASCII(target: string): boolean

Checks if target string contains only ASCII characters (U+0000 to U+007F).

Returns true if all characters in the string are within the ASCII range, false otherwise.

Example

import{String}from"@monstermann/string";String.isASCII("hello world");// trueString.isASCII("café");// falseString.isASCII("123!@#");// trueString.isASCII("hello 🌍");// false
import{String}from"@monstermann/string";pipe("hello world",String.isASCII());// truepipe("café",String.isASCII());// falsepipe("123!@#",String.isASCII());// truepipe("hello 🌍",String.isASCII());// false

isEmpty

functionString.isEmpty(target: string): boolean

Checks if target string is empty.

Example

import{String}from"@monstermann/string";String.isEmpty("");// trueString.isEmpty("hello");// false
import{String}from"@monstermann/string";pipe("",String.isEmpty());// truepipe("hello",String.isEmpty());// false

kebabCase

functionString.kebabCase<Textendsstring>(target: T,): KebabCase<T>

Converts target string to kebab-case format.

Example

import{String}from"@monstermann/string";String.kebabCase("hello world");// "hello-world"String.kebabCase("helloWorld");// "hello-world"
import{String}from"@monstermann/string";pipe("hello world",String.kebabCase());// "hello-world"pipe("helloWorld",String.kebabCase());// "hello-world"

lastIndexOf

functionString.lastIndexOf(target: string,source: string): number

Returns the index of the last occurrence of source string in target string, or -1 if not found.

Example

import{String}from"@monstermann/string";String.lastIndexOf("hello world hello","hello");// 12
import{String}from"@monstermann/string";pipe("hello world hello",String.lastIndexOf("hello"));// 12

lastIndexOfOr

functionString.lastIndexOfOr<T>(target: string,source: string,or: T,): number|T

Returns the index of the last occurrence of source string in target string, or the or value if not found.

Example

import{String}from"@monstermann/string";String.lastIndexOfOr("hello world hello","hello",-1);// 12String.lastIndexOfOr("hello world","foo",-1);// -1
import{String}from"@monstermann/string";pipe("hello world hello",String.lastIndexOfOr("hello",-1));// 12pipe("hello world",String.lastIndexOfOr("foo",-1));// -1

lastIndexOfOrElse

functionString.lastIndexOfOrElse<T>(target: string,source: string,orElse: (target: string)=>T,): number|T

Returns the index of the last occurrence of source string in target string, or the result of calling orElse function with target if not found.

Example

import{String}from"@monstermann/string";String.lastIndexOfOrElse("hello world hello","hello",()=>-1);// 12String.lastIndexOfOrElse("hello world","foo",(str)=>str.length);// 11
import{String}from"@monstermann/string";pipe("hello world hello",String.lastIndexOfOrElse("hello",()=>-1),);// 12pipe("hello world",String.lastIndexOfOrElse("foo",(str)=>str.length),);// 11

lastIndexOfOrThrow

functionString.lastIndexOfOrThrow(target: string,source: string,): number

Returns the index of the last occurrence of source string in target string, or throws an error if not found.

Example

import{String}from"@monstermann/string";String.lastIndexOfOrThrow("hello world hello","hello");// 12String.lastIndexOfOrThrow("hello world","foo");// throws FnError
import{String}from"@monstermann/string";pipe("hello world hello",String.lastIndexOfOrThrow("hello"));// 12pipe("hello world",String.lastIndexOfOrThrow("foo"));// throws FnError

length

functionString.length(target: string): number

Returns the length of target string.

Example

import{String}from"@monstermann/string";String.length("hello world");// 11
import{String}from"@monstermann/string";pipe("hello world",String.length());// 11

lowerCase

functionString.lowerCase<Textendsstring>(target: T,): Lowercase<T>

Converts target string to lowercase.

Example

import{String}from"@monstermann/string";String.lowerCase("HELLO WORLD");// "hello world"String.lowerCase("Hello World");// "hello world"
import{String}from"@monstermann/string";pipe("HELLO WORLD",String.lowerCase());// "hello world"pipe("Hello World",String.lowerCase());// "hello world"

match

functionString.match(target: string,source: string|RegExp,): RegExpMatchArray|null

Returns the result of matching target string against source string or regular expression, or null if no match is found.

Example

import{String}from"@monstermann/string";String.match("hello world","world");// ["world", index: 6, input: "hello world", groups: undefined]String.match("hello world",/\d+/);// null
import{String}from"@monstermann/string";pipe("hello world",String.match("world"));// ["world", index: 6, input: "hello world", groups: undefined]pipe("hello world",String.match(/\d+/));// null

matchOr

functionString.matchOr<T>(target: string,source: string|RegExp,or: T,): RegExpMatchArray|T

Returns the result of matching target string against source string or regular expression, or the or value if no match is found.

Example

import{String}from"@monstermann/string";String.matchOr("hello world","world",[]);// ["world", index: 6, input: "hello world", groups: undefined]String.matchOr("hello world",/\d+/,[]);// []
import{String}from"@monstermann/string";pipe("hello world",String.matchOr("world",[]));// ["world", index: 6, input: "hello world", groups: undefined]pipe("hello world",String.matchOr(/\d+/,[]));// []

matchOrElse

functionString.matchOrElse<T>(target: string,source: string|RegExp,orElse: (target: string)=>T,): RegExpMatchArray|T

Returns the result of matching target string against source string or regular expression, or the result of calling orElse function with target if no match is found.

Example

import{String}from"@monstermann/string";String.matchOrElse("hello world","world",()=>[]);// ["world", index: 6, input: "hello world", groups: undefined]String.matchOrElse("hello world",/\d+/,(str)=>[str]);// ["hello world"]
import{String}from"@monstermann/string";pipe("hello world",String.matchOrElse("world",()=>[]),);// ["world", index: 6, input: "hello world", groups: undefined]pipe("hello world",String.matchOrElse(/\d+/,(str)=>[str]),);// ["hello world"]

matchOrThrow

functionString.matchOrThrow(target: string,source: string|RegExp,): RegExpMatchArray

Returns the result of matching target string against source string or regular expression, or throws an error if no match is found.

Example

import{String}from"@monstermann/string";String.matchOrThrow("hello world","world");// ["world", index: 6, input: "hello world", groups: undefined]String.matchOrThrow("hello world",/\d+/);// throws FnError
import{String}from"@monstermann/string";pipe("hello world",String.matchOrThrow("world"));// ["world", index: 6, input: "hello world", groups: undefined]pipe("hello world",String.matchOrThrow(/\d+/));// throws FnError

padEnd

functionString.padEnd(target: string,length: number,fill: string,): string

Pads target string from the end with fill string until the result reaches the specified length.

Example

import{String}from"@monstermann/string";String.padEnd("hello",10," ");// "hello "
import{String}from"@monstermann/string";pipe("hello",String.padEnd(10," "));// "hello "

padStart

functionString.padStart(target: string,length: number,fill: string,): string

Pads target string from the start with fill string until the result reaches the specified length.

Example

import{String}from"@monstermann/string";String.padStart("hello",10," ");// " hello"
import{String}from"@monstermann/string";pipe("hello",String.padStart(10," "));// " hello"

parseFloat

functionString.parseFloat(target: string): number

Parses target string and returns a floating point number.

Example

import{String}from"@monstermann/string";String.parseFloat("3.14");// 3.14String.parseFloat("42.5abc");// 42.5
import{String}from"@monstermann/string";pipe("3.14",String.parseFloat());// 3.14pipe("42.5abc",String.parseFloat());// 42.5

parseFloatOr

functionString.parseFloatOr<T>(target: string,or: T): number|T

Parses target string and returns a floating point number, or the or value if parsing fails.

Example

import{String}from"@monstermann/string";String.parseFloatOr("3.14",0);// 3.14String.parseFloatOr("abc",0);// 0
import{String}from"@monstermann/string";pipe("3.14",String.parseFloatOr(0));// 3.14pipe("abc",String.parseFloatOr(0));// 0

parseFloatOrElse

functionString.parseFloatOrElse<T>(target: string,orElse: (target: string)=>T,): number|T

Parses target string and returns a floating point number, or the result of calling orElse function with target if parsing fails.

Example

import{String}from"@monstermann/string";String.parseFloatOrElse("3.14",()=>0);// 3.14String.parseFloatOrElse("abc",(str)=>str.length);// 3
import{String}from"@monstermann/string";pipe("3.14",String.parseFloatOrElse(()=>0),);// 3.14pipe("abc",String.parseFloatOrElse((str)=>str.length),);// 3

parseFloatOrThrow

functionString.parseFloatOrThrow(target: string): number

Parses target string and returns a floating point number, or throws an error if parsing fails.

Example

import{String}from"@monstermann/string";String.parseFloatOrThrow("3.14");// 3.14String.parseFloatOrThrow("abc");// throws FnError
import{String}from"@monstermann/string";pipe("3.14",String.parseFloatOrThrow());// 3.14pipe("abc",String.parseFloatOrThrow());// throws FnError

parseInt

functionString.parseInt(target: string): number

Parses target string and returns an integer.

Example

import{String}from"@monstermann/string";String.parseInt("42");// 42String.parseInt("42.5");// 42
import{String}from"@monstermann/string";pipe("42",String.parseInt());// 42pipe("42.5",String.parseInt());// 42

parseIntOr

functionString.parseIntOr<T>(target: string,or: T): number|T

Parses target string and returns an integer, or the or value if parsing fails.

Example

import{String}from"@monstermann/string";String.parseIntOr("42",0);// 42String.parseIntOr("abc",0);// 0
import{String}from"@monstermann/string";pipe("42",String.parseIntOr(0));// 42pipe("abc",String.parseIntOr(0));// 0

parseIntOrElse

functionString.parseIntOrElse<T>(target: string,orElse: (target: string)=>T,): number|T

Parses target string and returns an integer, or the result of calling orElse function with target if parsing fails.

Example

import{String}from"@monstermann/string";String.parseIntOrElse("42",()=>0);// 42String.parseIntOrElse("abc",(str)=>str.length);// 3
import{String}from"@monstermann/string";pipe("42",String.parseIntOrElse(()=>0),);// 42pipe("abc",String.parseIntOrElse((str)=>str.length),);// 3

parseIntOrThrow

functionString.parseIntOrThrow(target: string): number

Parses target string and returns an integer, or throws an error if parsing fails.

Example

import{String}from"@monstermann/string";String.parseIntOrThrow("42");// 42String.parseIntOrThrow("abc");// throws FnError
import{String}from"@monstermann/string";pipe("42",String.parseIntOrThrow());// 42pipe("abc",String.parseIntOrThrow());// throws FnError

pascalCase

functionString.pascalCase<Textendsstring>(target: T,): PascalCase<T>

Converts target string to PascalCase format.

Example

import{String}from"@monstermann/string";String.pascalCase("hello world");// "HelloWorld"String.pascalCase("hello-world");// "HelloWorld"
import{String}from"@monstermann/string";pipe("hello world",String.pascalCase());// "HelloWorld"pipe("hello-world",String.pascalCase());// "HelloWorld"

prepend

functionString.prepend(target: string,source: Iterable<string>,): string

Prepends string or strings from source iterable to the beginning of target string.

Example

import{String}from"@monstermann/string";String.prepend("world","hello ");// "hello world"String.prepend("world",["hello"," "]);// "hello world"
import{String}from"@monstermann/string";pipe("world",String.prepend("hello "));// "hello world"pipe("world",String.prepend(["hello"," "]));// "hello world"

repeat

functionString.repeat<Textendsstring,Uextendsnumber>(target: T,amount: U,): Repeat<T,U>

Repeats target string amount times.

Example

import{String}from"@monstermann/string";String.repeat("hello",3);// "hellohellohello"
import{String}from"@monstermann/string";pipe("hello",String.repeat(3));// "hellohellohello"

replace

functionString.replace<Textendsstring,Uextendsstring|RegExp,Vextendsstring,>(target: T,search: U,replacement: V): Replace<T,U,V>

Replaces the first occurrence of search string or regular expression in target string with replace string.

Example

import{String}from"@monstermann/string";String.replace("hello world","world","universe");// "hello universe"String.replace("hello world",/o/g,"0");// "hell0 w0rld"
import{String}from"@monstermann/string";pipe("hello world",String.replace("world","universe"));// "hello universe"pipe("hello world",String.replace(/o/g,"0"));// "hell0 w0rld"

replaceAll

functionString.replaceAll<Textendsstring,Uextendsstring|RegExp,Vextendsstring,>(target: T,search: U,replacement: V): ReplaceAll<T,U,V>

Replaces all occurrences of search string or regular expression in target string with replace string.

Example

import{String}from"@monstermann/string";String.replaceAll("hello world world","world","universe");// "hello universe universe"String.replaceAll("hello world",/o/g,"0");// "hell0 w0rld"
import{String}from"@monstermann/string";pipe("hello world world",String.replaceAll("world","universe"));// "hello universe universe"pipe("hello world",String.replaceAll(/o/g,"0"));// "hell0 w0rld"

reverse

functionString.reverse<Textendsstring>(target: T): Reverse<T>

Reverses the characters in target string.

Example

import{String}from"@monstermann/string";String.reverse("hello world");// "dlrow olleh"String.reverse("abc");// "cba"
import{String}from"@monstermann/string";pipe("hello world",String.reverse());// "dlrow olleh"pipe("abc",String.reverse());// "cba"

slice

functionString.slice<Textendsstring,Uextendsnumber,Vextendsnumber|undefined=undefined,>(target: T,start: U,end?: V): Slice<T,U,V>

Extracts a section of target string from start index to end index (exclusive). If end is not provided, extracts to the end of the string.

Example

import{String}from"@monstermann/string";String.slice("hello world",0,5);// "hello"String.slice("hello world",6,11);// "world"String.slice("hello world",6);// "world"
import{String}from"@monstermann/string";pipe("hello world",String.slice(0,5));// "hello"pipe("hello world",String.slice(6,11));// "world"pipe("hello world",String.slice(6));// "world"

slugify

functionString.slugify(target: string): string

Converts target string to a URL-friendly slug format.

The function removes diacritical marks, converts to lowercase, replaces whitespace and separators with hyphens, removes non-alphanumeric characters, and cleans up multiple or leading/trailing hyphens.

Example

import{String}from"@monstermann/string";String.slugify("Hello World");// "hello-world"String.slugify("Café au Lait");// "cafe-au-lait"String.slugify(" Multiple Spaces ");// "multiple-spaces"String.slugify("Special!@#Characters");// "specialcharacters"String.slugify("foo_bar/baz");// "foo-bar-baz"
import{String}from"@monstermann/string";pipe("Hello World",String.slugify());// "hello-world"pipe("Café au Lait",String.slugify());// "cafe-au-lait"pipe(" Multiple Spaces ",String.slugify());// "multiple-spaces"pipe("Special!@#Characters",String.slugify());// "specialcharacters"pipe("foo_bar/baz",String.slugify());// "foo-bar-baz"

snakeCase

functionString.snakeCase<Textendsstring>(target: T,): SnakeCase<T>

Converts target string to snake_case format.

Example

import{String}from"@monstermann/string";String.snakeCase("hello world");// "hello_world"String.snakeCase("helloWorld");// "hello_world"
import{String}from"@monstermann/string";pipe("hello world",String.snakeCase());// "hello_world"pipe("helloWorld",String.snakeCase());// "hello_world"

split

functionString.split<Textendsstring,UextendsRegExp>(target: T,delimiter: U,): string[]

Splits target string into an array of substrings using source string or regular expression as the separator.

Example

import{String}from"@monstermann/string";String.split("hello,world,test",",");// ["hello", "world", "test"]String.split("hello world",/\s+/);// ["hello", "world"]
import{String}from"@monstermann/string";pipe("hello,world,test",String.split(","));// ["hello", "world", "test"]pipe("hello world",String.split(/\s+/));// ["hello", "world"]

startsWith

functionString.startsWith(target: string,source: string): boolean

Checks if target string starts with source string.

Example

import{String}from"@monstermann/string";String.startsWith("hello world","hello");// trueString.startsWith("hello world","world");// false
import{String}from"@monstermann/string";pipe("hello world",String.startsWith("hello"));// truepipe("hello world",String.startsWith("world"));// false

take

functionString.take(target: string,amount: number): string

Takes the first amount characters from target string.

Example

import{String}from"@monstermann/string";String.take("hello world",5);// "hello"
import{String}from"@monstermann/string";pipe("hello world",String.take(5));// "hello"

takeLast

functionString.takeLast(target: string,amount: number): string

Takes the last amount characters from target string.

Example

import{String}from"@monstermann/string";String.takeLast("hello world",5);// "world"
import{String}from"@monstermann/string";pipe("hello world",String.takeLast(5));// "world"

test

functionString.test(target: string,source: RegExp): boolean

Tests if target string matches the source regular expression.

Example

import{String}from"@monstermann/string";String.test("hello world",/world/);// trueString.test("hello world",/\d+/);// false
import{String}from"@monstermann/string";pipe("hello world",String.test(/world/));// truepipe("hello world",String.test(/\d+/));// false

titleCase

functionString.titleCase<Textendsstring>(target: T,): TitleCase<T>

Converts target string to Title Case format.

Example

import{String}from"@monstermann/string";String.titleCase("hello world");// "Hello World"String.titleCase("hello-world");// "Hello World"
import{String}from"@monstermann/string";pipe("hello world",String.titleCase());// "Hello World"pipe("hello-world",String.titleCase());// "Hello World"

trim

functionString.trim<Textendsstring>(target: T): Trim<T>

Removes whitespace from both ends of target string.

Example

import{String}from"@monstermann/string";String.trim(" hello world ");// "hello world"
import{String}from"@monstermann/string";pipe(" hello world ",String.trim());// "hello world"

trimEnd

functionString.trimEnd<Textendsstring>(target: T): TrimEnd<T>

Removes whitespace from the end of target string.

Example

import{String}from"@monstermann/string";String.trimEnd(" hello world ");// " hello world"
import{String}from"@monstermann/string";pipe(" hello world ",String.trimEnd());// " hello world"

trimStart

functionString.trimStart<Textendsstring>(target: T,): TrimStart<T>

Removes whitespace from the start of target string.

Example

import{String}from"@monstermann/string";String.trimStart(" hello world ");// "hello world "
import{String}from"@monstermann/string";pipe(" hello world ",String.trimStart());// "hello world "

uncapitalize

functionString.uncapitalize<Textendsstring>(target: T,): Uncapitalize<T>

Uncapitalizes the first letter of target string.

Example

import{String}from"@monstermann/string";String.uncapitalize("Hello World");// "hello World"String.uncapitalize("Hello");// "hello"
import{String}from"@monstermann/string";pipe("Hello World",String.uncapitalize());// "hello World"pipe("Hello",String.uncapitalize());// "hello"

upperCase

functionString.upperCase<Textendsstring>(target: T,): Uppercase<T>

Converts target string to uppercase.

Example

import{String}from"@monstermann/string";String.upperCase("hello world");// "HELLO WORLD"String.upperCase("Hello World");// "HELLO WORLD"
import{String}from"@monstermann/string";pipe("hello world",String.upperCase());// "HELLO WORLD"pipe("Hello World",String.upperCase());// "HELLO WORLD"

About

Functional utilities for strings.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages