replace-string
Advanced tools
Comparing version 2.0.0 to 3.0.0
24
index.js
'use strict'; | ||
module.exports = (input, needle, replacement, options = {}) => { | ||
if (typeof input !== 'string') { | ||
throw new TypeError(`Expected input to be a string, got ${typeof input}`); | ||
module.exports = (string, needle, replacement, options = {}) => { | ||
if (typeof string !== 'string') { | ||
throw new TypeError(`Expected input to be a string, got ${typeof string}`); | ||
} | ||
@@ -10,15 +10,15 @@ | ||
!(typeof replacement === 'string' || typeof replacement === 'function')) { | ||
return input; | ||
return string; | ||
} | ||
let ret = ''; | ||
let result = ''; | ||
let matchCount = 0; | ||
let prevIndex = options.fromIndex > 0 ? options.fromIndex : 0; | ||
if (prevIndex > input.length) { | ||
return input; | ||
if (prevIndex > string.length) { | ||
return string; | ||
} | ||
while (true) { // eslint-disable-line no-constant-condition | ||
const index = input.indexOf(needle, prevIndex); | ||
const index = string.indexOf(needle, prevIndex); | ||
@@ -31,3 +31,3 @@ if (index === -1) { | ||
const replaceStr = typeof replacement === 'string' ? replacement : replacement(needle, matchCount, input, index); | ||
const replaceStr = typeof replacement === 'string' ? replacement : replacement(needle, matchCount, string, index); | ||
@@ -37,3 +37,3 @@ // Get the initial part of the string on the first iteration | ||
ret += input.slice(beginSlice, index) + replaceStr; | ||
result += string.slice(beginSlice, index) + replaceStr; | ||
@@ -44,6 +44,6 @@ prevIndex = index + needle.length; | ||
if (matchCount === 0) { | ||
return input; | ||
return string; | ||
} | ||
return ret + input.slice(prevIndex); | ||
return result + string.slice(prevIndex); | ||
}; |
{ | ||
"name": "replace-string", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "Replace all substring matches in a string", | ||
@@ -13,9 +13,10 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=6" | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
"test": "xo && ava && tsd" | ||
}, | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
@@ -41,5 +42,6 @@ "keywords": [ | ||
"devDependencies": { | ||
"ava": "^0.25.0", | ||
"xo": "^0.23.0" | ||
"ava": "^1.4.1", | ||
"tsd": "^0.7.2", | ||
"xo": "^0.24.0" | ||
} | ||
} |
@@ -20,5 +20,5 @@ # replace-string [![Build Status](https://travis-ci.org/sindresorhus/replace-string.svg?branch=master)](https://travis-ci.org/sindresorhus/replace-string) | ||
const input = 'My friend has a 🐑. I want a 🐑 too!'; | ||
const string = 'My friend has a 🐑. I want a 🐑 too!'; | ||
replaceString(input, '🐑', '🦄'); | ||
replaceString(string, '🐑', '🦄'); | ||
//=> 'My friend has a 🦄. I want a 🦄 too!' | ||
@@ -30,7 +30,7 @@ ``` | ||
### replaceString(input, needle, replacement, [options]) | ||
### replaceString(string, needle, replacement, [options]) | ||
Returns a new string with all `needle` matches replaced with `replacement`. | ||
#### input | ||
#### string | ||
@@ -49,3 +49,3 @@ Type: `string` | ||
Type: `string` `Function` | ||
Type: `string | Function` | ||
@@ -63,3 +63,3 @@ Replacement for `needle` matches. | ||
Type: `Object` | ||
Type: `object` | ||
@@ -66,0 +66,0 @@ ##### fromIndex |
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
5614
5
70
3