Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // curse Javascript's tiny standard library
- function id(x) { return x; }
- function uncurry(f) {
- return (args => f(...args));
- }
- function compose(...fs) {
- return fs.reduce((acc, f) => (x => f(acc(x))), id);
- }
- // oh god, assignment and a loop? >:(
- // js doesn't have tail call optimization, unfortunately
- function zip(l1, l2) {
- let result = [];
- let minRange = Math.min(l1.length, l2.length);
- for(let i = 0; i < minRange; i++) {
- result.push([l1[i], l2[i]]);
- }
- return result;
- }
- function sum(lst, acc) {
- return lst.reduce((x, y) => x + y, acc);
- }
- function fst(tup) {
- return tup[0];
- }
- function snd(tup) {
- return tup[1];
- }
- const lowerStr = "abcde"; // shortened for brevity
- const upperStr = lowerStr.toUpperCase();
- const specialStr = "!@#$%^&*";
- const numberStr = "0123456789";
- function getCharList(lowercase, uppercase, special, numbers) {
- return compose(uncurry(zip),
- (arr => arr.filter(fst)),
- (arr => arr.map(snd)),
- (arr => sum(arr, [])),
- (s => s.split('')))([[lowercase, uppercase, special, numbers],
- [lowerStr, upperStr, specialStr, numberStr]])
- }
- function randomChoice(lst) {
- return lst[Math.floor(Math.random() * lst.length)];
- }
- function randomPassword(n, lowercase, uppercase, special, numbers) {
- let charList = getCharList(lowercase, uppercase, special, numbers);
- return Array(n).fill(undefined)
- .map(_ => randomChoice(charList))
- .join('');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement