Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. const rows = ['qwertyuiopå', 'asdfghjklöä', 'zxcvbnm'];
  2. const charLists = [
  3. ...rows,
  4. ...rows.map(row => row.toUpperCase()),
  5. '1234567890'
  6. ];
  7.  
  8. const transform = transformer => text =>
  9. text.split('').map(char => {
  10. const chars = charLists.find(list => list.includes(char));
  11. return chars
  12. ? transformer(chars.indexOf(char), chars)
  13. : char;
  14. }).join('');
  15.  
  16. const shiftRight = transform((index, chars) => {
  17. const i = index + 1;
  18. return i < chars.length
  19. ? chars[i]
  20. : chars[0];
  21. });
  22.  
  23. const shiftLeft = transform((index, chars) => {
  24. const i = index - 1;
  25. return i < 0
  26. ? chars[chars.length - 1]
  27. : chars[i];
  28. });
  29.  
  30. console.log(shiftRight('Moi! testiä 123')); // => "Zpo! yrdyoa 234"
  31. console.log(shiftLeft('Zpo! yrdyoa 234')); // => "Moi! testiä 123"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement