Skip to content

Modules | @vltpkg/fast-split

Type Aliases

NullReplace<T, R>

type NullReplace<T, R>: T extends NonNullable<T> ? T : NonNullable<T> | R;

Utility type to replace null/undefined with a given type

Type Parameters

T

R = string

Defined in

index.ts:5


NullToString<T>

type NullToString<T>: VoidReplace<T>;

utility types to turn a null/undefined/void return into string

Type Parameters

T

Defined in

index.ts:2


VoidReplace<T, R>

type VoidReplace<T, R>: undefined extends T ? NullReplace<Exclude<T, void>, R> | R : NullReplace<T, R>;

Utility type to replace void with a given type

Type Parameters

T

R = string

Defined in

index.ts:9

Functions

fastSplit()

fastSplit(str, delim, limit, onPart)

function fastSplit<T>(str, delim, limit, onPart): NullToString<T>[]

Split a string by a string delimiter, optionally limiting the number of parts parsed, and/or transforming the string parts into some other type of value.

Pass -1 as the limit parameter to get all parts (useful if an onPart method is provided)

If an onPart method is provided, and returns undefined, then the original string part is included in the result set.

import { fastSplit } from '@vltpkg/fast-split'
// say we want to split a string on '.' characters
const str = getSomeStringSomehow()
// basic usage, just like str.split('.'), gives us an array
const parts = fastSplit(str, '.')
// get just the first two parts, leave the rest intact
// Note: unlike str.split('.', 3), the 'rest' here will
// include the entire rest of the string.
// If you do `str.split('.', 3)`, then the last item in the
// returned array is truncated at the next delimiter
const [first, second, rest] = fastSplit(str, '.', 3)
// If you need to transform it, say if it's an IPv4 address
// that you want to turn into numbers, you can do that by
// providing the onPart method, which will be slightly faster
// than getting an array and subsequently looping over it
// pass `-1` as the limit to give us all parts
const nums = fastSplit(str, '.', -1, (part, parts, index) =>
Number(s),
)
Type Parameters

T = string

Parameters

str: string

delim: string

limit: number

onPart

Returns

NullToString<T>[]

Defined in

index.ts:48

fastSplit(str, delim, limit)

function fastSplit(str, delim, limit?): string[]
Parameters

str: string

delim: string

limit?: number

Returns

string[]

Defined in

index.ts:54