replace-last
Advanced tools
Comparing version 1.0.23 to 1.0.24
49
index.js
@@ -0,30 +1,41 @@ | ||
/* Another missing method from the AWOL library, @license ISC */ | ||
'use strict'; | ||
var escapeRegExp = require('lodash.escaperegexp'); | ||
var isRegExp = require('lodash.isregexp'); | ||
const replaceLast = function(str, pattern, replacement) { | ||
var isRegex = function(any) { | ||
return Object.prototype.toString.call(any) === '[object RegExp]'; | ||
}; | ||
var strReplaceLast = function(str, pattern, replacement) { | ||
var i = str.lastIndexOf(pattern); | ||
if (i < 0) return str; | ||
var lhs = str.substring(0, i); | ||
var rhs = str.substring(i + pattern.length, str.length); | ||
return lhs + replacement + rhs; | ||
}; | ||
var regexReplaceLast = function(str, pattern, replacement) { | ||
pattern = new RegExp(pattern.source, 'g'); | ||
return str.replace(pattern, function(match, offset, str) { | ||
var follow = str.slice(offset); | ||
var isLast = follow.match(pattern).length === 1; | ||
return (isLast) ? replacement : match; | ||
}); | ||
}; | ||
var replaceLast = function(str, pattern, replacement) { | ||
var orig = str; | ||
if (typeof(str) === 'number') str = str.toString(); | ||
if (typeof(pattern) === 'number') pattern = pattern.toString(); | ||
if (typeof(replacement) === 'number') replacement = replacement.toString(); | ||
if (typeof(str) !== 'string') return str; | ||
if (typeof(replacement) !== 'string') return str; | ||
if (typeof(str) !== 'string') return orig; | ||
if (typeof(replacement) !== 'string') return orig; | ||
var result; | ||
if (typeof(pattern) === 'string') { | ||
pattern = new RegExp(escapeRegExp(pattern), 'g'); | ||
} else if (isRegExp(pattern)) { | ||
pattern = new RegExp(pattern.source, 'g'); | ||
result = strReplaceLast(str, pattern, replacement); | ||
} else if (isRegex(pattern)) { | ||
result = regexReplaceLast(str, pattern, replacement); | ||
} else { | ||
return str; | ||
return orig; | ||
} | ||
return str.replace(pattern, function(match, offset, str) { | ||
var follow = str.slice(offset); | ||
var isLast = follow.match(pattern).length === 1; | ||
return (isLast) ? replacement : match; | ||
}); | ||
return (result === str) ? orig : result; | ||
}; | ||
module.exports = replaceLast; |
@@ -29,2 +29,7 @@ 'use strict'; | ||
it('none', function() { | ||
var result = replaceLast('hello hello', 'bonjour', 'bye'); | ||
expect(result).to.equal('hello hello'); | ||
}); | ||
it('contains regex chars', function() { | ||
@@ -47,2 +52,7 @@ var result = replaceLast('hello . hello . hello', '.', 'bye'); | ||
}); | ||
it('none', function() { | ||
var result = replaceLast(99, 7, 8); | ||
expect(result).to.equal(99); | ||
}); | ||
}); | ||
@@ -62,2 +72,7 @@ | ||
it('none', function() { | ||
var result = replaceLast('hello hello', /bonjour/, 'bye'); | ||
expect(result).to.equal('hello hello'); | ||
}); | ||
it('contains regex chars', function() { | ||
@@ -64,0 +79,0 @@ var result = replaceLast('hello . hello . hello', /./, 'bye'); |
{ | ||
"name": "replace-last", | ||
"version": "1.0.23", | ||
"version": "1.0.24", | ||
"description": "", | ||
"main": "index.js", | ||
"browser": "replaceLast.js", | ||
"author": "Daniel Lewis BSc (Hons)", | ||
"license": "ISC", | ||
"scripts": {}, | ||
"dependencies": { | ||
"lodash.escaperegexp": "^4.1.2", | ||
"lodash.isregexp": "^4.0.1" | ||
} | ||
"scripts": {} | ||
} |
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
9109
0
7
195
- Removedlodash.escaperegexp@^4.1.2
- Removedlodash.isregexp@^4.0.1
- Removedlodash.escaperegexp@4.1.2(transitive)
- Removedlodash.isregexp@4.0.1(transitive)