Skip to content

Commit b0f3a17

Browse files
authored
Merge pull request #2 from LinusU/prefer-const
Prefer const over let
2 parents 49b827c + efbf4d5 commit b0f3a17

3 files changed

Lines changed: 38 additions & 38 deletions

File tree

example/redis_client.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Client extends EventEmitter {
1414
this._resp = new Resp()
1515
.on('error', (error) => this.emit('error', error))
1616
.on('data', (data) => {
17-
let cb = this._queue.shift()
17+
const cb = this._queue.shift()
1818
if (!cb) return this.emit('error', new Error('Unexpected reply: ' + data))
1919
if (data instanceof Error) cb[1](data)
2020
else cb[0](data)
@@ -42,31 +42,31 @@ class Client extends EventEmitter {
4242

4343
// tman example/redis_client.js
4444
tman('simple redis client', function () {
45-
let client = new Client(6379)
45+
const client = new Client(6379)
4646

4747
tman.it('info', function * () {
48-
let res = yield client.cmd('info')
48+
const res = yield client.cmd('info')
4949
console.log('INFO:', res)
5050
assert.ok(res.indexOf('redis_version') > 0)
5151
})
5252

5353
tman.it('set', function * () {
54-
let res = yield client.cmd('set', 'resp', 'hello')
54+
const res = yield client.cmd('set', 'resp', 'hello')
5555
console.log('SET:', res)
5656
assert.strictEqual(res, 'OK')
5757
})
5858

5959
tman.it('get', function * () {
60-
let res = yield client.cmd('get', 'resp')
60+
const res = yield client.cmd('get', 'resp')
6161
console.log('GET:', res)
6262
assert.strictEqual(res, 'hello')
6363
})
6464

6565
tman.it('ping', function * () {
6666
let count = 10000
67-
let time = Date.now()
67+
const time = Date.now()
6868
while (count--) {
69-
let res = yield client.cmd('ping')
69+
const res = yield client.cmd('ping')
7070
assert.strictEqual(res, 'PONG')
7171
}
7272
console.log('PING:', (10000 * 1000 / (Date.now() - time)).toFixed(2), 'ops/sec')

index.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class Resp extends EventEmitter {
4242

4343
static encodeBufBulk (buf) {
4444
if (!Buffer.isBuffer(buf)) throw new TypeError(String(buf) + ' must be Buffer object')
45-
let prefix = '$' + buf.length + CRLF
46-
let buffer = Buffer.allocUnsafe(prefix.length + buf.length + 2)
45+
const prefix = '$' + buf.length + CRLF
46+
const buffer = Buffer.allocUnsafe(prefix.length + buf.length + 2)
4747
buffer.write(prefix)
4848
buf.copy(buffer, prefix.length)
4949
buffer.write(CRLF, prefix.length + buf.length)
@@ -52,9 +52,9 @@ class Resp extends EventEmitter {
5252

5353
static encodeArray (arr) {
5454
if (!Array.isArray(arr)) throw new Error(String(arr) + ' must be Array object')
55-
let prefix = '*' + arr.length + CRLF
55+
const prefix = '*' + arr.length + CRLF
5656
let length = prefix.length
57-
let bufs = [Buffer.from(prefix)]
57+
const bufs = [Buffer.from(prefix)]
5858

5959
for (let buf, i = 0, len = arr.length; i < len; i++) {
6060
buf = arr[i]
@@ -71,7 +71,7 @@ class Resp extends EventEmitter {
7171
if (!Array.isArray(arr) || arr.length === 0) {
7272
throw new Error(String(arr) + ' must be array of value')
7373
}
74-
let bulks = Array(arr.length)
74+
const bulks = Array(arr.length)
7575
for (let i = 0, len = arr.length; i < len; i++) {
7676
bulks[i] = Buffer.isBuffer(arr[i]) ? Resp.encodeBufBulk(arr[i]) : Resp.encodeBulk(arr[i])
7777
}
@@ -80,7 +80,7 @@ class Resp extends EventEmitter {
8080

8181
// Decode a RESP buffer to RESP value
8282
static decode (buf, bufBulk) {
83-
let res = parseBuffer(buf, 0, bufBulk)
83+
const res = parseBuffer(buf, 0, bufBulk)
8484
if (!res || res.index < buf.length) throw new Error('Parse "' + buf + '" failed')
8585
if (res instanceof Error) throw res
8686
return res.content
@@ -105,8 +105,8 @@ class Resp extends EventEmitter {
105105

106106
if (!this._buf) this._buf = buf
107107
else {
108-
let ret = this._buf.length - this._pos
109-
let _buf = Buffer.allocUnsafe(buf.length + ret)
108+
const ret = this._buf.length - this._pos
109+
const _buf = Buffer.allocUnsafe(buf.length + ret)
110110

111111
this._buf.copy(_buf, 0, this._pos)
112112
buf.copy(_buf, ret)
@@ -115,7 +115,7 @@ class Resp extends EventEmitter {
115115
}
116116

117117
while (this._pos < this._buf.length) {
118-
let result = parseBuffer(this._buf, this._pos, this._bufBulk)
118+
const result = parseBuffer(this._buf, this._pos, this._bufBulk)
119119
if (result == null) {
120120
this.emit('drain')
121121
return true
@@ -153,8 +153,8 @@ class ReadRes {
153153
}
154154

155155
function readBuffer (buf, i) {
156-
let start = i
157-
let len = buf.length
156+
const start = i
157+
const len = buf.length
158158
while (i < len && !isCRLF(buf, i)) i++
159159
return i >= len ? null : new ReadRes(buf.utf8Slice(start, i), i + 2)
160160
}
@@ -190,7 +190,7 @@ function parseBuffer (buf, index, bufBulk) {
190190
if (result == null) return result
191191
num = parseInteger(result.content)
192192
if (num == null || num < -1) return new Error('Parse "$" failed, invalid length')
193-
let endIndex = result.index + num
193+
const endIndex = result.index + num
194194

195195
if (num === -1) {
196196
// Null Bulk
@@ -231,7 +231,7 @@ function parseBuffer (buf, index, bufBulk) {
231231
}
232232

233233
function parseInteger (str) {
234-
let num = +str
234+
const num = +str
235235
return (str && Number.isInteger(num)) ? num : null
236236
}
237237

test/index.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function test (Resp) {
9595
assert.strictEqual(Resp.decode(Resp.encodeError(new Error('err'))) instanceof Error, true)
9696
assert.strictEqual(Resp.decode(Resp.encodeError(new Error('err')), true) instanceof Error, true)
9797

98-
let err = Resp.decode(Resp.encodeError(new TypeError('err')))
98+
const err = Resp.decode(Resp.encodeError(new TypeError('err')))
9999
assert.strictEqual(err.name, 'TypeError')
100100
assert.strictEqual(err.code, 'TypeError')
101101
assert.strictEqual(err.message, 'err')
@@ -126,19 +126,19 @@ function test (Resp) {
126126
assert.throws(function () { Resp.decode(1) })
127127
assert.throws(function () { Resp.decode(Buffer.from('123')) })
128128
assert.throws(function () {
129-
let buf = Resp.encodeBulk('123')
129+
const buf = Resp.encodeBulk('123')
130130
buf[buf.length - 1] = 0
131131
Resp.decode(buf)
132132
})
133133
assert.throws(function () {
134-
let buf = Buffer.concat([Resp.encodeBulk('123'), Buffer.from('1')])
134+
const buf = Buffer.concat([Resp.encodeBulk('123'), Buffer.from('1')])
135135
Resp.decode(buf)
136136
})
137137
})
138138

139139
tman.it('new Resp()', function (done) {
140-
let result = []
141-
let reply = new Resp()
140+
const result = []
141+
const reply = new Resp()
142142

143143
reply
144144
.on('data', function (data) {
@@ -160,7 +160,7 @@ function test (Resp) {
160160
})
161161

162162
tman.it('new Resp({bufBulk: true})', function (done) {
163-
let reply = new Resp({bufBulk: true})
163+
const reply = new Resp({bufBulk: true})
164164

165165
reply
166166
.on('data', function (data) {
@@ -174,8 +174,8 @@ function test (Resp) {
174174
})
175175

176176
tman.it('new Resp(): Pipelining data', function (done) {
177-
let result = []
178-
let reply = new Resp()
177+
const result = []
178+
const reply = new Resp()
179179

180180
reply
181181
.on('data', function (data) {
@@ -193,7 +193,7 @@ function test (Resp) {
193193
})
194194

195195
tman.it('new Resp(): with non resp buffer', function (done) {
196-
let reply = new Resp()
196+
const reply = new Resp()
197197

198198
reply
199199
.on('error', function (error) {
@@ -206,8 +206,8 @@ function test (Resp) {
206206
})
207207

208208
tman.it('new Resp(): with error data', function (done) {
209-
let result = []
210-
let reply = new Resp()
209+
const result = []
210+
const reply = new Resp()
211211

212212
reply
213213
.on('data', function (data) {
@@ -231,9 +231,9 @@ function test (Resp) {
231231
tman.it('new Resp(): chaos', function (done) {
232232
this.timeout(100000)
233233

234-
let result = []
235-
let reply = new Resp()
236-
let buf = Resp.encodeArray([
234+
const result = []
235+
const reply = new Resp()
236+
const buf = Resp.encodeArray([
237237
Resp.encodeNull(),
238238
Resp.encodeString('OKOKOKOK'),
239239
Resp.encodeInteger(123456789),
@@ -263,7 +263,7 @@ function test (Resp) {
263263
})
264264

265265
let start = 0
266-
let length = bufs.length
266+
const length = bufs.length
267267
consumer()
268268

269269
function consumer () {
@@ -279,10 +279,10 @@ function test (Resp) {
279279
tman.it('new Resp(): bench', function (done) {
280280
this.timeout(100000)
281281

282-
let result = []
283-
let reply = new Resp()
282+
const result = []
283+
const reply = new Resp()
284284

285-
let buf = Resp.encodeArray([
285+
const buf = Resp.encodeArray([
286286
Resp.encodeString('OK'),
287287
Resp.encodeString('QUEUED'),
288288
Resp.encodeString('QUEUED'),

0 commit comments

Comments
 (0)