vlt / docs

  • PricingBenchmarks (opens in new window)Community (opens in new window)Feedback
  • Overview
    • Overview
    • Reference
  • Type Aliases
  • NullReplace<T, R>
  • NullToString<T>
  • VoidReplace<T, R>
  • Functions
  • fastSplit()
  1. Client
  2. /
  3. API Reference
  4. /
  5. @vltpkg/fast-split
  6. /
  7. Reference

Reference

Type Aliases

NullReplace<T, R>

TypeScript
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 ParameterDefault type
T-
Rstring

NullToString<T>

TypeScript
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>

TypeScript
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 ParameterDefault type
T-
Rstring

Functions

fastSplit()

Call Signature

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

Defined in: index.ts:48

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.

TypeScript
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
Type ParameterDefault type
Tstring
Parameters
ParameterType
strstring
delimstring
limitnumber
onPart(part, parts, i) => T
Returns

VoidReplace<T, string>[]

Call Signature

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

Defined in: index.ts:54

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.

TypeScript
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))
Parameters
ParameterType
strstring
delimstring
limit?number
Returns

string[]


Previous@vltpkg/fast-splitNext@vltpkg/git

On this page

  • Type Aliases
  • NullReplace<T, R>
  • NullToString<T>
  • VoidReplace<T, R>
  • Functions
  • fastSplit()

Deploy your package on vlt.io

Publish scoped and private packages, manage organizations and access, and give every developer and CI environment a consistent source for public and private JavaScript dependencies.

Publish now