replace-last
Advanced tools
Comparing version 1.2.0 to 1.2.1
11
index.js
@@ -6,2 +6,13 @@ 'use strict'; | ||
function main(str, pattern, replacement) { | ||
// Official MDN polyfill https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/flags | ||
// for Node.js v4 | ||
/* istanbul ignore next */ | ||
if (RegExp.prototype.flags === undefined) { | ||
Object.defineProperty(RegExp.prototype, 'flags', { | ||
configurable: true, | ||
get: function() { | ||
return this.toString().match(/[gimuy]*$/)[0]; | ||
} | ||
}); | ||
} | ||
return replaceLast(str, pattern, replacement); | ||
@@ -8,0 +19,0 @@ } |
'use strict'; | ||
var isRegex = function(any) { | ||
// Convert regex pattern to global regex pattern | ||
function getGlobalPattern(pattern) { | ||
var flags = (pattern.flags.indexOf('g') === -1) ? pattern.flags + 'g' : pattern.flags; | ||
return new RegExp(pattern.source, flags); | ||
} | ||
// Is this a regex? | ||
function isRegex(any) { | ||
return Object.prototype.toString.call(any) === '[object RegExp]'; | ||
}; | ||
} | ||
var regexReplaceLast = function(str, pattern, replacement) { | ||
// Official MDN polyfill https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/flags | ||
// for Node.js v4 | ||
/* istanbul ignore next */ | ||
if (RegExp.prototype.flags === undefined) { | ||
Object.defineProperty(RegExp.prototype, 'flags', { | ||
configurable: true, | ||
get: function() { | ||
return this.toString().match(/[gimuy]*$/)[0]; | ||
} | ||
}); | ||
// Replace pattern by replacement at index | ||
function replaceAtIndex(str, pattern, replacement, i) { | ||
var lhs = str.substring(0, i); | ||
var rhs = str.substring(i + pattern.length, str.length); | ||
return lhs + replacement + rhs; | ||
} | ||
// replaceLast where pattern is a regex | ||
function regexReplaceLast(str, pattern, replacement) { | ||
pattern = getGlobalPattern(pattern); | ||
// var lastIndex = 0; | ||
var match; | ||
var temp = pattern.exec(str); | ||
while (temp != null) { | ||
match = temp; | ||
// lastIndex = pattern.lastIndex; | ||
temp = pattern.exec(str); | ||
} | ||
var flags = (pattern.flags.indexOf('g') === -1) ? pattern.flags + 'g' : pattern.flags; | ||
pattern = new RegExp(pattern.source, flags); | ||
return str.replace(pattern, function(/* args expanded below */) { | ||
var match = arguments[0]; | ||
var p = Array.prototype.slice.call(arguments, 1, arguments.length - 2); | ||
var offset = arguments[arguments.length - 2]; | ||
var str = arguments[arguments.length - 1]; | ||
var follow = str.slice(offset); | ||
var isLast = follow.match(pattern).length === 1; | ||
if (!isLast) return match; | ||
if (!p.length) return replacement; | ||
for (var i = 0; i < p.length; i++) { | ||
match = strReplaceLast(match, p[i], replacement); | ||
if (match == null) return str; | ||
if (match.length === 1) { | ||
return replaceAtIndex(str, match[0], replacement, match.index); | ||
} else { | ||
var accReplacement = match[0]; | ||
for (var i = 1; i < match.length; i++) { | ||
accReplacement = strReplaceLast(accReplacement, match[i], replacement); | ||
} | ||
return match; | ||
}); | ||
}; | ||
return replaceAtIndex(str, match[0], accReplacement, match.index); | ||
} | ||
} | ||
var strReplaceLast = function(str, pattern, replacement) { | ||
// replaceLast where pattern is a string | ||
function strReplaceLast(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; | ||
}; | ||
return replaceAtIndex(str, pattern, replacement, i); | ||
} | ||
var replaceLast = function(str, pattern, replacement) { | ||
// replaceLast | ||
function replaceLast(str, pattern, replacement) { | ||
str = '' + str; | ||
@@ -54,4 +61,4 @@ if (typeof replacement === 'undefined') return str; | ||
} | ||
}; | ||
} | ||
module.exports = replaceLast; |
{ | ||
"name": "replace-last", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "JavaScript replaceLast function - Replaces last match for pattern in string with replacement", | ||
@@ -15,3 +15,4 @@ "author": "danday74", | ||
"postcommit": "cross-env FORCE_COLOR=true npm run browserify", | ||
"prepush": "npm run build" | ||
"prepush": "npm run build", | ||
"benchmarks": "node benchmarks" | ||
}, | ||
@@ -23,3 +24,3 @@ "dependencies": {}, | ||
"cross-env": "^5.1.4", | ||
"eslint": "^4.19.0", | ||
"eslint": "^4.19.1", | ||
"git-commit-file": "^1.3.1", | ||
@@ -29,5 +30,6 @@ "husky": "^0.14.3", | ||
"lodash": "^4.17.5", | ||
"mocha": "^5.0.4", | ||
"webpack": "^4.1.1", | ||
"webpack-cli": "^2.0.12" | ||
"mocha": "^5.0.5", | ||
"performance-now": "^2.1.0", | ||
"webpack": "^4.4.1", | ||
"webpack-cli": "^2.0.13" | ||
}, | ||
@@ -34,0 +36,0 @@ "keywords": [ |
@@ -1,1 +0,1 @@ | ||
var replaceLast=function(t){var e={};function r(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:n})},r.r=function(t){Object.defineProperty(t,"__esModule",{value:!0})},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=1)}([function(t,e,r){"use strict";var n=function(t,e,r){var n=t.lastIndexOf(e);return n<0?t:t.substring(0,n)+r+t.substring(n+e.length,t.length)};t.exports=function(t,e,r){return t=""+t,void 0===r?t:(o=e,"[object RegExp]"===Object.prototype.toString.call(o)?function(t,e,r){void 0===RegExp.prototype.flags&&Object.defineProperty(RegExp.prototype,"flags",{configurable:!0,get:function(){return this.toString().match(/[gimuy]*$/)[0]}});var o=-1===e.flags.indexOf("g")?e.flags+"g":e.flags;return e=new RegExp(e.source,o),t.replace(e,function(){var t=arguments[0],o=Array.prototype.slice.call(arguments,1,arguments.length-2),u=arguments[arguments.length-2];if(1!==arguments[arguments.length-1].slice(u).match(e).length)return t;if(!o.length)return r;for(var c=0;c<o.length;c++)t=n(t,o[c],r);return t})}(t,e,r):n(t,e=""+e,r));var o}},function(t,e,r){"use strict";const n=r(0);t.exports=function(t,e,r){return n(t,e,r)}}]); | ||
var replaceLast=function(t){var e={};function n(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:r})},n.r=function(t){Object.defineProperty(t,"__esModule",{value:!0})},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=1)}([function(t,e,n){"use strict";function r(t,e,n,r){return t.substring(0,r)+n+t.substring(r+e.length,t.length)}function o(t,e,n){for(var o,i=(e=function(t){var e=-1===t.flags.indexOf("g")?t.flags+"g":t.flags;return new RegExp(t.source,e)}(e)).exec(t);null!=i;)o=i,i=e.exec(t);if(null==o)return t;if(1===o.length)return r(t,o[0],n,o.index);for(var c=o[0],f=1;f<o.length;f++)c=u(c,o[f],n);return r(t,o[0],c,o.index)}function u(t,e,n){var o=t.lastIndexOf(e);return o<0?t:r(t,e,n,o)}t.exports=function(t,e,n){return t=""+t,void 0===n?t:(r=e,"[object RegExp]"===Object.prototype.toString.call(r)?o(t,e,n):u(t,e=""+e,n));var r}},function(t,e,n){"use strict";const r=n(0);t.exports=function(t,e,n){return void 0===RegExp.prototype.flags&&Object.defineProperty(RegExp.prototype,"flags",{configurable:!0,get:function(){return this.toString().match(/[gimuy]*$/)[0]}}),r(t,e,n)}}]); |
14
TODO.md
@@ -0,1 +1,15 @@ | ||
Benchmark code against alternative solutions to same problem | ||
Consider: | ||
negative lookahead - https://stackoverflow.com/questions/49372598/using-negative-lookahead-regex-abc-abc-versus-abc-abc | ||
str.match with g flag and take last | ||
regex.exec with g flag and take last | ||
Update README | ||
@@ -2,0 +16,0 @@ |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
25072
21
429
0
12