Skip to content

Instantly share code, notes, and snippets.

@mathildathompson
Created May 7, 2014 00:30
Show Gist options
  • Save mathildathompson/93738990dff6c7b7b22a to your computer and use it in GitHub Desktop.
Save mathildathompson/93738990dff6c7b7b22a to your computer and use it in GitHub Desktop.
Array.prototype.each_slice = function (size, callback){
console.log(this.length, 'Size', size, 'Callback', callback);
for (var i = 0, l = this.length; i < l; i += size){
callback.call(this, this.slice(i, i + size));
}
};
var Crypto = function(messageString){
this.message = messageString;
}
Crypto.prototype.normalizePlainText = function(){
plainText = _.map(this.message, function(character){
return character.toLowerCase();
})
var results = _.filter(plainText, function(character){
return character.search(/[a-z]+/) == 0;
})
return results;
}
//Adding methods;
Crypto.prototype.plainTextSegments = function(){
var length = Math.ceil(Math.sqrt(this.normalizePlainText().length));
var segments = []
this.normalizePlainText().each_slice(length, function(segment){
segments.push(segment);
})
return segments;
}
Crypto.prototype.cipherText = function(){
cipherArray = []
_.each(this.plainTextSegments(), function(segment){
_.each(segment, function(letter, index){
cipherArray[index] = cipherArray[index] || [];
cipherArray.push(letter);
})
})
return cipherArray.join('');
}
//Instantiated a new Crypto object
var crypto1 = new Crypto('This is a secret message');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment