Skip to main content

Returns a base-36, psuedo-random string with a hyphenated namespace (if you include one).

/**
 * Get namespace id.
 *
 * Returns a base-36, psuedo-random string with a hyphenated namespace (if you
 * include one). Both arguments are optional, it will by default return a string
 * six characters long.
 *
 * Taken from http://foundation.zurb.com/sites/docs/javascript-utilities.html
 *
 * @param {Number} length The maximum length of the random value (optional).
 * @param {String} namespace The namespace value (optional).
 * @return {String} A psuedo-random string with a hyphenated namespace (if you
 *     include one).
 */
function getNamespaceId(length, namespace) {
    length = length || 6;
    return Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, length)))
        .toString(36)
        .slice(1) + (namespace ? '-' + namespace : '');
}