locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
18 lines (17 loc) • 763 B
JavaScript
export function insert(str, startIndex, value) {
// discuss at: https://locutus.io/powershell/insert/
// parity verified: PowerShell 7.4
// original by: Kevin van Zonneveld (https://kvz.io)
// example 1: insert('HelloWorld', 5, ' ')
// returns 1: 'Hello World'
// example 2: insert('abcd', 0, 'X')
// returns 2: 'Xabcd'
// example 3: insert('abcd', 4, '!')
// returns 3: 'abcd!'
const source = String(str);
const start = Math.trunc(Number(startIndex));
if (!Number.isFinite(start) || start < 0 || start > source.length) {
throw new RangeError('insert(): startIndex out of range');
}
return source.slice(0, start) + String(value) + source.slice(start);
}