prototypes
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -22,9 +22,24 @@ 'use strict'; | ||
{ | ||
var options = 'g'; | ||
options += this.ignoreCase ? 'i' : ''; | ||
options += this.multiline ? 'm' : ''; | ||
options += this.sticky ? 'y' : ''; | ||
return this.addOptions('g'); | ||
}; | ||
/** | ||
* Add the given options to a regular expression. | ||
*/ | ||
newRegExp.addOptions = function(options) | ||
{ | ||
options = addOptionIf(this.global, 'g', options); | ||
options = addOptionIf(this.ignoreCase, 'i', options); | ||
options = addOptionIf(this.multiline, 'm', options); | ||
options = addOptionIf(this.sticky, 'y', options); | ||
return new RegExp(this.source, options); | ||
}; | ||
function addOptionIf(condition, option, options) | ||
{ | ||
if (options.contains(option)) return options; | ||
if (!condition) return options; | ||
return options + option; | ||
} | ||
// add new functions to regexp prototype | ||
@@ -134,4 +149,8 @@ core.addProperties(RegExp.prototype, newRegExp); | ||
newString.replaceIgnoreCase = function(find, replace, initialPos) | ||
newString.replaceIgnoreCase = function(find, replace) | ||
{ | ||
if (util.isRegExp(find)) | ||
{ | ||
return this.replace(find.addOptions('i'), replace); | ||
} | ||
if (typeof find != 'string') | ||
@@ -141,11 +160,4 @@ { | ||
} | ||
var lowerThis = this.toLowerCase(); | ||
var lowerFind = find.toLowerCase(); | ||
initialPos = initialPos || 0; | ||
var pos = lowerThis.indexOf(lowerFind, initialPos); | ||
if (pos == -1) | ||
{ | ||
return this; | ||
} | ||
return this.slice(0, pos) + replace + this.slice(pos + find.length); | ||
var newCase = new RegExp(find, 'i'); | ||
return this.replace(newCase, replace); | ||
}; | ||
@@ -155,2 +167,6 @@ | ||
{ | ||
if (util.isRegExp(find)) | ||
{ | ||
return this.replace(find.addOptions('gi'), replace); | ||
} | ||
if (typeof find != 'string') | ||
@@ -160,14 +176,4 @@ { | ||
} | ||
var lowerThis = this.toLowerCase(); | ||
var lowerFind = find.toLowerCase(); | ||
var pos = lowerThis.indexOf(lowerFind); | ||
var result = this; | ||
var offset = 0; | ||
while (pos != -1) | ||
{ | ||
result = result.slice(0, pos + offset) + replace + result.slice(pos + find.length + offset); | ||
offset += replace.length - find.length; | ||
pos = lowerThis.indexOf(lowerFind, pos + 1); | ||
} | ||
return result; | ||
var newCase = new RegExp(find, 'gi'); | ||
return this.replace(newCase, replace); | ||
}; | ||
@@ -174,0 +180,0 @@ |
{ | ||
"name": "prototypes", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Some common prototypes for node.js: string.startsWith(), object.countProperties() and more. Facilities for functional programming with objects: object.forEach(), object.filter(). Functions are added safely using Object.defineProperty().", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/alexfernandez/prototypes", |
29003
684