Merge pull request #10 from mafintosh/bytes

bytesRead, bytesWritten -> bytes
master
Chris Dickinson 10 years ago
commit f48d840c95

@ -23,13 +23,13 @@ and returns that array filled with integers.
decodes `data`, which can be either a buffer or array of integers, from position `offset` or default 0 and returns the decoded original integer. decodes `data`, which can be either a buffer or array of integers, from position `offset` or default 0 and returns the decoded original integer.
### varint.decode.bytesRead ### varint.decode.bytes
if you also require the length (number of bytes) that were required to decode the integer you can access it via `varint.decode.bytesRead`. this is an integer property that will tell you the number of bytes that the last .decode() call had to use to decode. if you also require the length (number of bytes) that were required to decode the integer you can access it via `varint.decode.bytes`. this is an integer property that will tell you the number of bytes that the last .decode() call had to use to decode.
### varint.encode.bytesWritten ### varint.encode.bytes
similar to `bytesRead` when encoding a number it can be useful to know how many bytes where written (especially if you pass an output array). you can access this via `varint.encode.bytesWritten` which holds the number of bytes written in the last encode. similar to `decode.bytes` when encoding a number it can be useful to know how many bytes where written (especially if you pass an output array). you can access this via `varint.encode.bytes` which holds the number of bytes written in the last encode.
### varint.encodingLength(num) ### varint.encodingLength(num)

@ -23,7 +23,7 @@ function read(buf, offset) {
shift += 7 shift += 7
} while (b >= MSB) } while (b >= MSB)
read.bytesRead = counter - offset read.bytes = counter - offset
return res return res
} }

@ -20,7 +20,7 @@ function encode(num, out, offset) {
} }
out[offset] = num | 0 out[offset] = num | 0
encode.bytesWritten = offset - oldOffset + 1 encode.bytes = offset - oldOffset + 1
return out return out
} }

@ -13,7 +13,7 @@ test('fuzz test', function(assert) {
encoded = encode(expect) encoded = encode(expect)
var data = decode(encoded) var data = decode(encoded)
assert.equal(expect, data, 'fuzz test: ' + expect.toString()) assert.equal(expect, data, 'fuzz test: ' + expect.toString())
assert.equal(decode.bytesRead, encoded.length) assert.equal(decode.bytes, encoded.length)
} }
assert.end() assert.end()
@ -25,7 +25,7 @@ test('test single byte works as expected', function(assert) {
buf[1] = 2 buf[1] = 2
var data = decode(buf) var data = decode(buf)
assert.equal(data, 300, 'should equal 300') assert.equal(data, 300, 'should equal 300')
assert.equal(decode.bytesRead, 2) assert.equal(decode.bytes, 2)
assert.end() assert.end()
}) })
@ -43,7 +43,7 @@ test('test decode single bytes', function(assert) {
buf[0] = expected buf[0] = expected
var data = decode(buf) var data = decode(buf)
assert.equal(data, expected) assert.equal(data, expected)
assert.equal(decode.bytesRead, 1) assert.equal(decode.bytes, 1)
assert.end() assert.end()
}) })
@ -54,21 +54,21 @@ test('test decode multiple bytes with zero', function(assert) {
buf[1] = expected buf[1] = expected
var data = decode(buf) var data = decode(buf)
assert.equal(data, expected << 7) assert.equal(data, expected << 7)
assert.equal(decode.bytesRead, 2) assert.equal(decode.bytes, 2)
assert.end() assert.end()
}) })
test('encode single byte', function(assert) { test('encode single byte', function(assert) {
var expected = randint(parseInt('1111111', '2')) var expected = randint(parseInt('1111111', '2'))
assert.deepEqual(encode(expected), [expected]) assert.deepEqual(encode(expected), [expected])
assert.equal(encode.bytesWritten, 1) assert.equal(encode.bytes, 1)
assert.end() assert.end()
}) })
test('encode multiple byte with zero first byte', function(assert) { test('encode multiple byte with zero first byte', function(assert) {
var expected = 0x0F00 var expected = 0x0F00
assert.deepEqual(encode(expected), [0x80, 0x1E]) assert.deepEqual(encode(expected), [0x80, 0x1E])
assert.equal(encode.bytesWritten, 2) assert.equal(encode.bytes, 2)
assert.end() assert.end()
}) })
@ -101,7 +101,7 @@ test('fuzz test - big', function(assert) {
encoded = encode(expect) encoded = encode(expect)
var data = decode(encoded) var data = decode(encoded)
assert.equal(expect, data, 'fuzz test: ' + expect.toString()) assert.equal(expect, data, 'fuzz test: ' + expect.toString())
assert.equal(decode.bytesRead, encoded.length) assert.equal(decode.bytes, encoded.length)
} }
assert.end() assert.end()

Loading…
Cancel
Save