huge regactoring

This commit is contained in:
Borys Levytskyi
2015-04-02 22:53:06 +03:00
parent 0b3e0ed4fb
commit cadafab3ec
15 changed files with 418 additions and 291 deletions

23
app/bitwise/calc.js Normal file
View File

@@ -0,0 +1,23 @@
(function(app, should){
var calc = {};
calc.numberOfBits = function(num) {
should.bePositiveInteger(num);
return Math.floor(Math.log(num) / Math.log(2)) + 1;
};
calc.maxNumberOfBits = function () {
var counts = [], num;
for(var i=0;i<arguments.length; i++)
{
num = arguments[i];
counts.push(this.numberOfBits(num));
}
return Math.max.apply(null, counts);
};
app.service('calc', calc);
})(window.app, window.should);

25
app/bitwise/expression.js Normal file
View File

@@ -0,0 +1,25 @@
(function(bitwise) {
var twoOperandsRegex = /^(\d+)(<<|>>|\||\&|\^)(\d+)$/;
app.service('expression', {
parse: function(string) {
var matches = twoOperandsRegex.exec(string);
if(matches == null) {
return null;
}
console.log(matches);
return {
string:matches[0],
operand1: parseInt(matches[1], 10),
sign: matches[2],
operand2: parseInt(matches[3], 10),
result: function() {
return eval(string);
}
}
}
});
})(window.app);

28
app/bitwise/formatter.js Normal file
View File

@@ -0,0 +1,28 @@
(function(should, app){
app.service("formatter", {
toBinaryString: function(num, totalLength) {
var binaryStr = num.toString(2),
formatted = [],
i;
if(totalLength != null) {
should.bePositiveInteger(totalLength);
}
for(i = 0; i<binaryStr.length; i++) {
formatted.push(binaryStr[i]);
}
while(totalLength > formatted.length) {
formatted.unshift('0');
}
return formatted.join('');
}
});
})(window.should, window.app);