Posted
over 13 years
ago
by
Ghabriel Nunes
I found some errors in the function above.
The above function does not handle negative limits, and, according to the PHP documentation, if limit is 0, it's treated as 1. The above function treats limit as "not set" if it's 0, which is wrong. Also, if
... [More]
the limit is bigger than the split length, it adds white spaces to the array, which is also wrong. I re-created this function, fixing all the mistakes I found:
Note: sorry for any english mistakes, english is not my first language.
function explode( delimiter, string, limit ){
if ( arguments.length < 2 || typeof delimiter == 'undefined' || typeof string == 'undefined' ) return null;
if ( delimiter === '' || delimiter === false || delimiter === null) return false;
if ( typeof delimiter == 'function' || typeof delimiter == 'object' || typeof string == 'function' || typeof string == 'object'){
return { 0: '' };
}
if ( delimiter === true ) delimiter = '1';
// Here we go...
delimiter += '';
string += '';
var s = string.split( delimiter );
if ( typeof limit === 'undefined' ) return s;
// Support for limit
if ( limit === 0 ) limit = 1;
// Positive limit
if ( limit > 0 ){
if ( limit >= s.length ) return s;
return s.slice( 0, limit - 1 ).concat( [ s.slice( limit - 1 ).join( delimiter ) ] );
}
// Negative limit
if ( -limit >= s.length ) return [];
s.splice( s.length + limit );
return s;
}
[Less]
|
Posted
over 13 years
ago
by
jj
this peace of shit is not working.
|
Posted
over 13 years
ago
by
insild
|
Posted
over 13 years
ago
by
4545
432
|
Posted
over 13 years
ago
by
lord_t
Math.fmod = function(x, y) {
// Returns the remainder of dividing x by y as a float
//
// version: 1109.2015
// discuss at: http://phpjs.org/functions/fmod // original by: Onno Marsman
// input by: Brett Zamir
... [More]
(http://brett-zamir.me)
// bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// bugfixed by: Pawel 'lord_t' Maruszczyk (http://hrabstwo.net)
// * example 1: fmod(5.7, 1.3);
// * returns 1: 0.5
var tmp, tmp2, p = 0,
pY = 0,
l = 0.0,
l2 = 0.0;
tmp = x.toExponential().match(/^.\.?(.*)e(. )$/);
p = parseInt(tmp[2], 10) - (tmp[1] '').length;
tmp = y.toExponential().match(/^.\.?(.*)e(. )$/);
pY = parseInt(tmp[2], 10) - (tmp[1] '').length;
if (pY > p) {
p = pY;
}
tmp2 = (x % y);
if (p < -100 || p > 20) {
// toFixed will give an out of bound error so we fix it like this:
l = Math.round(Math.log(tmp2) / Math.log(10));
l2 = Math.pow(10, l);
return (tmp2 / l2).toFixed(l - p) * l2;
} else {
return tmp2;
}
}
[Less]
|
Posted
over 13 years
ago
by
lord_t
Math.fmod = function(x, y) {
// Returns the remainder of dividing x by y as a float
//
// version: 1109.2015
// discuss at: http://phpjs.org/functions/fmod // original by: Onno Marsman
// input by: Brett Zamir
... [More]
(http://brett-zamir.me)
// bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// bugfixed by: Pawel 'lord_t' Maruszczyk
// * example 1: fmod(5.7, 1.3);
// * returns 1: 0.5
var tmp, tmp2, p = 0,
pY = 0,
l = 0.0,
l2 = 0.0;
tmp = x.toExponential().match(/^.\.?(.*)e(. )$/);
p = parseInt(tmp[2], 10) - (tmp[1] '').length;
tmp = y.toExponential().match(/^.\.?(.*)e(. )$/);
pY = parseInt(tmp[2], 10) - (tmp[1] '').length;
if (pY > p) {
p = pY;
}
tmp2 = (x % y);
if (p < -100 || p > 20) {
// toFixed will give an out of bound error so we fix it like this:
l = Math.round(Math.log(tmp2) / Math.log(10));
l2 = Math.pow(10, l);
return (tmp2 / l2).toFixed(l - p) * l2;
} else {
return tmp2;
}
}
[Less]
|
Posted
over 13 years
ago
by
Lord_t
It doesn't work!
|
Posted
over 13 years
ago
by
kirilloid
String.fromCharCode accepts several arguments.
Replacing lines 34-36 with
enc = String.fromCharCode((c1 >> 6) | 192, (c1 & 63) | 128);
} else {
enc = String.fromCharCode((c1 >> 12) | 224, ((c1 >> 6) & 63) | 128, (c1
... [More]
& 63) | 128);
may reduce execution time from 20x to 12x on mostly non-ascii strings (e.g. cyrillic text). [Less]
|
Posted
over 13 years
ago
by
Brett Zamir
test
|
Posted
over 13 years
ago
by
Unodeepinna
|