Skip to content

Instantly share code, notes, and snippets.

@DDuarte
Created April 17, 2020 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DDuarte/61ff40738d7969eb04862c19aaa3c0a0 to your computer and use it in GitHub Desktop.
Save DDuarte/61ff40738d7969eb04862c19aaa3c0a0 to your computer and use it in GitHub Desktop.
/**
* @param {string} alphabet
* @param {string} key
* @param {string} text
* @return {string}
*/
f = (
alphabet,
key,
text,
alphabetLength = alphabet.length,
indexOf = c => alphabet.indexOf(c)
) =>
[...text]
.filter(c => ~indexOf(c))
.map((c, i) =>
key
? alphabet[
(indexOf(key[i % key.length]) - indexOf(c) + alphabetLength) % alphabetLength
]
: c
)
.join("");
const assert = require("assert").strict;
assert.deepEqual(
f("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "", "MO3VEYOURTROOP3SATZEROTH3REEHUND3RED"),
"MOVEYOURTROOPSATZEROTHREEHUNDRED"
);
assert.deepEqual(
f("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "SECRET", "MOVEYOURTROOPSATZEROTHREEHUNDRED"),
"GQHNGFYNJAQFDMCYFPBQJKNPOXIEBCOB"
);
assert.deepEqual(f("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "SECRET", "abcd"), "");
assert.deepEqual(
f(
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"SECRET",
"MoOo1£44$24!VEYOURTROOPSATZEROTHREEHUNDRED"
),
"GQHNGFYNJAQFDMCYFPBQJKNPOXIEBCOB"
);
/*
f=(a,b,c)=>[...c].filter(b=>a.includes(b)).reduce((d,e,c)=>{if(!b)return d+e;for(let g=0;;g++)if(b[c%b.length]==a[(g+a.indexOf(e))%a.length])return d+a[g]},""); -- 160
f=(a,b,c)=>[...c].filter(b=>a.includes(b)).reduce((d,e,c)=>d+(b?a[(a.indexOf(b[c%b.length])-a.indexOf(e)+a.length)%a.length]:e),"") - 131
f=(a,b,c,z=a.length,w=c=>a.indexOf(c))=>[...c].filter(b=>a.includes(b)).reduce((d,e,c)=>d+(b?a[(w(b[c%b.length])-w(e)+z)%z]:e),"") - 130
f=(a,b,c,z=a.length,w=c=>a.indexOf(c))=>[...c].filter(b=>a.includes(b)).reduce((d,e,c)=>d+(b?a[(w(b[c%b.length])-w(e)+z)%z]:e),"") - 130
f=(a,b,c,z=a.length,w=c=>a.indexOf(c))=>[...c].filter(b=>a.includes(b)).map((d,c)=>b?a[(w(b[c%b.length])-w(d)+z)%z]:d).join("") - 127
f=(a,b,c,z=a.length,w=c=>a.indexOf(c))=>[...c].filter(a=>~w(a)).map((d,c)=>b?a[(w(b[c%b.length])-w(d)+z)%z]:d).join("") - 119
*/
f=(a,k,t,i=j=1)=>t.replace(/./g,h=>~(c=a.indexOf(h))?a[(a+a).indexOf((j+=k)[i++],c)-c]||h:"")
f=(e,f,d,i=(j=1))=>d.replace(/./g,d=>~(c=e.indexOf(d))?e[(e+e).indexOf((j+=f)[i++],c)-c]||d:"");
f = (a, k, t, i = (j = 1)) =>
t.replace(/./g, h =>
~(c = a.indexOf(h)) ? a[(a + a).indexOf((j += k)[i++], c) - c] || h : ""
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment