---
title: "@vltpkg/fast-split: Reference — vlt docs"
description: "Documentation for the vlt client and the vlt registry."
url: "https://docs.vlt.io/client/api-reference/fast-split/reference"
image: "https://docs.vlt.io/og/client/api-reference/fast-split/reference"
lang: "en"
---

# Reference

## Type Aliases

### NullReplace\<T, R>

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

Defined in: index.ts:5

Utility type to replace null/undefined with a given type

#### Type Parameters

| Type Parameter | Default type |
| -------------- | ------------ |
| `T`            | -            |
| `R`            | `string`     |

---

### NullToString\<T>

```ts
type NullToString<T> = VoidReplace<T>;
```

Defined in: index.ts:2

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

#### Type Parameters

| Type Parameter |
| -------------- |
| `T`            |

---

### VoidReplace\<T, R>

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

Defined in: index.ts:9

Utility type to replace void with a given type

#### Type Parameters

| Type Parameter | Default type |
| -------------- | ------------ |
| `T`            | -            |
| `R`            | `string`     |

## Functions

### fastSplit()

#### Call Signature

```ts
function fastSplit<T>(
   str, 
   delim, 
   limit, 
   onPart): VoidReplace<T, string>[]
```

Defined in: index.ts:77

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.

```ts
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(part))
```

##### Type Parameters

| Type Parameter | Default type |
| -------------- | ------------ |
| `T`            | `string`     |

##### Parameters

| Parameter | Type                          |
| --------- | ----------------------------- |
| `str`     | `string`                      |
| `delim`   | `string`                      |
| `limit`   | `number`                      |
| `onPart`  | (`part`, `parts`, `i`) => `T` |

##### Returns

[`VoidReplace`](https://docs.vlt.io/client/api-reference/fast-split/reference.md#voidreplacet-r)<`T`, `string`>\[]

#### Call Signature

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

Defined in: index.ts:83

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.

```ts
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(part))
```

##### Parameters

| Parameter | Type     |
| --------- | -------- |
| `str`     | `string` |
| `delim`   | `string` |
| `limit`?  | `number` |

##### Returns

`string`\[]
