1
1
// Inspiration: https://github.com/facebook/react/issues/3386
2
2
3
- function replace ( string , regexpOrSubstr , newValueOrFn ) {
3
+ function replace ( string , regexpOrSubstr , newValueOrFn , globalOffset ) {
4
4
if ( typeof string !== 'string' ) throw new Error ( 'First param must be a string' )
5
5
if ( typeof regexpOrSubstr !== 'string' && ! ( regexpOrSubstr instanceof RegExp ) ) throw new Error ( 'Second param must be a string pattern or a regular expression' )
6
6
7
7
var fn = ( typeof regexpOrSubstr === 'string' ) ? replaceUsingString : replaceUsingRegexp
8
8
9
- return fn ( string , regexpOrSubstr , newValueOrFn )
9
+ return fn ( string , regexpOrSubstr , newValueOrFn , globalOffset )
10
10
}
11
11
12
- function replaceUsingString ( string , patternString , newValueOrFn ) {
12
+ function replaceUsingString ( string , patternString , newValueOrFn , globalOffset ) {
13
13
var index = string . indexOf ( patternString )
14
14
15
15
if ( index >= 0 ) {
@@ -24,7 +24,7 @@ function replaceUsingString (string, patternString, newValueOrFn) {
24
24
( typeof newValueOrFn === 'function' ) ?
25
25
newValueOrFn (
26
26
string . substring ( index , endIndex ) ,
27
- index ,
27
+ index + globalOffset ,
28
28
string
29
29
) :
30
30
newValueOrFn
@@ -40,7 +40,7 @@ function replaceUsingString (string, patternString, newValueOrFn) {
40
40
}
41
41
}
42
42
43
- function replaceUsingRegexp ( string , regexp , newValueOrFn ) {
43
+ function replaceUsingRegexp ( string , regexp , newValueOrFn , globalOffset ) {
44
44
var output = [ ]
45
45
46
46
var replacerIsFn = ( typeof newValueOrFn === 'function' )
@@ -68,7 +68,7 @@ function replaceUsingRegexp (string, regexp, newValueOrFn) {
68
68
lastIndex = index + match . length
69
69
70
70
var out = replacerIsFn ?
71
- newValueOrFn . apply ( this , result . concat ( index , result . input ) ) :
71
+ newValueOrFn . apply ( this , result . concat ( index + globalOffset , result . input ) ) :
72
72
newValueOrFn
73
73
output . push ( out )
74
74
@@ -87,16 +87,21 @@ function replaceUsingRegexp (string, regexp, newValueOrFn) {
87
87
88
88
module . exports = function stringReplaceToArray ( stringOrArray , regexpOrSubstr , newSubStrOrFn ) {
89
89
if ( typeof stringOrArray === 'string' ) {
90
- return replace ( stringOrArray , regexpOrSubstr , newSubStrOrFn )
90
+ return replace ( stringOrArray , regexpOrSubstr , newSubStrOrFn , 0 )
91
91
} else if ( ! Array . isArray ( stringOrArray ) || ! stringOrArray [ 0 ] ) {
92
92
throw new TypeError ( 'First argument must be an array or non-empty string' )
93
93
} else {
94
94
var len = stringOrArray . length
95
95
var output = [ ]
96
+ var globalOffset = 0
96
97
for ( var i = 0 ; i < len ; ++ i ) {
97
98
var arrayItem = stringOrArray [ i ]
98
- if ( typeof arrayItem !== 'string' ) output . push ( arrayItem )
99
- else output . push . apply ( output , replace ( arrayItem , regexpOrSubstr , newSubStrOrFn ) )
99
+ if ( typeof arrayItem === 'string' ) {
100
+ output . push . apply ( output , replace ( arrayItem , regexpOrSubstr , newSubStrOrFn , globalOffset ) )
101
+ globalOffset += arrayItem . length
102
+ } else {
103
+ output . push ( arrayItem )
104
+ }
100
105
}
101
106
return output
102
107
}
0 commit comments