Skip to content

Instantly share code, notes, and snippets.

@draeton
Created August 4, 2012 20:35
Show Gist options
  • Save draeton/3259787 to your computer and use it in GitHub Desktop.
Save draeton/3259787 to your computer and use it in GitHub Desktop.
Playing around after an article
// http://coding.smashingmagazine.com/2011/10/19/optimizing-long-lists-of-yesno-values-with-javascript/
var Encoder = (function () {
var Encoder = function (trueValue) {
this.trueValue = trueValue;
};
Encoder.prototype = {
encode: function (list) {
var trueValue = this.trueValue;
return list.map(function (value) {
return value === trueValue ? 1 : 0;
}).join("").match(/.{1,16}/g).map(function (chunk) {
return String.fromCharCode(parseInt(chunk, 2));
}).join("");
},
decode: function (encoded) {
return encoded.split("").map(function (char) {
return char.charCodeAt(0).toString(2);
}).join("").split("");
}
};
return Encoder;
}());
var test = ["Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No", "Yes", "No", "Yes", "Yes", "No"];
var encoder = new Encoder("Yes");
var encoded = encoder.encode(test);
var decoded = encoder.decode(encoded);
console.log(encoded, decoded);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment