PARSE

Copyright(c) 2013 Stefano Balietti MIT Licensed

(function(JSUS) {

    function PARSE(){};

PARSE.stringify_prefix

Prefix used by PARSE.stringify and PARSE.parse to decode strings with special meaning

See
PARSE.stringify
See
PARSE.parse
    PARSE.stringify_prefix = '!?_';

    PARSE.marker_func = PARSE.stringify_prefix + 'function';
    PARSE.marker_null = PARSE.stringify_prefix + 'null';
    PARSE.marker_und = PARSE.stringify_prefix + 'undefined';
    PARSE.marker_nan = PARSE.stringify_prefix + 'NaN';
    PARSE.marker_inf = PARSE.stringify_prefix + 'Infinity';
    PARSE.marker_minus_inf = PARSE.stringify_prefix + '-Infinity';

PARSE.getQueryString

Parses the current querystring and returns it full or a specific variable. Return false if the requested variable is not found.

Params
variable string Optional. If set, returns only the value associated with this variable
Returns
string boolean The querystring, or a part of it, or FALSE
    PARSE.getQueryString = function(variable) {
        var query = window.location.search.substring(1);
        if ('undefined' === typeof variable) return query;

        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] === variable) {
                return unescape(pair[1]);
            }
        }
        return false;
    };

PARSE.tokenize

Splits a string in tokens that users can specified as input parameter. Additional options can be specified with the modifiers parameter

  • limit: An integer that specifies the number of split items after the split limit will not be included in the array
Params
str string The string to split
separators array Array containing the separators words
modifiers object Optional. Configuration options for the tokenizing
Returns
array Tokens in which the string was split
    PARSE.tokenize = function(str, separators, modifiers) {
        if (!str) return;
        if (!separators || !separators.length) return [str];
        modifiers = modifiers || {};

        var pattern = '[';

        JSUS.each(separators, function(s) {
            if (s === ' ') s = '\\s';

            pattern += s;
        });

        pattern += ']+';

        var regex = new RegExp(pattern);
        return str.split(regex, modifiers.limit);
    };

PARSE.stringify

Stringifies objects, functions, primitive, undefined or null values

Makes uses JSON.stringify with a special reviver function, that strinfifies also functions, undefined, and null values.

A special prefix is prepended to avoid name collisions.

Params
o mixed The value to stringify
spaces number Optional the number of indentation spaces. Defaults, 0
Returns
string The stringified result
See
JSON.stringify
See
PARSE.stringify_prefix
    PARSE.stringify = function(o, spaces) {
        return JSON.stringify(o, function(key, value){
            var type = typeof value;
            if ('function' === type) {
                return PARSE.stringify_prefix + value.toString()
            }

            if ('undefined' === type) return PARSE.marker_und;
            if (value === null) return PARSE.marker_null;
            if ('number' === type && isNaN(value)) return PARSE.marker_nan;
            if (value == Number.POSITIVE_INFINITY) return PARSE.marker_inf;
            if (value == Number.NEGATIVE_INFINITY) return PARSE.marker_minus_inf;

            return value;

        }, spaces);
    };

PARSE.stringifyAll

Copies all the properties of the prototype before stringifying

Notice: The original object is modified!

Params
o mixed The value to stringify
spaces number Optional the number of indentation spaces. Defaults, 0
Returns
string The stringified result
See
PARSE.stringify
    PARSE.stringifyAll = function(o, spaces) {
        for (var i in o) {
            if (!o.hasOwnProperty(i)) {
                if ('object' === typeof o[i]) {
                    o[i] = PARSE.stringifyAll(o[i]);
                }
                else {
                    o[i] = o[i];
                }
            }
        }
        return PARSE.stringify(o);
    };

PARSE.parse

Decodes strings in objects and other values

Uses JSON.parse and then looks for special strings encoded by PARSE.stringify

Params
str string The string to decode
Returns
mixed The decoded value
See
JSON.parse
See
PARSE.stringify_prefix
    PARSE.parse = function(str) {

        var len_prefix = PARSE.stringify_prefix.length,
        len_func = PARSE.marker_func.length,
        len_null = PARSE.marker_null.length,
        len_und = PARSE.marker_und.length,
        len_nan = PARSE.marker_nan.length,
        len_inf = PARSE.marker_inf.length,
        len_inf = PARSE.marker_minus_inf.length;


        var o = JSON.parse(str);
        return walker(o);

        function walker(o) {
            if ('object' !== typeof o) return reviver(o);

            for (var i in o) {
                if (o.hasOwnProperty(i)) {
                    if ('object' === typeof o[i]) {
                        walker(o[i]);
                    }
                    else {
                        o[i] = reviver(o[i]);
                    }
                }
            }

            return o;
        }

        function reviver(value) {
            var type = typeof value;

            if (type === 'string') {
                if (value.substring(0, len_prefix) !== PARSE.stringify_prefix) {
                    return value;
                }
                else if (value.substring(0, len_func) === PARSE.marker_func) {
                    return eval('('+value.substring(len_prefix)+')');
                }
                else if (value.substring(0, len_null) === PARSE.marker_null) {
                    return null;
                }
                else if (value.substring(0, len_und) === PARSE.marker_und) {
                    return undefined;
                }

                else if (value.substring(0, len_nan) === PARSE.marker_nan) {
                    return NaN;
                }
                else if (value.substring(0, len_inf) === PARSE.marker_inf) {
                    return Infinity;
                }
                else if (value.substring(0, len_inf) === PARSE.marker_minus_inf) {
                    return -Infinity;
                }

            }
            return value;
        };
    }

    JSUS.extend(PARSE);

})('undefined' !== typeof JSUS ? JSUS : module.parent.exports.JSUS);