Go's strings.SplitN in TypeScript

✓ Verified: Go 1.23
Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.

How to use

Install via yarn add locutus and import: import { SplitN } from 'locutus/golang/strings/SplitN'.

Or with CommonJS: const { SplitN } = require('locutus/golang/strings/SplitN')

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
1SplitN('a,b,c', ',', 2)['a', 'b,c']
2SplitN('a,b,c', ',', -1)['a', 'b', 'c']
3SplitN('hello', '', 3)['h', 'e', 'llo']
4SplitN('☺☻☹', '', 17)['☺', '☻', '☹']

Notes

  • Splits into at most n parts. n < 0 means no limit, n == 0 returns [].

  • Includes edge cases adapted from Go’s src/strings/strings_test.go.

Here's what our current TypeScript equivalent to Go's strings.SplitN looks like.

export function SplitN(s: string, sep: string, n: number): string[] {
// discuss at: https://locutus.io/golang/strings/SplitN
// parity verified: Go 1.23
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Splits into at most n parts. n < 0 means no limit, n == 0 returns [].
// note 2: Includes edge cases adapted from Go's src/strings/strings_test.go.
// example 1: SplitN('a,b,c', ',', 2)
// returns 1: ['a', 'b,c']
// example 2: SplitN('a,b,c', ',', -1)
// returns 2: ['a', 'b', 'c']
// example 3: SplitN('hello', '', 3)
// returns 3: ['h', 'e', 'llo']
// example 4: SplitN('☺☻☹', '', 17)
// returns 4: ['☺', '☻', '☹']

const value = String(s)
const delimiter = String(sep)
const limit = Math.trunc(Number(n))

if (limit === 0) {
return []
}

if (delimiter === '') {
const chars = Array.from(value)

if (limit < 0) {
return chars
}

if (limit === 1) {
return [value]
}

if (chars.length < limit) {
return chars
}

return [...chars.slice(0, limit - 1), chars.slice(limit - 1).join('')]
}

if (limit < 0) {
return value.split(delimiter)
}

if (limit === 1) {
return [value]
}

const out: string[] = []
let remainder = value

for (let i = 0; i < limit - 1; i += 1) {
const index = remainder.indexOf(delimiter)
if (index < 0) {
break
}

out.push(remainder.slice(0, index))
remainder = remainder.slice(index + delimiter.length)
}

out.push(remainder)
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


Help us add more

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

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 Go strings functions


Star