phpjs
Version:
187 lines (148 loc) • 4.62 kB
HTML
<!-- Generated by Rakefile:build -->
<strong>
zeroneta
</strong>
on 2010-01-05 22:11:57 <br />
http://bgscript.com/jscore/script/core.js
<hr />
<strong>
zeroneta
</strong>
on 2010-01-05 17:28:30 <br />
<pre><code>
_.trim = function( a, s )
{
s = s == un ? '\s\n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000' : real( s );
return ( a + '' ).replace( new _._.RegExp( '(^[' + s + ']*)|([' + s + ']*$)', 'g' ), '' );
}
</code></pre>
<pre><code>
$.real = function( a )
{
return ( a + '' ).replace( /(\$|\^|\*|\(|\)|\-|\+|\||\\|\{|\[|\}|\]|\,|\.|\?|\/)/g, '\\$1' );
}
</code></pre>
<hr />
<strong>
<a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
</strong>
on 2008-10-06 12:01:05 <br />
@ Onno Marsmann: Fixed, thank you!
<hr />
<strong>
Onno Marsman
</strong>
on 2008-10-04 17:08:42 <br />
Something like trim(16,1) does not work correctly.
Fix:
Add the following lines of code to the beginning of the function:
<pre><code>
str += '';
charlist += '';
</code></pre>
<hr />
<strong>
<a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
</strong>
on 2008-08-27 17:16:01 <br />
@ Jack: That's a good idea, thank you, I've implemented it and credited you accordingly.
@ Ben W: I will have to study on that, thanks for the input
<hr />
<strong>
<a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
</strong>
on 2008-08-27 17:08:04 <br />
@ 0x0: That's definitely something to pounder about. We did not in the first place, because one of the project goals is to keep functions as standalone as reasonably possible. This duplication seemed reasonable for a standalone trim.
<hr />
<strong>
Ben W
</strong>
on 2008-08-27 15:08:26 <br />
Wouldn't it be better to use regex? Like one of these:
http://blog.stevenlevithan.com/archives/faster-trim-javascript
<hr />
<strong>
Jack
</strong>
on 2008-08-21 16:21:58 <br />
we can avoid unnecessary string length computations by changing the &quot;for&quot; loop from
<pre><code>
for (var i = 0; i &lt; str.length; i++)
</code></pre>
to
<pre><code>
for (var i = 0, l=str.length; i &lt; l; i++)
</code></pre>
<pre><code>
function trim( str, charlist ) {
var whitespace;
if (!charlist) {
whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
} else {
whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
}
for (var i = 0, l=str.length; i &lt; l; i++) {
if (whitespace.indexOf(str.charAt(i)) === -1) {
str = str.substring(i);
break;
}
}
for (i = str.length - 1; i &gt;= 0; i--) {
if (whitespace.indexOf(str.charAt(i)) === -1) {
str = str.substring(0, i + 1);
break;
}
}
return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}
</code></pre>
<hr />
<strong>
0x0
</strong>
on 2008-08-07 02:06:14 <br />
If you also have rtrim and ltrim, you could just do this:
<pre><code>
function trim(str, charlist) {
return ltrim(rtrim(str, charlist), charlist);
}
</code></pre>
<hr />
<strong>
DxGx
</strong>
on 2008-03-20 12:47:21 <br />
<pre><code>
function trim (str) {
var whitespace = ' \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
for (var i = 0; i &lt; str.length; i++) {
if (whitespace.indexOf(str.charAt(i)) === -1) {
str = str.substring(i);
break;
}
}
for (i = str.length - 1; i &gt;= 0; i--) {
if (whitespace.indexOf(str.charAt(i)) === -1) {
str = str.substring(0, i + 1);
break;
}
}
return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}
</code></pre>
Replace trim function by this code. Acoding to this site: it is faster: http://blog.stevenlevithan.com/archives/faster-trim-javascript
<hr />
<strong>
<a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a>
</strong>
on 2008-02-21 12:17:27 <br />
@ Erkekjetter: Hi Erkekjetter, thanks for your input. I've found the time to recode the functions and updated php.js accordingly.
<hr />
<strong>
Erkekjetter
</strong>
on 2008-02-19 19:36:38 <br />
The trim() function for PHP is described as
string trim ( string $str [, string $charlist ] )
In your trim(), rtrim() and ltrim() functions the second parameter charlist is missing. You might change that to have a equivalent function.
<hr />