Skip to content
This repository was archived by the owner on Aug 17, 2023. It is now read-only.

Commit 6220d30

Browse files
authored
Merge pull request #24 from LinusU/prefer-const
Prefer const over let
2 parents cffac2a + 0598dce commit 6220d30

23 files changed

Lines changed: 162 additions & 162 deletions

benchmark/cluster.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ const IoRedis = require('ioredis')
88
thunk(function * () {
99
let timeT = 0
1010
let timeI = 0
11-
let testLen = 100000
12-
let titleT = 'redis(T):'
13-
let titleI = 'redis(I):'
14-
let clientT = redis.createClient(7000)
15-
let clientI = new IoRedis.Cluster([
11+
const testLen = 100000
12+
const titleT = 'redis(T):'
13+
const titleI = 'redis(I):'
14+
const clientT = redis.createClient(7000)
15+
const clientI = new IoRedis.Cluster([
1616
{port: 7000, host: '127.0.0.1'},
1717
{port: 7001, host: '127.0.0.1'},
1818
{port: 7002, host: '127.0.0.1'}
1919
])
2020

21-
let queue = []
21+
const queue = []
2222
while (queue.length < testLen) queue.push(queue.length)
2323

24-
let smallStr = 'teambition'
25-
let longStr = (new Array(4097).join('-'))
24+
const smallStr = 'teambition'
25+
const longStr = (new Array(4097).join('-'))
2626

2727
function printResult (title, timeT, timeI) {
2828
console.log(titleT, title, Math.floor(testLen * 1000 / timeT) + ' ops/sec', '100%')

benchmark/index.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ function * bench () {
1818
let timeN = 0
1919
let timeT = 0
2020
let timeI = 0
21-
let testLen = 100000
22-
let titleN = 'redis(N):'
23-
let titleT = 'redis(T):'
24-
let titleI = 'redis(I):'
25-
let clientN = nodeRedis.createClient(6380)
26-
let clientT = redis.createClient(6381)
27-
let clientI = new IoRedis(6382)
28-
29-
let queue = []
21+
const testLen = 100000
22+
const titleN = 'redis(N):'
23+
const titleT = 'redis(T):'
24+
const titleI = 'redis(I):'
25+
const clientN = nodeRedis.createClient(6380)
26+
const clientT = redis.createClient(6381)
27+
const clientI = new IoRedis(6382)
28+
29+
const queue = []
3030
while (queue.length < testLen) queue.push(queue.length)
3131

32-
let smallStr = 'teambition'
33-
let longStr = (new Array(4097).join('-'))
32+
const smallStr = 'teambition'
33+
const longStr = (new Array(4097).join('-'))
3434

3535
function printResult (title, timeN, timeT, timeI) {
3636
console.log(`\n${title}:`)

examples/demo-generator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ client.select(1)(function * (error, res) {
1212
console.log('foo -> %s', yield this.get('foo'))
1313
console.log('bar -> %s', yield this.get('bar'))
1414

15-
let user = {
15+
const user = {
1616
id: 'u001',
1717
name: 'jay',
1818
age: 24

lib/client.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ RedisState.prototype.resetConnection = function () {
6767
if (this.connection && this.connection.connected) return
6868

6969
let connection = null
70-
let keys = Object.keys(this.pool)
70+
const keys = Object.keys(this.pool)
7171
for (let i = 0; i < keys.length; i++) {
7272
connection = this.pool[keys[i]]
7373
if (connection.connected) break
@@ -79,7 +79,7 @@ function RedisClient (addressArray, options) {
7979
EventEmitter.call(this)
8080
tool.setPrivate(this, '_redisState', new RedisState(options, addressArray))
8181

82-
let ctx = this
82+
const ctx = this
8383
this._redisState.thunkE = thunks(function (error) {
8484
ctx.emit('error', error)
8585
})
@@ -95,8 +95,8 @@ util.inherits(RedisClient, EventEmitter)
9595
initCommands(RedisClient.prototype)
9696

9797
RedisClient.prototype.clientConnect = function () {
98-
let ctx = this
99-
let redisState = this._redisState
98+
const ctx = this
99+
const redisState = this._redisState
100100
redisState.ended = false
101101
createConnections(this, redisState.addressArray)
102102

@@ -111,7 +111,7 @@ RedisClient.prototype.clientConnect = function () {
111111
// deprecate!
112112
RedisClient.prototype.clientSwitch = function (id) {
113113
console.warn('clientSwitch is deprecated, It will be removed in next version!')
114-
let redisState = this._redisState
114+
const redisState = this._redisState
115115
id = redisState.slots[id] || id
116116
if (!redisState.pool[id]) throw new Error(id + ' is not exist')
117117
redisState.slots[-1] = id
@@ -120,8 +120,8 @@ RedisClient.prototype.clientSwitch = function (id) {
120120

121121
RedisClient.prototype.clientUnref = function () {
122122
if (this._redisState.ended) return
123-
for (let key of Object.keys(this._redisState.pool)) {
124-
let connection = this._redisState.pool[key]
123+
for (const key of Object.keys(this._redisState.pool)) {
124+
const connection = this._redisState.pool[key]
125125
if (connection.connected) connection.socket.unref()
126126
else {
127127
connection.socket.once('connect', function () {
@@ -132,28 +132,28 @@ RedisClient.prototype.clientUnref = function () {
132132
}
133133

134134
RedisClient.prototype.clientEnd = function (hadError) {
135-
let redisState = this._redisState
135+
const redisState = this._redisState
136136
if (redisState.ended) return
137137
redisState.ended = true
138138
redisState.connected = false
139139

140140
clearInterval(redisState.pingInterval)
141141
redisState.pingInterval = null
142142

143-
for (let key of Object.keys(redisState.pool)) {
144-
let connection = redisState.pool[key]
143+
for (const key of Object.keys(redisState.pool)) {
144+
const connection = redisState.pool[key]
145145
if (connection) connection.disconnect()
146146
}
147-
let commandQueue = redisState.commandQueue
148-
let message = (hadError && hadError.toString()) || 'The redis connection was ended'
147+
const commandQueue = redisState.commandQueue
148+
const message = (hadError && hadError.toString()) || 'The redis connection was ended'
149149
while (commandQueue.length) commandQueue.shift().callback(new Error(message))
150150

151151
this.emit('close', hadError)
152152
}
153153

154154
RedisClient.prototype.clientState = function () {
155-
let redisState = this._redisState
156-
let state = {
155+
const redisState = this._redisState
156+
const state = {
157157
pool: {},
158158
ended: redisState.ended,
159159
clientId: redisState.clientId,
@@ -165,8 +165,8 @@ RedisClient.prototype.clientState = function () {
165165
commandQueueLength: redisState.commandQueue.length
166166
}
167167

168-
for (let key of Object.keys(redisState.pool)) {
169-
let connection = redisState.pool[key]
168+
for (const key of Object.keys(redisState.pool)) {
169+
const connection = redisState.pool[key]
170170
state.pool[connection.id] = connection.replicationIds ? connection.replicationIds.slice() : []
171171
}
172172
return state

lib/commands.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -229,18 +229,18 @@ exports.initCommands = function (proto) {
229229
proto.clientCommands = commands
230230

231231
proto.clientCalcSlot = function (reqArray) {
232-
let info = commandsInfo[reqArray[0]]
232+
const info = commandsInfo[reqArray[0]]
233233
if (!info || reqArray.length === 1 || (info[2] === 0 && info[0] !== 1)) return null
234234
// if command have no argument but user provide one, use the argument to calcSlot
235235
// it is useful in cluster for `multi`, `exec`, `discard`, `unwatch` and so on
236-
let keyIndex = info[2] || 1
236+
const keyIndex = info[2] || 1
237237
// Only calc first key, user should ensure that all keys are in a same node.
238-
let slot = calcSlot(reqArray[keyIndex])
238+
const slot = calcSlot(reqArray[keyIndex])
239239
if (info[0] === 1) reqArray.length = 1
240240
return slot
241241
}
242242

243-
for (let command of commands) {
243+
for (const command of commands) {
244244
proto[command] = function () {
245245
return sendCommand(this, command, adjustArgs(arguments))
246246
}
@@ -282,23 +282,23 @@ exports.initCommands = function (proto) {
282282
}
283283

284284
proto.pubsub = function () {
285-
let args = adjustArgs(arguments)
285+
const args = adjustArgs(arguments)
286286
return sendCommand(this, 'pubsub', args, 0, function (res) {
287287
if (args[0].toLowerCase() === 'numsub') res = toHash(res)
288288
return res
289289
})
290290
}
291291

292292
proto.monitor = function (hashKey) {
293-
let args = adjustArgs(arguments)
293+
const args = adjustArgs(arguments)
294294
if (hashKey || !this._redisState.clusterMode) {
295295
return sendCommand(this, 'monitor', args)
296296
}
297297

298298
// monit all nodes in cluster mode
299-
let tasks = []
300-
for (let key of Object.keys(this._redisState.pool)) {
301-
let connection = this._redisState.pool[key]
299+
const tasks = []
300+
for (const key of Object.keys(this._redisState.pool)) {
301+
const connection = this._redisState.pool[key]
302302
if (connection.monitorMode) return
303303
tasks.push(connection.sendCommand('monitor', args))
304304
}
@@ -316,13 +316,13 @@ exports.initCommands = function (proto) {
316316
*/
317317

318318
proto.evalauto = function (script, numkeys, key) {
319-
let args = tool.slice(adjustArgs(arguments))
319+
const args = tool.slice(adjustArgs(arguments))
320320
script = String(args[0]).trim()
321321

322322
return thunk.call(this, function * () {
323323
yield this.clientReady
324324

325-
let slot = args.length < 3 ? null : calcSlot(args[2])
325+
const slot = args.length < 3 ? null : calcSlot(args[2])
326326
let connection = this._redisState.getConnection(slot)
327327
if (connection instanceof Error) throw connection
328328

@@ -337,7 +337,7 @@ exports.initCommands = function (proto) {
337337
}
338338
}
339339

340-
let res = yield connection.sendCommand('eval', args)
340+
const res = yield connection.sendCommand('eval', args)
341341
// connection maybe change with MOVED response
342342
connection = this._redisState.getConnection(slot)
343343
if (connection instanceof Error) throw connection
@@ -346,14 +346,14 @@ exports.initCommands = function (proto) {
346346
})
347347
}
348348

349-
for (let command of ['psubscribe', 'punsubscribe', 'subscribe', 'unsubscribe']) {
349+
for (const command of ['psubscribe', 'punsubscribe', 'subscribe', 'unsubscribe']) {
350350
proto[command] = function () {
351-
let args = adjustArgs(arguments)
351+
const args = adjustArgs(arguments)
352352
return sendCommand(this, command, args, args.length ? (args.length - 1) : 0)
353353
}
354354
}
355355

356-
for (let command of commands) {
356+
for (const command of commands) {
357357
proto[command.toUpperCase()] = proto[command]
358358
}
359359
}
@@ -367,14 +367,14 @@ function adjustArgs (args) {
367367
}
368368

369369
function toArray (hash, array) {
370-
for (let key of Object.keys(hash)) {
370+
for (const key of Object.keys(hash)) {
371371
array.push(key, hash[key])
372372
}
373373
return array
374374
}
375375

376376
function toHash (array) {
377-
let hash = {}
377+
const hash = {}
378378

379379
for (let i = 0, len = array.length; i < len; i += 2) {
380380
hash[array[i]] = array[i + 1]
@@ -384,13 +384,13 @@ function toHash (array) {
384384
}
385385

386386
function formatInfo (info) {
387-
let hash = {}
387+
const hash = {}
388388

389-
for (let line of info.toString().split('\r\n')) {
390-
let index = line.indexOf(':')
389+
for (const line of info.toString().split('\r\n')) {
390+
const index = line.indexOf(':')
391391

392392
if (index === -1) continue
393-
let name = line.slice(0, index)
393+
const name = line.slice(0, index)
394394
hash[name] = line.slice(index + 1)
395395
}
396396

0 commit comments

Comments
 (0)