-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspliceStr.js
More file actions
29 lines (28 loc) · 895 Bytes
/
spliceStr.js
File metadata and controls
29 lines (28 loc) · 895 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
export default function splicedStr(s, i, val) {
const {start, end} = s.split('')
.reduce(function({start, end}, char, charI) {
if (charI < i) start = start.concat(char)
else end = end.concat(char)
return {start, end}
}, {start: '', end: ''})
return start
.concat(val)
.concat(end)
}
if (!String.prototype.splice) {
/**
* {JSDoc}
*
* This method takes the current string, splits it in half at the given index,
* and inserts a string (val) in between. It returns the new string
* and does not modify the original string
*
* @this {String}
* @param {number} i Index at which to split the string
* @param {string} val The value to insert at the given index
* @return {string} The new string with inserted value at the given index
*/
String.prototype.splice =
function(i, val) {
return splicedStr(this, i, val)}
}