Posted
over 13 years
ago
by
acebone
This will not uppercase foreign/extended chars, in order to do that simply replace [a-z] with [^\s] in the regex, so that the function looks like this:
function ucwords (str) {
// Uppercase the first character of every word in a string
//
... [More]
// version: 1109.2015
// discuss at: http://phpjs.org/functions/ucwords // + original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
// + improved by: Waldo Malqui Silva
// + bugfixed by: Onno Marsman
// + improved by: Robin
// + input by: James (http://www.james-bell.co.uk/) // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// * example 1: ucwords('kevin van zonneveld');
// * returns 1: 'Kevin Van Zonneveld'
// * example 2: ucwords('HELLO WORLD');
// * returns 2: 'HELLO WORLD' return (str + '').replace(/^([^\s])|\s+([^\s])/g, function ($1) {
return $1.toUpperCase();
});
[Less]
|
Posted
over 13 years
ago
by
carpinteyronyc
|
Posted
over 13 years
ago
by
Daniel Dotsenko
Per my tests pre-splitting the string, with consequent use of array[index] lookup is 3 times faster on IE - the only browser you would use this on. (All others have btoa())
In addition to that, Closure Compiler for some reason "optimizes" the code
... [More]
above (long string with 4x .charAt look up in that string) into 4 fold repetition of that string within minified code. I know it's Closure's faulty "optimization" logic, but switching to array helps it do the right thing.
Here is something that behaved about 3 times faster (on IE) than the code you have above (on long 20k texts) in our use:
function base64_encode(data) {
// use native implementation if it's present
if (typeof btoa === 'function') return btoa(data)
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 /=",
b64a = b64.split(''),
o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
ac = 0,
enc = "",
tmp_arr = [];
do { // pack three octets into four hexets
o1 = data.charCodeAt(i );
o2 = data.charCodeAt(i );
o3 = data.charCodeAt(i );
bits = o1 << 16 | o2 << 8 | o3;
h1 = bits >> 18 & 0x3f;
h2 = bits >> 12 & 0x3f;
h3 = bits >> 6 & 0x3f;
h4 = bits & 0x3f;
// use hexets to index into b64, and append result to encoded string
tmp_arr[ac ] = b64a[h1] b64a[h2] b64a[h3] b64a[h4];
} while (i < data.length);
enc = tmp_arr.join('');
var r = data.length % 3;
return (r ? enc.slice(0, r - 3) : enc) '==='.slice(r || 3);
}
test is here:
http://jsperf.com/base64-most-referred-mit-and-bsd-vs-native-btoa/2 [Less]
|
Posted
over 13 years
ago
by
dyerynund
|
Posted
over 13 years
ago
by
dyerynund
|
Posted
over 13 years
ago
by
dyerynund
|
Posted
over 13 years
ago
by
dyerynund
|
Posted
over 13 years
ago
by
kirilloid
Hm-hm. Why not use this?
function fmod (x, y) {
return x % y;
}
|
Posted
over 13 years
ago
by
Denny Wardhana
I've just noticed that this function and also other functions which are using Date.parse return incorrect result in:
- Chrome 4 and older (can be ignored)
- IE8 and older (hardly to be ignored)
- Safari latest version and older (shouldn't be ignored)
|
Posted
over 13 years
ago
by
Brett Zamir
@Glen: Yes, you are right; as per the notes, this is only really useful for the likes of Firefox extensions (not sure if Mozilla will or has abandoned sorting order). See the array() function source code for the beginnings of an API to allow a more
... [More]
reliable though somewhat uglier syntax like: array({key1:value1}, {key2:value2}). Feel free to implement this using that syntax! [Less]
|