locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
34 lines (33 loc) • 1.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.split_inclusive = split_inclusive;
function split_inclusive(delimiter, str) {
// discuss at: https://locutus.io/rust/split_inclusive/
// parity verified: Rust 1.85
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Splits by delimiter and keeps delimiter as suffix of each non-final match chunk.
// example 1: split_inclusive(',', 'a,b,c')
// returns 1: ['a,', 'b,', 'c']
// example 2: split_inclusive('::', 'x::y::z')
// returns 2: ['x::', 'y::', 'z']
// example 3: split_inclusive('', 'ab')
// returns 3: ['', 'a', 'b']
const haystack = String(str);
const needle = String(delimiter);
if (needle === '') {
return ['', ...Array.from(haystack)];
}
const out = [];
let start = 0;
while (start < haystack.length) {
const found = haystack.indexOf(needle, start);
if (found < 0) {
out.push(haystack.slice(start));
break;
}
const end = found + needle.length;
out.push(haystack.slice(start, end));
start = end;
}
return out;
}