From 93ff6e1bc70b9b0ee4b9aabeb09aacc048eb4291 Mon Sep 17 00:00:00 2001 From: TaishiM Date: Wed, 22 Jan 2020 12:09:09 +0900 Subject: [PATCH] Fix key duplicate bug --- index.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 0eda61c..f3e414d 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,14 @@ /* eslint-disable vars-on-top, no-var, prefer-template */ -var isRegExp = require('lodash/isRegExp'); -var escapeRegExp = require('lodash/escapeRegExp'); -var isString = require('lodash/isString'); -var flatten = require('lodash/flatten'); +var isRegExp = require('lodash.isregexp'); +var escapeRegExp = require('lodash.escaperegexp'); +var isString = require('lodash.isstring'); +var flatten = require('lodash.flatten'); /** * Given a string, replace every substring that is matched by the `match` regex * with the result of calling `fn` on matched substring. The result will be an * array with all odd indexed elements containing the replacements. The primary - * use case is similar to using String.prototype.replace except for React. + * use case is similar to using String.prototype.replace execpt for React. * * React will happily render an array as children of a react element, which * makes this approach very useful for tasks like surrounding certain text @@ -27,7 +27,7 @@ var flatten = require('lodash/flatten'); * @param {function} fn * @return {array} */ -function replaceString(str, match, fn) { +function replaceString(str, match, fn, mapIndex) { var curCharStart = 0; var curCharLen = 0; @@ -49,7 +49,8 @@ function replaceString(str, match, fn) { for (var i = 1, length = result.length; i < length; i += 2) { curCharLen = result[i].length; curCharStart += result[i - 1].length; - result[i] = fn(result[i], i, curCharStart); + const key = `${mapIndex}-${i}` + result[i] = fn(result[i], i, curCharStart, key); curCharStart += curCharLen; } @@ -59,7 +60,7 @@ function replaceString(str, match, fn) { module.exports = function reactStringReplace(source, match, fn) { if (!Array.isArray(source)) source = [source]; - return flatten(source.map(function(x) { - return isString(x) ? replaceString(x, match, fn) : x; + return flatten(source.map(function(x, index) { + return isString(x) ? replaceString(x, match, fn, index) : x; })); };