Last active
November 30, 2018 09:00
-
-
Save samdenty/fdf4bac682c632650a0d90576ddde36a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Generator { | |
constructor( | |
readonly zero: string, | |
readonly one: string, | |
readonly separator: string | |
) {} | |
encode(data: string) { | |
return data | |
.split('') | |
.map(char => '00'.concat(char.charCodeAt(0).toString(2)).slice(-8)) | |
.join(this.separator) | |
.split('0') | |
.join(this.zero) | |
.split('1') | |
.join(this.one) | |
} | |
decode(data: string) { | |
return String.fromCharCode( | |
...(data | |
.split(this.zero) | |
.join('0') | |
.split(this.one) | |
.join('1') | |
.split(this.separator) | |
.map(b => '0b' + b) as any) | |
) | |
} | |
payload(payload: string | Function) { | |
const data = typeof payload === 'function' ? `(${payload})()` : payload | |
return `setTimeout(String.fromCharCode(...'${this.encode(data)}'.replace(/${ | |
this.zero | |
}|${this.one}/g,B=>+('${this.one}'==B)).split\`${ | |
this.separator | |
}\`.map(b=>'0b'+b)))` | |
} | |
} | |
;(window as any).generator = new Generator('\u200B', '\u200C', '\u200D') | |
generator.payload(() => console.log('works')) // setTimeout(String.fromCharCode(...''.replace(/|/g,B=>+(''==B)).split``.map(b=>'0b'+b))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment