I found this ingenious caesar cipher solution on codefights. I'm trying to understand what the buffer is doing here.
caesarian = (m, n) =>
"" + b([...b(m)].map(x => (n % 26 + x + 7) % 26 + 97))
Iterating a Node.js
Buffer yields its data one byte at a time. For a Buffer (with the default encoding of utf8) that contains alphanumeric characters, that's the same thing getting each char's ASCII code:
[...Buffer("hello")] // [ 104, 101, 108, 108, 111 ]
(n % 26 + x + 7) % 26 + 97) is the Caeser cipher arithmetic - I guess you're familiar with this part.
So, this part of the code interprets the String as an array ASCII codes and transforms them to their corresponding outputs:
[...b(m)].map(x => (n % 26 + x + 7) % 26 + 97))
You can initialise a
Buffer with an array of byte values:
Buffer() // <Buffer 61>
"" + b() is the same as
Buffer#toString interprets the stored bytes as unicode characters:
"" + Buffer() // 'a'
Therefore the point of the outer
b is to turn the manipulated ASCII codes back into alphanumeric characters for display.