Comparing version
37
index.js
@@ -1,2 +0,2 @@ | ||
var readUInt4 = function(buffer, cursor) { | ||
var readUInt4BE = function(buffer, cursor) { | ||
if(cursor % 1) | ||
@@ -8,3 +8,3 @@ return buffer.readUInt8(Math.floor(cursor)) & 15; | ||
var writeUInt4 = function(buffer, value, cursor) { | ||
var writeUInt4BE = function(buffer, value, cursor) { | ||
if(value >= 16) | ||
@@ -15,11 +15,32 @@ throw(new Error('value is out of bounds')); | ||
if(cursor % 1) // Second half byte | ||
buffer.writeUInt8((readUInt4(buffer, byteLoc) << 4 | value), byteLoc); | ||
buffer.writeUInt8((readUInt4BE(buffer, byteLoc) << 4 | value), byteLoc); | ||
else // First half byte | ||
buffer.writeUInt8((value << 4 | readUInt4(buffer, cursor)), byteLoc); | ||
buffer.writeUInt8((value << 4 | readUInt4BE(buffer, cursor)), byteLoc); | ||
}; | ||
module.exports.read = readUInt4; | ||
module.exports.readUInt4 = readUInt4; | ||
var readUInt4LE = function(buffer, cursor) { | ||
if(cursor % 1) | ||
return buffer.readUInt8(Math.floor(cursor)) >> 4; | ||
else | ||
return buffer.readUInt8(cursor) & 15; | ||
}; | ||
module.exports.write = writeUInt4; | ||
module.exports.writeUInt4 = writeUInt4; | ||
var writeUInt4LE = function(buffer, value, cursor) { | ||
if(value >= 16) | ||
throw(new Error('value is out of bounds')); | ||
var byteLoc = Math.floor(cursor); | ||
if(cursor % 1) | ||
buffer.writeUInt8((value << 4 | readUInt4LE(buffer, Math.floor(cursor))), byteLoc); | ||
else | ||
buffer.writeUInt8((readUInt4LE(buffer, byteLoc) << 4 | value), byteLoc); | ||
}; | ||
module.exports.read = readUInt4BE; | ||
module.exports.readUInt4 = readUInt4BE; | ||
module.exports.write = writeUInt4BE; | ||
module.exports.writeUInt4 = writeUInt4BE; | ||
module.exports.readUInt4BE = readUInt4BE; | ||
module.exports.writeUInt4BE = writeUInt4BE; | ||
module.exports.readUInt4LE = readUInt4LE; | ||
module.exports.writeUInt4LE = writeUInt4LE; |
{ | ||
"name": "uint4", | ||
"version": "0.0.0", | ||
"version": "0.1.1", | ||
"description": "Read and write half-bytes to buffers", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
var assert = require('assert'); | ||
describe('UInt4', function() { | ||
describe('uint4', function() { | ||
var readUInt4 = require('../index.js').readUInt4; | ||
var writeUInt4 = require('../index.js').writeUInt4; | ||
var uint4 = require('../index.js'); | ||
describe('#read(buffer, cursor)', function() { | ||
describe('#readUInt4BE(buffer, cursor)', function() { | ||
@@ -17,10 +16,10 @@ it('should read half a byte', function() { | ||
assert.equal(0x03, readUInt4(buf, 0.0)); | ||
assert.equal(0x05, readUInt4(buf, 0.5)); | ||
assert.equal(0x00, readUInt4(buf, 1.0)); | ||
assert.equal(0x01, readUInt4(buf, 1.5)); | ||
assert.equal(0x01, readUInt4(buf, 2.0)); | ||
assert.equal(0x00, readUInt4(buf, 2.5)); | ||
assert.equal(0x0f, readUInt4(buf, 3.0)); | ||
assert.equal(0x0f, readUInt4(buf, 3.5)); | ||
assert.equal(0x03, uint4.readUInt4BE(buf, 0.0)); | ||
assert.equal(0x05, uint4.readUInt4BE(buf, 0.5)); | ||
assert.equal(0x00, uint4.readUInt4BE(buf, 1.0)); | ||
assert.equal(0x01, uint4.readUInt4BE(buf, 1.5)); | ||
assert.equal(0x01, uint4.readUInt4BE(buf, 2.0)); | ||
assert.equal(0x00, uint4.readUInt4BE(buf, 2.5)); | ||
assert.equal(0x0f, uint4.readUInt4BE(buf, 3.0)); | ||
assert.equal(0x0f, uint4.readUInt4BE(buf, 3.5)); | ||
}); | ||
@@ -30,3 +29,3 @@ | ||
describe('#write(buffer, value, cursor)', function() { | ||
describe('#writeUInt4BE(buffer, value, cursor)', function() { | ||
@@ -36,10 +35,10 @@ it('should write half a byte', function() { | ||
writeUInt4(buf, 0x03, 0.0); | ||
writeUInt4(buf, 0x05, 0.5); | ||
writeUInt4(buf, 0x00, 1.0); | ||
writeUInt4(buf, 0x01, 1.5); | ||
writeUInt4(buf, 0x01, 2.0); | ||
writeUInt4(buf, 0x00, 2.5); | ||
writeUInt4(buf, 0x0f, 3.0); | ||
writeUInt4(buf, 0x0f, 3.5); | ||
uint4.writeUInt4BE(buf, 0x03, 0.0); | ||
uint4.writeUInt4BE(buf, 0x05, 0.5); | ||
uint4.writeUInt4BE(buf, 0x00, 1.0); | ||
uint4.writeUInt4BE(buf, 0x01, 1.5); | ||
uint4.writeUInt4BE(buf, 0x01, 2.0); | ||
uint4.writeUInt4BE(buf, 0x00, 2.5); | ||
uint4.writeUInt4BE(buf, 0x0f, 3.0); | ||
uint4.writeUInt4BE(buf, 0x0f, 3.5); | ||
@@ -62,2 +61,53 @@ assert.equal(0x35, buf[0]); | ||
describe('#readUInt4LE(buffer, cursor)', function() { | ||
it('should read half a byte', function() { | ||
var buf = new Buffer(4); | ||
buf[0] = 0x53; | ||
buf[1] = 0x10; | ||
buf[2] = 0x01; | ||
buf[3] = 0xff; | ||
assert.equal(0x03, uint4.readUInt4LE(buf, 0.0)); | ||
assert.equal(0x05, uint4.readUInt4LE(buf, 0.5)); | ||
assert.equal(0x00, uint4.readUInt4LE(buf, 1.0)); | ||
assert.equal(0x01, uint4.readUInt4LE(buf, 1.5)); | ||
assert.equal(0x01, uint4.readUInt4LE(buf, 2.0)); | ||
assert.equal(0x00, uint4.readUInt4LE(buf, 2.5)); | ||
assert.equal(0x0f, uint4.readUInt4LE(buf, 3.0)); | ||
assert.equal(0x0f, uint4.readUInt4LE(buf, 3.5)); | ||
}); | ||
}); | ||
describe('#writeUInt4LE(buffer, value, cursor)', function() { | ||
it('should write half a byte', function() { | ||
var buf = new Buffer(4); | ||
uint4.writeUInt4LE(buf, 0x03, 0.0); | ||
uint4.writeUInt4LE(buf, 0x05, 0.5); | ||
uint4.writeUInt4LE(buf, 0x00, 1.0); | ||
uint4.writeUInt4LE(buf, 0x01, 1.5); | ||
uint4.writeUInt4LE(buf, 0x01, 2.0); | ||
uint4.writeUInt4LE(buf, 0x00, 2.5); | ||
uint4.writeUInt4LE(buf, 0x0f, 3.0); | ||
uint4.writeUInt4LE(buf, 0x0f, 3.5); | ||
assert.equal(0x53, buf[0]); | ||
assert.equal(0x10, buf[1]); | ||
assert.equal(0x01, buf[2]); | ||
assert.equal(0xff, buf[3]); | ||
}); | ||
it('should throw when given values >= 16', function() { | ||
var buf = new Buffer(4); | ||
assert.throws(function() { | ||
writeUInt4LE(buf, 20, 0); | ||
}); | ||
}); | ||
}); | ||
}); |
6247
69.16%122
90.63%