Skip to content

Commit

Permalink
fix code to match standard rules
Browse files Browse the repository at this point in the history
  • Loading branch information
notslang committed May 26, 2016
1 parent 2536c42 commit 1eff280
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 24 deletions.
10 changes: 7 additions & 3 deletions benchmark/buffer-vs-string.js
@@ -1,10 +1,14 @@
/* global suite, bench */
var fs = require('fs')
var path = require('path')
var bencode = require('../')
var buf = require('fs').readFileSync(__dirname + '/test.torrent')
var str = buf.toString('ascii')

var buffer = fs.readFileSync(path.join(__dirname, 'test.torrent'))
var str = buffer.toString('ascii')

suite('buffer vs string', function () {
bench('buffer', function () {
bencode.decode(buf)
bencode.decode(buffer)
})
bench('string', function () {
bencode.decode(str)
Expand Down
6 changes: 4 additions & 2 deletions benchmark/decode.js
@@ -1,19 +1,21 @@
/* global suite, bench */
var fs = require('fs')
var path = require('path')

var bencode = require('../')
var bencoding = require('bencoding')
var bncode = require('bncode')
var dht = require('dht.js/lib/dht/bencode')
var dhtBencode = require('dht-bencode')

var buffer = fs.readFileSync(__dirname + '/test.torrent')
var buffer = fs.readFileSync(path.join(__dirname, 'test.torrent'))

suite('decode to buffer', function () {
bench('bencode', function () {
bencode.decode(buffer)
})
bench('bencoding', function () {
var v = bencoding.decode(buffer)
bencoding.decode(buffer)
})
bench('dht-bencode', function () {
dhtBencode.bdecode(buffer)
Expand Down
4 changes: 3 additions & 1 deletion benchmark/encode.js
@@ -1,12 +1,14 @@
/* global suite, bench */
var fs = require('fs')
var path = require('path')

var bencode = require('../')
var bencoding = require('bencoding')
var bncode = require('bncode')
var dht = require('dht.js/lib/dht/bencode')
var dhtBencode = require('dht-bencode')

var buffer = fs.readFileSync(__dirname + '/test.torrent')
var buffer = fs.readFileSync(path.join(__dirname, 'test.torrent'))
var object = bencode.decode(buffer)
var objectUtf8 = bencode.decode(buffer, 'utf8')
var objectAscii = bencode.decode(buffer, 'ascii')
Expand Down
4 changes: 3 additions & 1 deletion benchmark/encoding-length.js
@@ -1,7 +1,9 @@
/* global suite, bench */
var fs = require('fs')
var path = require('path')
var bencode = require('..')

var buffer = fs.readFileSync(__dirname + '/test.torrent')
var buffer = fs.readFileSync(path.join(__dirname, 'test.torrent'))
var torrent = bencode.decode(buffer)

suite('encoding length', function () {
Expand Down
13 changes: 4 additions & 9 deletions lib/decode.js
Expand Up @@ -39,16 +39,12 @@ decode.next = function () {
switch (decode.data[decode.position]) {
case 0x64:
return decode.dictionary()
break
case 0x6C:
return decode.list()
break
case 0x69:
return decode.integer()
break
default:
return decode.buffer()
break
}
}

Expand All @@ -57,9 +53,8 @@ decode.find = function (chr) {
var c = decode.data.length
var d = decode.data

while(i < c) {
if (d[i] === chr)
return i
while (i < c) {
if (d[i] === chr) return i
i++
}

Expand All @@ -75,7 +70,7 @@ decode.dictionary = function () {

var dict = {}

while(decode.data[decode.position] !== 0x65) {
while (decode.data[decode.position] !== 0x65) {
dict[decode.buffer()] = decode.next()
}

Expand All @@ -89,7 +84,7 @@ decode.list = function () {

var lst = []

while(decode.data[decode.position] !== 0x65) {
while (decode.data[decode.position] !== 0x65) {
lst.push(decode.next())
}

Expand Down
8 changes: 4 additions & 4 deletions lib/encode.js
Expand Up @@ -48,9 +48,9 @@ encode._encode = function (buffers, data) {
}
}

var buffE = new Buffer('e'),
buffD = new Buffer('d'),
buffL = new Buffer('l')
var buffE = new Buffer('e')
var buffD = new Buffer('d')
var buffL = new Buffer('l')

encode.buffer = function (buffers, data) {
buffers.push(new Buffer(Buffer.byteLength(data) + ':' + data))
Expand Down Expand Up @@ -93,7 +93,7 @@ encode.dict = function (buffers, data) {
}

encode.list = function (buffers, data) {
var i = 0, j = 1
var i = 0
var c = data.length
buffers.push(buffL)

Expand Down
2 changes: 1 addition & 1 deletion test/abstract-encoding.test.js
Expand Up @@ -36,7 +36,7 @@ test('abstract encoding', function (t) {

t.test('decode.bytes', function (t) {
var input = new Buffer('d7:integeri12345e6:string11:Hello Worlde')
var output = bencode.decode(input)
bencode.decode(input)
t.plan(1)
t.equal(bencode.decode.bytes, input.length)
})
Expand Down
4 changes: 2 additions & 2 deletions test/decode.buffer.test.js
Expand Up @@ -38,8 +38,8 @@ test('bencode#decode(x)', function (t) {
t.deepEqual(
bencode.decode('d4:spaml1:a1:bee'),
{ spam: [
new Buffer('a'),
new Buffer('b')
new Buffer('a'),
new Buffer('b')
] }
)
t.deepEqual(
Expand Down
2 changes: 1 addition & 1 deletion test/encode.test.js
Expand Up @@ -25,7 +25,7 @@ test('bencode#encode()', function (t) {
t.plan(1)
var data = {
12: 'Hello World',
34: 12345,
34: 12345
}
t.equal(bencode.encode(data).toString(), 'd2:1211:Hello World2:34i12345ee')
})
Expand Down

0 comments on commit 1eff280

Please sign in to comment.