Improve usability of bitfield.encode

This commit is contained in:
Kagami Hiiragi 2015-01-15 17:46:58 +03:00
parent df5e024004
commit 3d5968bcc1
2 changed files with 5 additions and 0 deletions

View File

@ -520,6 +520,9 @@ var bitfield = function(size) {
encode: function(features) {
var buf = new Buffer(bytesize);
buf.fill(0);
if (!Array.isArray(features)) {
features = [features];
}
features.forEach(function(feature) {
assert(feature >= 0, "Bad feature");
assert(feature <= (size - 1), "Bad feature");

View File

@ -259,6 +259,7 @@ describe("Common structures", function() {
it("should encode", function() {
expect(serviceFeatures.encode([serviceFeatures.NODE_NETWORK]).toString("hex")).to.equal("0000000000000001");
expect(serviceFeatures.encode(serviceFeatures.NODE_NETWORK).toString("hex")).to.equal("0000000000000001");
});
});
@ -269,6 +270,7 @@ describe("Common structures", function() {
it("should encode", function() {
expect(pubkeyFeatures.encode([pubkeyFeatures.INCLUDE_DESTINATION, pubkeyFeatures.DOES_ACK]).toString("hex")).to.equal("c0000000");
expect(pubkeyFeatures.encode(pubkeyFeatures.INCLUDE_DESTINATION).toString("hex")).to.equal("40000000");
});
});
});