Skip to content

Commit ea4f34c

Browse files
committed
Added character start to fn call arguments per issue #8
1 parent ce2fd67 commit ea4f34c

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ var flatten = require('lodash.flatten');
2828
* @return {array}
2929
*/
3030
function replaceString(str, match, fn) {
31+
var curCharStart = 0;
32+
var curCharLen = 0;
33+
3134
if (str === '') {
3235
return str;
3336
} else if (!str || !isString(str)) {
@@ -44,7 +47,10 @@ function replaceString(str, match, fn) {
4447

4548
// Apply fn to all odd elements
4649
for (var i = 1, length = result.length; i < length; i += 2) {
47-
result[i] = fn(result[i], i);
50+
curCharLen = result[i].length;
51+
curCharStart += result[i - 1].length;
52+
result[i] = fn(result[i], i, curCharStart);
53+
curCharStart += curCharLen;
4854
}
4955

5056
return result;

0 commit comments

Comments
 (0)