replace-string
Advanced tools
Comparing version 3.0.0 to 3.1.0
declare namespace replaceString { | ||
type ReplacementFunction = ( | ||
needle: string, | ||
matchedSubstring: string, | ||
matchCount: number, | ||
@@ -16,2 +16,9 @@ input: string, | ||
readonly fromIndex?: number; | ||
/** | ||
Whether or not substring matching should be case-insensitive. | ||
@default false | ||
*/ | ||
readonly caseInsensitive?: boolean; | ||
} | ||
@@ -37,3 +44,3 @@ } | ||
replaceString('Foo 🐑 Bar', '🐑', (needle, matchCount, input, matchIndex) => `${needle}❤️`); | ||
replaceString('Foo 🐑 Bar', '🐑', (matchedSubstring, matchCount, input, matchIndex) => `${matchedSubstring}❤️`); | ||
//=> 'Foo 🐑❤️ Bar' | ||
@@ -40,0 +47,0 @@ ``` |
14
index.js
@@ -22,3 +22,5 @@ 'use strict'; | ||
while (true) { // eslint-disable-line no-constant-condition | ||
const index = string.indexOf(needle, prevIndex); | ||
const index = options.caseInsensitive ? | ||
string.toLowerCase().indexOf(needle.toLowerCase(), prevIndex) : | ||
string.indexOf(needle, prevIndex); | ||
@@ -31,5 +33,11 @@ if (index === -1) { | ||
const replaceStr = typeof replacement === 'string' ? replacement : replacement(needle, matchCount, string, index); | ||
const replaceStr = typeof replacement === 'string' ? replacement : replacement( | ||
// If `caseInsensitive`` is enabled, the matched substring may be different from the needle. | ||
string.slice(index, index + needle.length), | ||
matchCount, | ||
string, | ||
index | ||
); | ||
// Get the initial part of the string on the first iteration | ||
// Get the initial part of the string on the first iteration. | ||
const beginSlice = matchCount === 1 ? 0 : prevIndex; | ||
@@ -36,0 +44,0 @@ |
{ | ||
"name": "replace-string", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Replace all substring matches in a string", | ||
"license": "MIT", | ||
"repository": "sindresorhus/replace-string", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
@@ -12,0 +13,0 @@ "engines": { |
@@ -1,2 +0,2 @@ | ||
# replace-string [![Build Status](https://travis-ci.org/sindresorhus/replace-string.svg?branch=master)](https://travis-ci.org/sindresorhus/replace-string) | ||
# replace-string [![Build Status](https://travis-ci.com/sindresorhus/replace-string.svg?branch=master)](https://travis-ci.com/sindresorhus/replace-string) | ||
@@ -7,3 +7,2 @@ > Replace all substring matches in a string | ||
## Install | ||
@@ -15,3 +14,2 @@ | ||
## Usage | ||
@@ -28,6 +26,5 @@ | ||
## API | ||
### replaceString(string, needle, replacement, [options]) | ||
### replaceString(string, needle, replacement, options?) | ||
@@ -54,6 +51,6 @@ Returns a new string with all `needle` matches replaced with `replacement`. | ||
If a function, it receives the following arguments; the `needle`, the match count, and the `input`: | ||
If a function, it receives the matched substring, the match count, the original input, and the index in which the match happened (as measured from the original input): | ||
```js | ||
replaceString('Foo 🐑 Bar', '🐑', (needle, matchCount, input, matchIndex) => `${needle}❤️`); | ||
replaceString('Foo 🐑 Bar', '🐑', (matchedSubstring, matchCount, input, matchIndex) => `${matchedSubstring}❤️`); | ||
//=> 'Foo 🐑❤️ Bar' | ||
@@ -68,3 +65,3 @@ ``` | ||
Type: `number`<br> | ||
Type: `number`\ | ||
Default: `0` | ||
@@ -74,10 +71,11 @@ | ||
##### caseInsensitive | ||
Type: `boolean`\ | ||
Default: `false` | ||
Whether or not substring matching should be case-insensitive. | ||
## Related | ||
- [execall](https://github.com/sindresorhus/execall) - Find multiple `RegExp` matches in a string | ||
## License | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6231
83
76