PAGE2GO2 HOME | INTERNET NEWS

LeighExchange - Free Advertising Network Stock Research at Internet Speed

Re: Request for Comments: StringUtils utility for JavaScript

 List
Subject: Re: Request for Comments: StringUtils utility for JavaScript
Poster: ray.honeypot@gmail.com
Date: 23 Mar 2007 09:01:40 -0700
Related Postings: 1 2
Hi Femto,

Thanks for the comments! Response inline...

On Mar 23, 10:33 pm, "femto" wrote:
> Hi, Not very much comment,
> but here
> isBlank: function(str) {
> var i, // counter
> strLen; // the str length
> if((str == null) || (strLen = str.length)) {
> return true;
> }
>
> I guess strLen = str.length is kind of buggy or weird,
> maybe str.length == 0?

I assigned it there so in the iteration below that I can refer to strLen instead of str.length repeatedly.


> in line 618:
> endsWidth: function(str, suffix) {
> return StringUtils.startsWith(str, str.length - suffix.length);
> },
> should be return StringUtils.startsWith(str, suffix, str.length -
> suffix.length); ?

Thanks much for the catch--I momentarily forgot that Java and JavaScript are different--the two versions of startsWith cause infinite recursion! I'll be sure to add proper tests in the next incarnation!


> in line 686:
> for (i = 0; i < str1.length && i < str2.length; ++i) {
> if (str1.charAt(i) !== str2.charAt(i)) {
> break;
> }
> }
> if (i < str2.length || i < str1.length) {
> return i;
> }
> return -1;
>
> don't get it,
> why doesn't
> for (i = 0; i < str1.length && i < str2.length; ++i) {
> if (str1.charAt(i) !== str2.charAt(i)) {
> return i;
> }
> }
> ?

Hmmm, I was thinking that one string may be shorter than another (so if it's gone past one we can't say that i is index of difference...), but come to think of it again, you're right. Thanks again!


> many place use === , don't get it.

That's the difference between identity and equality. When you use == JavaScript does conversion under the hood (e.g.: "1" == true, but "1" ! == true). I just blogged about this very thing a few days ago, actually:

http://rayfd.wordpress.com/2007/03/18/really-understanding-javascripts-equality-and-identity/

Best regards, Ray

 

Page2Go2.com is not responsible for content of this message.