1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-09 00:22:08 +03:00
Files
node-redis/lib/commands.js
Ruben Bridgewater f1a7bcd735 chore: use standard
2017-05-06 07:06:52 +02:00

100 lines
3.0 KiB
JavaScript

'use strict'
var commands = require('redis-commands')
var Multi = require('./multi')
var RedisClient = require('../').RedisClient
var Command = require('./command')
// 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(function (command) {
// Some rare Redis commands use special characters in their command name
// Convert those to a underscore to prevent using invalid function names
var commandName = command.replace(/(?:^([0-9])|[^a-zA-Z0-9_$])/g, '_$1')
// Do not override existing functions
if (!RedisClient.prototype[command]) {
RedisClient.prototype[command] = function () {
var arr
var len = arguments.length
var callback
var i = 0
if (Array.isArray(arguments[0])) {
arr = arguments[0]
if (len === 2) {
callback = arguments[1]
}
} else if (len > 1 && Array.isArray(arguments[1])) {
if (len === 3) {
callback = arguments[2]
}
len = arguments[1].length
arr = new Array(len + 1)
arr[0] = arguments[0]
for (; i < len; i += 1) {
arr[i + 1] = arguments[1][i]
}
} else {
// The later should not be the average use case
if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) {
len--
callback = arguments[len]
}
arr = new Array(len)
for (; i < len; i += 1) {
arr[i] = arguments[i]
}
}
return this.internalSendCommand(new Command(command, arr, callback))
}
if (RedisClient.prototype[command] !== commandName) {
Object.defineProperty(RedisClient.prototype[command], 'name', {
value: commandName
})
}
}
// Do not override existing functions
if (!Multi.prototype[command]) {
Multi.prototype[command] = function () {
var arr
var len = arguments.length
var callback
var i = 0
if (Array.isArray(arguments[0])) {
arr = arguments[0]
if (len === 2) {
callback = arguments[1]
}
} else if (len > 1 && Array.isArray(arguments[1])) {
if (len === 3) {
callback = arguments[2]
}
len = arguments[1].length
arr = new Array(len + 1)
arr[0] = arguments[0]
for (; i < len; i += 1) {
arr[i + 1] = arguments[1][i]
}
} else {
// The later should not be the average use case
if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) {
len--
callback = arguments[len]
}
arr = new Array(len)
for (; i < len; i += 1) {
arr[i] = arguments[i]
}
}
this.queue.push(new Command(command, arr, callback))
return this
}
if (Multi.prototype[command] !== commandName) {
Object.defineProperty(Multi.prototype[command], 'name', {
value: commandName
})
}
}
})