Skip to content

Commit 7722e3d

Browse files
committed
Fixed issue where the offset was not truly global and unique
1 parent 85bd42b commit 7722e3d

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "string-replace-to-array",
3-
"version": "2.0.1",
3+
"version": "2.1.0",
44
"description": "Works like String.prototype.replace but outputs an array. Useful for replacing parts of the string with objects of other types.",
55
"main": "string-replace-to-array.js",
66
"repository": {

string-replace-to-array.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// Inspiration: https://github.com/facebook/react/issues/3386
22

3-
function replace (string, regexpOrSubstr, newValueOrFn) {
3+
function replace (string, regexpOrSubstr, newValueOrFn, globalOffset) {
44
if (typeof string !== 'string') throw new Error('First param must be a string')
55
if (typeof regexpOrSubstr !== 'string' && !(regexpOrSubstr instanceof RegExp)) throw new Error('Second param must be a string pattern or a regular expression')
66

77
var fn = (typeof regexpOrSubstr === 'string') ? replaceUsingString : replaceUsingRegexp
88

9-
return fn(string, regexpOrSubstr, newValueOrFn)
9+
return fn(string, regexpOrSubstr, newValueOrFn, globalOffset)
1010
}
1111

12-
function replaceUsingString (string, patternString, newValueOrFn) {
12+
function replaceUsingString (string, patternString, newValueOrFn, globalOffset) {
1313
var index = string.indexOf(patternString)
1414

1515
if (index >= 0) {
@@ -24,7 +24,7 @@ function replaceUsingString (string, patternString, newValueOrFn) {
2424
(typeof newValueOrFn === 'function') ?
2525
newValueOrFn(
2626
string.substring(index, endIndex),
27-
index,
27+
index + globalOffset,
2828
string
2929
) :
3030
newValueOrFn
@@ -40,7 +40,7 @@ function replaceUsingString (string, patternString, newValueOrFn) {
4040
}
4141
}
4242

43-
function replaceUsingRegexp (string, regexp, newValueOrFn) {
43+
function replaceUsingRegexp (string, regexp, newValueOrFn, globalOffset) {
4444
var output = []
4545

4646
var replacerIsFn = (typeof newValueOrFn === 'function')
@@ -68,7 +68,7 @@ function replaceUsingRegexp (string, regexp, newValueOrFn) {
6868
lastIndex = index + match.length
6969

7070
var out = replacerIsFn ?
71-
newValueOrFn.apply(this, result.concat(index, result.input)) :
71+
newValueOrFn.apply(this, result.concat(index + globalOffset, result.input)) :
7272
newValueOrFn
7373
output.push(out)
7474

@@ -87,16 +87,21 @@ function replaceUsingRegexp (string, regexp, newValueOrFn) {
8787

8888
module.exports = function stringReplaceToArray (stringOrArray, regexpOrSubstr, newSubStrOrFn) {
8989
if (typeof stringOrArray === 'string') {
90-
return replace(stringOrArray, regexpOrSubstr, newSubStrOrFn)
90+
return replace(stringOrArray, regexpOrSubstr, newSubStrOrFn, 0)
9191
} else if (!Array.isArray(stringOrArray) || !stringOrArray[0]) {
9292
throw new TypeError('First argument must be an array or non-empty string')
9393
} else {
9494
var len = stringOrArray.length
9595
var output = []
96+
var globalOffset = 0
9697
for (var i = 0; i < len; ++i) {
9798
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+
}
100105
}
101106
return output
102107
}

test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,18 @@ describe('string-replace-to-array', function () {
8383
replace([{}, 'some string', {}], 'some', 'a')
8484
.should.eql([{}, 'a', ' string', {}])
8585
})
86+
// Source: https://github.com/appfigures/react-easy-emoji/issues/18
87+
it ('should provide a global offset when doing a string replace', function () {
88+
const result = replace(["🧡 abc", "🧡 xyz"], '🧡', (_match, offset, _string) => {
89+
return offset
90+
})
91+
result.should.eql([0, ' abc', 6, ' xyz'])
92+
})
93+
it ('should provide a global offset when doing a regex replace', function () {
94+
const result = replace(["🧡 abc", "🧡 xyz"], /🧡/g, (_match, offset, _string) => {
95+
return offset
96+
})
97+
result.should.eql([0, ' abc', 6, ' xyz'])
98+
})
8699
})
87100
})

0 commit comments

Comments
 (0)