Created
April 16, 2019 18:39
-
-
Save annibal/c5cbb3e7c0292cf5d00ddb6fe36d5f58 to your computer and use it in GitHub Desktop.
Js Customizable Random Table generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
function zeroFill(str, len, char) { | |
if ((str+"").length < len) { | |
return Array(len-(str+"").length).fill(char)+(str+"") | |
} | |
return (str+"") | |
} | |
function withVerifierDigits(numbers) { | |
numbers = numbers+"" | |
function v(n) { | |
n = (n+"") | |
.split("") | |
.map(function(number){ return parseInt(number, 10); }) | |
; | |
var modulus = n.length + 1; | |
var multiplied = n.map(function(number, index) { | |
return number * (modulus - index); | |
}); | |
var mod = multiplied.reduce(function(buffer, number){ | |
return buffer + number; | |
}) % 11; | |
return (mod < 2 ? 0 : 11 - mod); | |
} | |
return numbers+v(numbers)+v(numbers+v(numbers)) | |
} | |
var columnGen = { | |
float: function(min=0, max=100) { | |
return function() { | |
return (Math.random() * (max - min) + min) | |
} | |
}, | |
int: function(min=0, max=100) { | |
return function() { | |
return (Math.floor(Math.random() * (max - min + 1) + min)) | |
} | |
}, | |
text: function(names) { | |
return function() { | |
if (names == null) { | |
return (Math.round(Math.random()*10)*1000000000).toString(32).toUpperCase() | |
} else { | |
return (names[Math.floor(Math.random() * names.length)]) | |
} | |
} | |
}, | |
date: function() { | |
return function() { | |
return new Date( columnGen.int(-628466400000, 2527293600000)() ) | |
} | |
}, | |
dateDMY: function() { | |
return function() { | |
var x = columnGen.date()() | |
return zeroFill(x.getDate(), 2, "0") + "/" + | |
zeroFill(x.getMonth()+1, 2, "0") + "/" + | |
zeroFill(x.getFullYear(), 4, "0") | |
} | |
}, | |
dateYMD:function() { | |
return function() { | |
var x = columnGen.date()() | |
return zeroFill(x.getFullYear(), 4, "0") + "-" + | |
zeroFill(x.getMonth()+1, 2, "0") + "-" + | |
zeroFill(x.getDate(), 2, "0") | |
} | |
}, | |
dateISON:function() { | |
return function() { | |
var x = columnGen.date()().toISOString() | |
} | |
}, | |
cpf: function(withDots=false, withValidator=false) { | |
return function() { | |
var cpf = columnGen.int(100000000, 999999999)()+"" | |
if (withValidator) { | |
cpf = withVerifierDigits(cpf) | |
if (withDots) { | |
cpf = cpf.replace(/(.{3})(.{3})(.{3})(.{2})/, "$1.$2.$3-$4") | |
} | |
} else { | |
if (withDots) { | |
cpf = cpf.replace(/(.{3})(.{3})(.{3})/, "$1.$2.$3") | |
} | |
} | |
return cpf | |
} | |
}, | |
} | |
return { | |
generators: columnGen, | |
generate: function(_cfg) { | |
var cfg = { | |
size: _cfg.size == null ? 15 : (isNaN(_cfg.size) ? 15 : parseInt(_cfg.size)), | |
columns: (_cfg.columns == null ? [] : _cfg.columns).map(function(col, i) { | |
return { | |
name: col.name == null ? function(i, size) { return "Column "+(i+1)+" of "+size } : col.name, | |
gen: col.gen == null ? function() {return null} : col.gen | |
} | |
}) | |
} | |
var size = typeof cfg.size === "function" ? cfg.size() : cfg.size | |
var columnLength = cfg.columns.length | |
return Array( size+1 ).fill().map(function( _, i) { | |
if (i === 0) { | |
return cfg.columns.map(function(col, j) { | |
return (typeof col.name === "function" ? col.name(j, columnLength) : col.name) | |
}) | |
} else { | |
return cfg.columns.map(function(col, j) { | |
return col.gen(i, j, size, columnLength) | |
}) | |
} | |
}) | |
} | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment