Last active
December 27, 2015 17:19
-
-
Save ericlifka/7361572 to your computer and use it in GitHub Desktop.
This file contains 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
String.prototype.encodeAsPeriods = function () { | |
var characterArray = this.split(''); | |
var charCodeArray = characterArray.map(function (char) { | |
return char.charCodeAt(); | |
}); | |
var periodEncodedCharArray = charCodeArray.map(function (charCode) { | |
var charCodeString = "" + charCode; | |
var digitArray = charCodeString.split(''); | |
var periodStringArray = digitArray.map(function (digitString) { | |
var digit = parseInt(digitString, 10); | |
var periodString = ""; | |
for (var i = 0; i <= digit; i++) { | |
periodString += "."; | |
} | |
return periodString; | |
}); | |
var periodEncodedChar = periodStringArray.join(' '); | |
return periodEncodedChar; | |
}); | |
var periodEncodedString = periodEncodedCharArray.join(' '); | |
return periodEncodedString; | |
}; | |
Function.prototype.encodeAsPeriods = function () { | |
var sourceCode = this.toString(); | |
return sourceCode.encodeAsPeriods(); | |
}; | |
String.prototype.decodeFromPeriods = function () { | |
return this.split(' ').map(function(w) { | |
return String.fromCharCode(parseInt(w.split(' ').map(function(s) { | |
return s.length-1; | |
}).join(''))); | |
}).join(''); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment