Advertisement
Guest User

Untitled

a guest
Nov 9th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // curse Javascript's tiny standard library
  2.  
  3. function id(x) { return x; }
  4.  
  5. function uncurry(f) {
  6.     return (args => f(...args));
  7. }
  8.  
  9. function compose(...fs) {
  10.     return fs.reduce((acc, f) => (x => f(acc(x))), id);
  11. }
  12.  
  13. // oh god, assignment and a loop? >:(
  14. // js doesn't have tail call optimization, unfortunately
  15. function zip(l1, l2) {
  16.     let result = [];
  17.     let minRange = Math.min(l1.length, l2.length);
  18.     for(let i = 0; i < minRange; i++) {
  19.         result.push([l1[i], l2[i]]);
  20.     }
  21.     return result;
  22. }
  23.  
  24. function sum(lst, acc) {
  25.     return lst.reduce((x, y) => x + y, acc);
  26. }
  27.  
  28. function fst(tup) {
  29.     return tup[0];
  30. }
  31.  
  32. function snd(tup) {
  33.     return tup[1];
  34. }
  35.  
  36. const lowerStr = "abcde"; // shortened for brevity
  37. const upperStr = lowerStr.toUpperCase();
  38. const specialStr = "!@#$%^&*";
  39. const numberStr = "0123456789";
  40.  
  41. function getCharList(lowercase, uppercase, special, numbers) {
  42.     return compose(uncurry(zip),
  43.         (arr => arr.filter(fst)),
  44.         (arr => arr.map(snd)),
  45.         (arr => sum(arr, [])),
  46.         (s => s.split('')))([[lowercase, uppercase, special, numbers],
  47.                              [lowerStr, upperStr, specialStr, numberStr]])
  48. }
  49.  
  50. function randomChoice(lst) {
  51.     return lst[Math.floor(Math.random() * lst.length)];
  52. }
  53.  
  54. function randomPassword(n, lowercase, uppercase, special, numbers) {
  55.     let charList = getCharList(lowercase, uppercase, special, numbers);
  56.     return Array(n).fill(undefined)
  57.                    .map(_ => randomChoice(charList))
  58.                    .join('');
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement