Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

replace-last

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

replace-last - npm Package Compare versions

Comparing version 1.0.23 to 1.0.24

index.next.js

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": {}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc