Swift's String.split in TypeScript

How to use

Install via yarn add locutus and import: import { split } from 'locutus/swift/String/split'.

Or with CommonJS: const { split } = require('locutus/swift/String/split')

Use a bundler that supports tree-shaking so you only ship the functions you actually use. Vite, webpack, Rollup, and Parcel all handle this. For server-side use this is less of a concern.

Examples

These examples are extracted from test cases that automatically verify our functions against their native counterparts.

#codeexpected result
1split('a,b,c', ',')['a', 'b', 'c']
2split('a,,b,', ',', Number.MAX_SAFE_INTEGER, false)['a', '', 'b', '']
3split('a,b,c,d', ',', 2)['a', 'b', 'c,d']

Notes

  • Mirrors Swift String.split semantics with configurable max splits and empty-subsequence handling.

Here's what our current TypeScript equivalent to Swift's String.split looks like.

export function split(
str: string,
separator: string,
maxSplits: number = Number.MAX_SAFE_INTEGER,
omittingEmptySubsequences: boolean = true,
): string[] {
// discuss at: https://locutus.io/swift/String/split/
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Mirrors Swift String.split semantics with configurable max splits and empty-subsequence handling.
// example 1: split('a,b,c', ',')
// returns 1: ['a', 'b', 'c']
// example 2: split('a,,b,', ',', Number.MAX_SAFE_INTEGER, false)
// returns 2: ['a', '', 'b', '']
// example 3: split('a,b,c,d', ',', 2)
// returns 3: ['a', 'b', 'c,d']

const source = String(str)
const needle = String(separator)
const max = Number.isFinite(maxSplits) && maxSplits >= 0 ? Math.floor(maxSplits) : Number.MAX_SAFE_INTEGER

if (needle === '') {
const chars = Array.from(source)
return omittingEmptySubsequences ? chars.filter((part) => part.length > 0) : chars
}

const out: string[] = []
let start = 0
let splits = 0

while (splits < max) {
const next = source.indexOf(needle, start)
if (next < 0) {
break
}
const part = source.slice(start, next)
if (!omittingEmptySubsequences || part.length > 0) {
out.push(part)
}
start = next + needle.length
splits += 1
}

const tail = source.slice(start)
if (!omittingEmptySubsequences || tail.length > 0) {
out.push(tail)
}

return out
}

Improve this function

Locutus is a community effort following The McDonald's Theory: we ship first iterations, hoping others will improve them. If you see something that could be better, we'd love your contribution.

View on GitHub · Edit on GitHub · View Raw


We have 2 Swift functions so far - help us add more

Got a rainy Sunday afternoon and a taste for a porting puzzle?

  • Get inspiration from the Swift documentation.
  • Click "New file" in the appropriate folder on GitHub. This will fork the project to your account, directly add the file to it, and send a Pull Request to us.

We will then review it. If it's useful to the project and in line with our contributing guidelines your work will become part of Locutus and you'll be automatically credited in the authors section accordingly.


« More Swift String functions


Star