smourph
12/27/2016 - 1:29 PM

IDGenerator

IDGenerator

function IDGenerator(length) {
    this.length = typeof length !== 'undefined' ? length : 10;
    this.timestamp = +new Date;

    var _getRandomInt = function (min, max) {
        return Math.floor(Math.random() * ( max - min + 1 )) + min;
    };

    this.generate = function () {
        var ts = this.timestamp.toString();
        var parts = ts.split("").reverse();
        var id = "";

        for (var i = 0; i < this.length; ++i) {
            var index = _getRandomInt(0, parts.length - 1);
            id += parts[index];
        }

        return id;
    };
}

var generator = new IDGenerator(),
    id = generator.generate();