Skip to content

Instantly share code, notes, and snippets.

@Inclushe
Last active October 3, 2016 21:41
Show Gist options
  • Save Inclushe/9c9f287a8c7404f9431ea5371c3a33f8 to your computer and use it in GitHub Desktop.
Save Inclushe/9c9f287a8c7404f9431ea5371c3a33f8 to your computer and use it in GitHub Desktop.
module.exports = {
characters: ['᠎', '​'],
encode: function (input) {
var output = ''
var binary = ''
for (var char = 0; char < input.length; char++) {
var dec = input.charCodeAt(char).toString(2)
binary += Array(9 - dec.length).join('0') + dec
}
for (var bit = 0; bit < binary.length; bit++) {
output += this.characters[binary.charAt(bit)]
}
return output
},
decode: function (input) {
var output = ''
var binary = ''
for (var char = 0; char < input.length; char++) {
binary += this.characters.indexOf(input[char])
}
for (var byte = 0; byte < binary.length / 8; byte++) {
output += String.fromCharCode(parseInt(binary.substr(byte * 8, 8), 2))
}
return output
}
}
// Only works with Unicode characters up to 255
// $ node
// > var nonsense = require('./nonsense.js')
// undefined
// > nonsense.encode('Hello')
// '᠎​᠎᠎​᠎᠎᠎᠎​​᠎᠎​᠎​᠎​​᠎​​᠎᠎᠎​​᠎​​᠎᠎᠎​​᠎​​​​'
// > nonsense.decode('᠎​᠎᠎​᠎᠎᠎᠎​​᠎᠎​᠎​᠎​​᠎​​᠎᠎᠎​​᠎​​᠎᠎᠎​​᠎​​​​')
// 'Hello'
// > nonsense.characters = ['A', 'B']
// [ 'A', 'B' ]
// > nonsense.encode('Hello')
// 'ABAABAAAABBAABABABBABBAAABBABBAAABBABBBB'
// > nonsense.decode('ABAABAAAABBAABABABBABBAAABBABBAAABBABBBB')
// 'Hello'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment