proto

stringMethods

declaration
 stringMethods 

var stringMethods = module.exports = {
   firstUpperCase: firstUpperCase,
   firstLowerCase: firstLowerCase,
   toRegExp: toRegExp,
   toFunction: toFunction,
   toDate: toDate,
   toQueryString: toQueryString,
   fromQueryString: fromQueryString,
   jsonParse: jsonParse,
   hashCode: hashCode,
   unPrefix: unPrefix,
   format: format
};

firstUpperCase

function
 firstUpperCase() 

Option name Type Description
self String

A string that will have its first character replaced

Returns string with the first character changed to upper case.

function firstUpperCase() {
    return this ? this[0].toUpperCase() + this.slice(1) : this;
}

firstLowerCase

function
 firstLowerCase() 

Option name Type Description
self String

A string that will have its first character replaced

Returns string with the first character changed to lower case.

function firstLowerCase() {
    return this ? this[0].toLowerCase() + this.slice(1) : this;
}

toRegExp

function
 toRegExp() 

Option name Type Description
self String

string containing regular expression including enclosing "/" symbols and flags

return RegExp

Converts string created by toString method of RegExp back to RegExp

function toRegExp() {
    var rx = this.match(regexpStringPattern);
    if (rx) return new RegExp(rx[1], rx[2]);
}
var regexpStringPattern = /^\/(.*)\/([gimy]*)$/;

toFunction

function
 toFunction() 

Option name Type Description
self String

string containing full function code

return Function

Converts string created by toString method of function back to function

function toFunction() {
    var func;
    var code = 'func = ' + this + ';';
    try {
        eval(code);
        return func;
    } catch(e) {
        return;
    }
}

toDate

function
 toDate() 

Option name Type Description
self String, Date

string or date object to convert to VALID date

return Date

Converts string to date in a safe way so that the resiult is undefined if date is invalid

function toDate() {
    if (! this) return;
    try {
        var date = new Date(this);
    } catch (e) {}
    if (date && date.getTime && !isNaN(date.getTime()))
        return date;
}

toQueryString

function
 toQueryString() 

Option name Type Description
self Object

The object hash to be converted

encode Function

optional function used to encode data, encodeURIComponent is used if not specified

return String

the resulting query string

Convert params object to a url style query string (without "?")

function toQueryString(encode) {
    var qs = ''
        , params = this || {}
        , encode = encode || encodeURIComponent;

    __.eachKey.call(params, function(value, key) {
        qs += key + '=' + encode(value) + '&';
    });
    
    return qs.slice(0, -1);
}

fromQueryString

function
 fromQueryString() 

Option name Type Description
self String

The string to be converted

decode Function

optional decode function, decodeURIComponent will be used if not supplied

return Object

The resulting object hash

Convert url style query string (without "?") into object hash

function fromQueryString(decode) {
    var pairs = this.split('&')
        , results = {}
        , decode = decode || decodeURIComponent;

    pairs.forEach(function(pair) {
        var splitPair = pair.split('=');
        if (splitPair.length < 2) return;
        var key = splitPair[0]
            , value = decode(splitPair[1] || '');
        if (!key) return;
        results[key] = value;
    });

    return results;
}

jsonParse

function
 jsonParse() 

Option name Type Description
self String

JSON string representation of object

return Object

Safe JSON.parse, returns undefined if JSON.parse throws an exception

function jsonParse() {
    try {
        return JSON.parse(this);
    } catch (e) {}
}

hashCode

function
 hashCode() 

Option name Type Description
self String

string to convert to hash

return Number

Dan Bernstein's algorythm to create hash from string

function hashCode() {
    var hash = 5381
        , str = this
        , len = str.length;
    for (var i = 0; i < len; i++) {
        var char = str.charCodeAt(i);
        hash = ((hash << 5) + hash) + char;

hash * 33 + c

}
return hash;
}

unPrefix

function
 unPrefix() 

Option name Type Description
self String
return String

Removes given prefix from the string. If string does not begin from the prefix, returns undefined

function unPrefix(str) {
    if (this.indexOf(str) == 0)
        return this.replace(str, '');
}

formatRegexp

declaration
 formatRegexp 

Regex used to identify format vars

var formatRegexp = /\$[0-9]+|\$\$/g;

format

function
 format() 

Option name Type Description
this String

The string to be formatted

args Array

The values to be formatted

return String

The formatted string

String formatting utility to swap out tokens for variables.

function format() { // , ... arguments
    var str = this;
    var args = arguments;
    return str.replace(formatRegexp, function (item) {
        if (item == '$$') return '$';
        item = item.slice(1);
        return args[item - 1];
    });
};