Last active
August 29, 2015 14:10
-
-
Save dannyduc/e393004c19930079f2d5 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
var partialAny = (function() { | |
var slice = Array.prototype.slice; | |
function partialAny(fn /*, args...*/) { | |
var orig = slice.call(arguments, 1); | |
return function() { | |
var partial = slice.call(arguments, 0); | |
var args = []; | |
for (var i = 0; i < orig.length; i++) { | |
args[i] = orig[i] === partialAny._ ? partial.shift() : orig[i]; | |
} | |
return fn.apply(this, args.concat(partial)); | |
} | |
} | |
partialAny._ = {}; | |
return partialAny; | |
}()); | |
var compose = function(/* args... */) { | |
var fns = arguments; | |
return function(result) { | |
for (var i = fns.length - 1; i > -1; i--) { | |
result = fns[i].call(this, result); | |
} | |
return result; | |
}; | |
}; | |
var __ = partialAny._; | |
var parseBinaryToInt = partialAny(parseInt, __, 2); | |
var toAsciiChar = compose(String.fromCharCode, parseBinaryToInt); | |
function charCode(ch) { | |
if (!ch || ch.length > 1) { | |
throw Error(ch + " is not a single char string") | |
} | |
return ch.charCodeAt(0); | |
} | |
function byteString(n) { | |
if (n < 0 || n > 255 || n % 1 !== 0) { | |
throw Error(n + " does not fit in a byte"); | |
} | |
return ("000000000" + n.toString(2)).substr(-8) | |
} | |
var toBinaryChar = compose(byteString, charCode); | |
function toText(binaryStr) { | |
return binaryStr.match(/.{1,8}/g).map(toAsciiChar).join(''); | |
} | |
function toBinary(string) { | |
return string.split('').map(toBinaryChar).join(''); | |
} | |
console.log( | |
toText(toBinary("sample")) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment