• Question about string.trim()

    From KenDB3@VERT to All on Thu Sep 8 10:11:46 2016
    Subject: Question about string.trim()
    @VIA: KD3NET
    @MSGID: <57D171A2.2211.sync_js@bbs.kd3.us>
    @TZ: c12c
    Hi there folks,

    So, I've been running the Solar Data app for a little while and have not run into this, but now that I put it out there for a wider audience, I seem to have encountered an issue.

    Someone reported this error message to me:

    9/7 11:14:09p Node 1 !javascript c:\sbbs\xtrn\solar\alco-solar.js line 275: typeError: sfi.trim is not a function

    So, my first question is, is the trim() function valid in JavaScript 1.8.5? I'm guessing it is because it's been working in this app thus far, but maybe this is the first time something actually needed to be trimmed?

    What I find interesting is that the error flagged on a trim() on line 275, but was OK with the trim() on line 270: timestamp.trim().

    The code can be found here: https://github.com/KenDB3/alco-solar-JS/blob/master/alco-solar.js

    If trim() is valid, then am I...
    1) Doing something wrong?
    2) Using it incorrectly?
    3) Not doing a good job of dealing with it if it is undefined? (lines 81-84)
    4) Gone plumb crazy (crazier than normal, I guess)?

    My third question stood out to me because it is the only one that turns into an Integer (0) if it is undefined, where the other ones turn into Strings ("No Data").

    ~KenDB3

    ---
    ■ Synchronet ■ KD3net-Rhode Island's only BBS about nothing. http://bbs.kd3.us
  • From echicken@VERT to KenDB3 on Thu Sep 8 11:39:34 2016
    Subject: Question about string.trim()
    @VIA: ECBBS
    @MSGID: <57D18636.2216.sync_js@bbs.electronicchicken.com>
    @REPLY: <57D171A2.2211.sync_js@bbs.kd3.us>
    @TZ: c12c
    9/7 11:14:09p Node 1 !javascript c:\sbbs\xtrn\solar\alco-solar.js line 275: typeError: sfi.trim is not a function

    So, my first question is, is the trim() function valid in JavaScript 1.8.5? I'm guessing it is because it's been working in

    Yes, it's a valid method on a String object.

    What I find interesting is that the error flagged on a trim() on line 275, but was OK with the trim() on line 270: timestamp.trim().

    In this case 'timestamp' is really just another String, since you're just grabbing the text from the feed and not turning it into a Number or Date object, so that makes sense.

    My best guess is that the 'solarflux' value was absent when this person fetched the feed, or that they failed to fetch the feed altogether. On line 80, if 'solardata.solarflux' is undefined, 'sfi' gets set to 0, a Number, which does not have the '.trim()' method. Wrap that 0 in quotes on line 80 and this particular problem should go away.

    Also, if you wanted to, you could also shorten things a bit by doing something like:

    var sfi = solardata.solarflux || '0';
    var sfi_int = parseInt(sfi);
    var ai = solardata.aindex || 'No Data';
    // etc.

    That would be like a shorter version of:

    var sfi = (typeof solardata.solarflux == 'undefined' ? '0' : solardata.solarflux);

    Which is a shorter version of:

    var sfi = solardata.solarflux;
    if (sfi == undefined) {
    var sfi = '0';
    }

    ---
    echicken
    electronic chicken bbs - bbs.electronicchicken.com - 416-273-7230
    ■ Synchronet ■ electronic chicken bbs - bbs.electronicchicken.com
  • From KenDB3@VERT to echicken on Thu Sep 8 12:32:12 2016
    Subject: Question about string.trim()
    @VIA: KD3NET
    @MSGID: <57D1928C.2214.sync_js@bbs.kd3.us>
    @REPLY: <57D18636.2216.sync_js@bbs.electronicchicken.com>
    @TZ: c12c
    Re: Question about string.trim()
    By: echicken to KenDB3 on Thu Sep 08 2016 11:39 am

    So, my first question is, is the trim() function valid in JavaScript
    1.8.5? I'm guessing it is because it's been working in

    Yes, it's a valid method on a String object.

    Awesome, thank you.

    What I find interesting is that the error flagged on a trim() on line
    275, but was OK with the trim() on line 270: timestamp.trim().

    In this case 'timestamp' is really just another String, since you're just grabbing the text from the feed and not turning it into a Number or Date object, so that makes sense.

    My best guess is that the 'solarflux' value was absent when this person fetched the feed, or that they failed to fetch the feed altogether. On line 80, if 'solardata.solarflux' is undefined, 'sfi' gets set to 0, a Number, which does not have the '.trim()' method. Wrap that 0 in quotes on line 80 and this particular problem should go away.

    Also, if you wanted to, you could also shorten things a bit by doing something like:

    var sfi = solardata.solarflux || '0';
    var sfi_int = parseInt(sfi);
    var ai = solardata.aindex || 'No Data';
    // etc.

    That would be like a shorter version of:

    var sfi = (typeof solardata.solarflux == 'undefined' ? '0' : solardata.solarflux);

    Which is a shorter version of:

    var sfi = solardata.solarflux;
    if (sfi == undefined) {
    var sfi = '0';
    }

    You make it look so easy! Thanks man, that's incredibly helpful, I really appreciate it. I'm learning lots playing with this stuff. I had a feeling, but I wasn't sure.

    Updates imminent :-)

    ~KenDB3

    ---
    ■ Synchronet ■ KD3net-Rhode Island's only BBS about nothing. http://bbs.kd3.us