mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2025-12-12 16:02:08 +01:00
Added support for bin, dec, hex modes to formatter. Unit tests support
This commit is contained in:
@@ -7,7 +7,8 @@
|
|||||||
|
|
||||||
app.set('cmdConfig', core.ObservableObject.create({
|
app.set('cmdConfig', core.ObservableObject.create({
|
||||||
emphasizeBytes: true,
|
emphasizeBytes: true,
|
||||||
theme: 'dark'
|
theme: 'dark',
|
||||||
|
mode: 'dec'
|
||||||
}));
|
}));
|
||||||
|
|
||||||
app.debugMode = false;
|
app.debugMode = false;
|
||||||
|
|||||||
@@ -2,27 +2,38 @@ app.set("formatter", function() {
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var should = app.get('should');
|
var should = app.get('should');
|
||||||
|
var is = app.get('is');
|
||||||
|
|
||||||
return {
|
return {
|
||||||
toBinaryString: function(num, totalLength) {
|
formatString: function(num, mode) {
|
||||||
|
mode = mode || "bin";
|
||||||
|
|
||||||
var binaryStr = num.toString(2),
|
var convertedString = num.toString(getBase(mode));
|
||||||
formatted = [],
|
return convertedString;
|
||||||
i;
|
|
||||||
|
|
||||||
if(totalLength != null) {
|
},
|
||||||
should.bePositiveInteger(totalLength);
|
padLeft: function (str, length, symbol) {
|
||||||
|
var sb = Array.prototype.slice.call(str), symbol = symbol || "0";
|
||||||
|
|
||||||
|
if(length == null) {
|
||||||
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(i = 0; i<binaryStr.length; i++) {
|
while(length > sb.length) {
|
||||||
formatted.push(binaryStr[i]);
|
sb.unshift(symbol);
|
||||||
}
|
}
|
||||||
|
|
||||||
while(totalLength > formatted.length) {
|
return sb.join('');
|
||||||
formatted.unshift('0');
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
return formatted.join('');
|
|
||||||
|
|
||||||
|
function getBase(mode) {
|
||||||
|
switch (mode){
|
||||||
|
case 'bin': return 2;
|
||||||
|
case 'hex': return 16;
|
||||||
|
case 'dec': return 10;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -14,9 +14,9 @@ app.compose(function () {
|
|||||||
|
|
||||||
var model = Object.create(expr);
|
var model = Object.create(expr);
|
||||||
model.result = result;
|
model.result = result;
|
||||||
model.operand1Binary = formatter.toBinaryString(expr.operand1, maxLen);
|
model.operand1Binary = formatter.padLeft(formatter.formatString(expr.operand1), maxLen);
|
||||||
model.operand2Binary = formatter.toBinaryString(expr.operand2, maxLen);
|
model.operand2Binary = formatter.padLeft(formatter.formatString(expr.operand2), maxLen);
|
||||||
model.resultBinary = formatter.toBinaryString(result, maxLen);
|
model.resultBinary = formatter.padLeft(formatter.formatString(result), maxLen);
|
||||||
|
|
||||||
var templateId = /<<|>>/.test(model.sign) ? 'shiftExpressionView' : 'binaryExpressionView';
|
var templateId = /<<|>>/.test(model.sign) ? 'shiftExpressionView' : 'binaryExpressionView';
|
||||||
var template = app.template(templateId)
|
var template = app.template(templateId)
|
||||||
@@ -43,7 +43,7 @@ app.compose(function () {
|
|||||||
binCell.className = 'bin';
|
binCell.className = 'bin';
|
||||||
|
|
||||||
decCell.textContent = o;
|
decCell.textContent = o;
|
||||||
binCell.textContent = formatter.toBinaryString(o, maxLen);
|
binCell.textContent = formatter.formatString(o, maxLen);
|
||||||
});
|
});
|
||||||
|
|
||||||
colorizeBits(table);
|
colorizeBits(table);
|
||||||
|
|||||||
@@ -28,6 +28,10 @@
|
|||||||
|
|
||||||
array: function(obj) {
|
array: function(obj) {
|
||||||
return obj instanceof Array;
|
return obj instanceof Array;
|
||||||
|
},
|
||||||
|
|
||||||
|
number: function(num) {
|
||||||
|
return typeof num == "number" && !isNaN(num)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
(function(){
|
(function(){
|
||||||
"use strict";
|
"use strict";
|
||||||
|
var is = window.core.is;
|
||||||
|
|
||||||
window.core.should = {
|
window.core.should = {
|
||||||
beNumber: function (num, name) {
|
beNumber: function (num, name) {
|
||||||
this.check(typeof num == "number" && !isNaN(num), num + " is not a number");
|
this.check(is.number(num), num + " is not a number");
|
||||||
this.check(isFinite(num), append(name, "is an infinite number"));
|
this.check(isFinite(num), append(name, "is an infinite number"));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
20
tests/domain/formatterSpec.js
Normal file
20
tests/domain/formatterSpec.js
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
describe('expression formatter', function () {
|
||||||
|
var di = app.di.clone();
|
||||||
|
var formatter = di.resolve('formatter');
|
||||||
|
|
||||||
|
it('should format number to binary by default', function() {
|
||||||
|
expect(formatter.formatString(10)).toBe("1010");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should format number hexadecimal', function() {
|
||||||
|
expect(formatter.formatString(15, 'hex')).toBe("f");
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should format number decimal', function() {
|
||||||
|
expect(formatter.formatString(16, 'dec')).toBe('16');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should respect padding', function() {
|
||||||
|
expect(formatter.padLeft("a", 6)).toBe("00000a");
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user