Helpers when working with parseParams

1 view
Skip to first unread message

Udo Borkowski

unread,
May 12, 2006, 6:41:59 PM5/12/06
to Tiddly...@googlegroups.com
I found the following two helper functions very useful when working with the named parameter feature (i.e. the stuff around the "parseParams" function).

// Returns the first value of the given named parameter.
//
// @
param params
//         as returned by parseParams or null/undefined
//
function getParamValue(params, name, defaultValue) {
    if (!params)
        return defaultValue;
       
    var p = params[0][name];
    return (!p || p.length == 0) ? defaultValue : p[0];
}

// Returns the first value of the given boolean named parameter.
//
// @param params
//         as returned by parseParams or null/undefined
//
function getBooleanParamValue(params, name, defaultValue) {
    var f = getParamValue(params, name, null);
    if (f === null)
        return defaultValue;
    return (typeof f == "boolean") ? f : f.toString() == "true";
}

Retrieving parameters for macros is then just an easy to read one-liner (at least for the commonly used cases). Here an example:

    var args = paramString.parseParams("list",null,true);

    var buttonAtRight = getBooleanParamValue(args, "buttonAtRight");
    var sizeTextbox = getParamValue(args, "sizeTextbox", this.sizeTextbox);



Udo

Saq Imtiaz

unread,
May 13, 2006, 4:24:34 AM5/13/06
to Tiddly...@googlegroups.com
On 5/13/06, Udo Borkowski <Udo.Bo...@gmx.de> wrote:
I found the following two helper functions very useful when working with the named parameter feature (i.e. the stuff around the "parseParams" function).

hmm..... does seem more userfriendly than the unwieldy way I handle my named params. However, I'm reluctant to have to throw those functions into every plugin I write.... it would be quite handy to have something like that in the core though. ;)

Saq

Udo Borkowski

unread,
May 13, 2006, 4:55:31 AM5/13/06
to Tiddly...@googlegroups.com
it would be quite handy to have something like that in the core though. ;)

Nice that you said that ;-)

Udo
Reply all
Reply to author
Forward
0 new messages