Sha256: 9d39d41a8f9f0fd30142741c77a7e7a221532e44a590d6f25b7a6c8368333ef6

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

String.prototype.format = function (args) {
    var result = this;
    if (arguments.length > 0) {
        if (arguments.length == 1 && typeof (args) == "object") {
            for (var key in args) {
                if (args[key] != undefined) {
                    var reg = new RegExp("({" + key + "})", "g");
                    result = result.replace(reg, args[key]);
                }
            }
        }
        else {
            for (var i = 0; i < arguments.length; i++) {
                if (arguments[i] != undefined) {
                    var reg = new RegExp("({[" + i + "]})", "g");
                    result = result.replace(reg, arguments[i]);
                }
            }
        }
    }
    return result;
};

/**
 * 生成随机字符串
 * @param len 字符串长度
 * @param includeUpcase 是否包含大写字符,默认为false
 * @returns {string}
 */
String.random = function (len, includeUpcase) {
    var str = '0123456789abcdefghijklmnopqrstuvwxyz';
    var randomStr = '';
    for (var i = 0; i < len; i++) {
        var c = String(str[parseInt(Math.random() * str.length)]);
        randomStr += (includeUpcase && parseInt(Math.random() * 10) % 2 == 0) ? c.toUpperCase() : c;
    }
    return randomStr;
};

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jextend-0.1.4 vendor/assets/javascripts/jextend/string.js