You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
652 B
JavaScript
34 lines
652 B
JavaScript
2 years ago
|
"use strict";
|
||
|
|
||
|
const BOOLEAN_FALSE = Buffer.from([ 0 ]);
|
||
|
const BOOLEAN_TRUE = Buffer.from([ 1 ]);
|
||
|
|
||
|
module.exports = {
|
||
|
encode: function (value, _asIndexKey) {
|
||
|
return {
|
||
|
value: (value === true)
|
||
|
? BOOLEAN_TRUE
|
||
|
: BOOLEAN_FALSE,
|
||
|
auxiliaryBlob: undefined
|
||
|
};
|
||
|
},
|
||
|
decode: function (buffer, offset) {
|
||
|
let value = buffer.readUInt8(offset);
|
||
|
let booleanValue;
|
||
|
|
||
|
if (value === 0) {
|
||
|
booleanValue = false;
|
||
|
} else if (value === 1) {
|
||
|
booleanValue = true;
|
||
|
} else {
|
||
|
throw new Error(`Invalid byte value while decoding boolean: ${value}`);
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
bytesRead: 1,
|
||
|
value: booleanValue,
|
||
|
auxiliaryBlob: undefined
|
||
|
};
|
||
|
}
|
||
|
};
|