You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-13 10:02:24 +03:00
chore: refactor parts out of the index.js file
This commit is contained in:
@@ -1,23 +1,15 @@
|
||||
'use strict'
|
||||
|
||||
const commands = require('redis-commands')
|
||||
const Multi = require('./multi')
|
||||
const RedisClient = require('../').RedisClient
|
||||
const Command = require('./command')
|
||||
|
||||
const clientProto = RedisClient.prototype
|
||||
const multiProto = Multi.prototype
|
||||
|
||||
// TODO: Rewrite this including the individual commands into a Commands class
|
||||
// that provided a functionality to add new commands to the client
|
||||
commands.list.forEach((command) => {
|
||||
function addCommand (clientProto, multiProto, command) {
|
||||
// Some rare Redis commands use special characters in their command name
|
||||
// Convert those to a underscore to prevent using invalid function names
|
||||
const commandName = command.replace(/(?:^([0-9])|[^a-zA-Z0-9_$])/g, '_$1')
|
||||
|
||||
// Do not override existing functions
|
||||
if (!clientProto[command]) {
|
||||
clientProto[command] = function () {
|
||||
clientProto[commandName] = function () {
|
||||
const len = arguments.length
|
||||
const arr = new Array(len)
|
||||
for (var i = 0; i < len; i += 1) {
|
||||
@@ -25,16 +17,16 @@ commands.list.forEach((command) => {
|
||||
}
|
||||
return this.internalSendCommand(new Command(command, arr))
|
||||
}
|
||||
if (clientProto[command] !== commandName) {
|
||||
Object.defineProperty(clientProto[command], 'name', {
|
||||
if (!clientProto[commandName].name) {
|
||||
Object.defineProperty(clientProto[commandName], 'name', {
|
||||
value: commandName
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Do not override existing functions
|
||||
if (!multiProto[command]) {
|
||||
multiProto[command] = function () {
|
||||
if (!multiProto[command] && command !== 'multi') {
|
||||
multiProto[commandName] = function () {
|
||||
const len = arguments.length
|
||||
const arr = new Array(len)
|
||||
for (var i = 0; i < len; i += 1) {
|
||||
@@ -43,10 +35,12 @@ commands.list.forEach((command) => {
|
||||
this._queue.push(new Command(command, arr))
|
||||
return this
|
||||
}
|
||||
if (multiProto[command] !== commandName) {
|
||||
Object.defineProperty(multiProto[command], 'name', {
|
||||
if (!multiProto[commandName].name) {
|
||||
Object.defineProperty(multiProto[commandName], 'name', {
|
||||
value: commandName
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = addCommand
|
||||
|
Reference in New Issue
Block a user