Skip to content

Commit f1a4e65

Browse files
committed
Merge pull request locutusjs#157 from glenara/patch-6
Update str_ireplace.js
2 parents 251c1b1 + 50c45bc commit f1a4e65

File tree

1 file changed

+84
-53
lines changed

1 file changed

+84
-53
lines changed

functions/strings/str_ireplace.js

Lines changed: 84 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,96 @@
1-
function str_ireplace(search, replace, subject) {
1+
function str_ireplace(search, replace, subject, count) {
22
// discuss at: http://phpjs.org/functions/str_ireplace/
3-
// original by: Martijn Wieringa
4-
// input by: penutbutterjelly
5-
// input by: Brett Zamir (http://brett-zamir.me)
6-
// improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
7-
// improved by: Jack
8-
// bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
9-
// bugfixed by: Onno Marsman
10-
// bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
11-
// bugfixed by: Philipp Lenssen
12-
// example 1: str_ireplace('l', 'l', 'HeLLo');
13-
// returns 1: 'Hello'
14-
// example 2: str_ireplace('$', 'foo', '$bar');
15-
// returns 2: 'foobar'
3+
// original by: Glen Arason (http://CanadianDomainRegistry.ca)
4+
// note: Case-insensitive version of str_replace()
5+
// note: Compliant with PHP 5.0 str_ireplace() Full details at:
6+
// note: http://ca3.php.net/manual/en/function.str-ireplace.php
7+
// format: str_ireplace($search, $replace, $subject[, 'count'])
8+
// Parameters: If search and replace are arrays, then str_ireplace() takes a
9+
// Parameters: value from each array and uses them to search and replace on
10+
// Parameters: subject.
11+
// Parameters: If replace has fewer values than search, then an empty string
12+
// Parameters: is used for the rest of replacement values.
13+
// Parameters: If search is an array and replace is a string, then this
14+
// Parameters: replacement string is used for every value of search.
15+
// note: The count parameter (optional) if used must be passed in as a
16+
// note: string. eg global var MyCount:
17+
// note: str_ireplace($search, $replace, $subject, 'MyCount');
18+
// input: str_ireplace($search, $replace, $subject[, {string}]);
19+
// Returns: a string or an array of replacements.
1620

17-
var i, k = '';
18-
var searchl = 0;
19-
var reg;
21+
var i = 0,
22+
j = 0,
23+
temp = '',
24+
repl = '',
25+
sl = 0,
26+
fl = 0,
27+
f = '',
28+
r = '',
29+
s = '',
30+
ra = '',
31+
sa = '',
32+
otemp = '',
33+
oi = '',
34+
ofjl = '',
35+
os = subject,
36+
osa = Object.prototype.toString.call(os) === '[object Array]';
2037

21-
var escapeRegex = function (s) {
22-
return s.replace(/([\\\^\$*+\[\]?{}.=!:(|)])/g, '\\$1');
23-
};
38+
if(typeof(search) === 'object') {
39+
temp = search;
40+
search = new Array();
41+
for(i=0; i<temp.length;i+=1) {
42+
search[i] = temp[i].toLowerCase();
43+
}
44+
}else { search = search.toLowerCase(); }
2445

25-
search += '';
26-
searchl = search.length;
27-
if (Object.prototype.toString.call(replace) !== '[object Array]') {
28-
replace = [replace];
29-
if (Object.prototype.toString.call(search) === '[object Array]') {
30-
// If search is an array and replace is a string,
31-
// then this replacement string is used for every value of search
32-
while (searchl > replace.length) {
33-
replace[replace.length] = replace[0];
34-
}
46+
if(typeof(subject) === 'object') {
47+
temp = subject;
48+
subject = new Array();
49+
for(i=0; i<temp.length;i+=1) {
50+
subject[i] = temp[i].toLowerCase();
3551
}
36-
}
52+
}else { subject = subject.toLowerCase(); }
3753

38-
if (Object.prototype.toString.call(search) !== '[object Array]') {
39-
search = [search];
54+
if(typeof(search) === 'object' && typeof(replace) === 'string' ) {
55+
temp = replace;
56+
replace = new Array();
57+
for (i=0; i < search.length; i+=1) {
58+
replace[i] = temp;
59+
}
4060
}
41-
while (search.length > replace.length) {
42-
// If replace has fewer values than search,
43-
// then an empty string is used for the rest of replacement values
44-
replace[replace.length] = '';
61+
62+
temp = '';
63+
f = [].concat(search);
64+
r = [].concat(replace);
65+
ra = Object.prototype.toString.call(r) === '[object Array]';
66+
s = subject;
67+
sa = Object.prototype.toString.call(s) === '[object Array]';
68+
s = [].concat(s);
69+
os = [].concat(os);
70+
71+
if (count) {
72+
this.window[count] = 0;
4573
}
4674

47-
if (Object.prototype.toString.call(subject) === '[object Array]') {
48-
// If subject is an array, then the search and replace is performed
49-
// with every entry of subject , and the return value is an array as well.
50-
for (k in subject) {
51-
if (subject.hasOwnProperty(k)) {
52-
subject[k] = str_ireplace(search, replace, subject[k]);
53-
}
75+
for (i = 0, sl = s.length; i < sl; i++) {
76+
if (s[i] === '') {
77+
continue;
5478
}
55-
return subject;
56-
}
79+
for (j = 0, fl = f.length; j < fl; j++) {
80+
temp = s[i] + '';
81+
repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
82+
s[i] = (temp).split(f[j]).join(repl);
83+
otemp = os[i] + '';
84+
oi = temp.indexOf(f[j]);
85+
ofjl = f[j].length;
86+
if(oi >= 0) {
87+
os[i] = (otemp).split(otemp.substr(oi,ofjl)).join(repl);
88+
}
5789

58-
searchl = search.length;
59-
for (i = 0; i < searchl; i++) {
60-
reg = new RegExp(escapeRegex(search[i]), 'gi');
61-
subject = subject.replace(reg, replace[i]);
90+
if (count) {
91+
this.window[count] += ((temp.split(f[j])).length - 1);
92+
}
93+
}
6294
}
63-
64-
return subject;
65-
}
95+
return osa ? os : os[0];
96+
}

0 commit comments

Comments
 (0)