UNPKG

4.09 kBJavaScriptView Raw
1'use strict';
2
3var commands = require('redis-commands');
4var Multi = require('./multi');
5var RedisClient = require('../').RedisClient;
6var Command = require('./command');
7
8var addCommand = function (command) {
9 // Some rare Redis commands use special characters in their command name
10 // Convert those to a underscore to prevent using invalid function names
11 var commandName = command.replace(/(?:^([0-9])|[^a-zA-Z0-9_$])/g, '_$1');
12
13 // Do not override existing functions
14 if (!RedisClient.prototype[command]) {
15 RedisClient.prototype[command.toUpperCase()] = RedisClient.prototype[command] = function () {
16 var arr;
17 var len = arguments.length;
18 var callback;
19 var i = 0;
20 if (Array.isArray(arguments[0])) {
21 arr = arguments[0];
22 if (len === 2) {
23 callback = arguments[1];
24 }
25 } else if (len > 1 && Array.isArray(arguments[1])) {
26 if (len === 3) {
27 callback = arguments[2];
28 }
29 len = arguments[1].length;
30 arr = new Array(len + 1);
31 arr[0] = arguments[0];
32 for (; i < len; i += 1) {
33 arr[i + 1] = arguments[1][i];
34 }
35 } else {
36 // The later should not be the average use case
37 if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) {
38 len--;
39 callback = arguments[len];
40 }
41 arr = new Array(len);
42 for (; i < len; i += 1) {
43 arr[i] = arguments[i];
44 }
45 }
46 return this.internal_send_command(new Command(command, arr, callback));
47 };
48 // Alias special function names (e.g. NR.RUN becomes NR_RUN and nr_run)
49 if (commandName !== command) {
50 RedisClient.prototype[commandName.toUpperCase()] = RedisClient.prototype[commandName] = RedisClient.prototype[command];
51 }
52 Object.defineProperty(RedisClient.prototype[command], 'name', {
53 value: commandName
54 });
55 }
56
57 // Do not override existing functions
58 if (!Multi.prototype[command]) {
59 Multi.prototype[command.toUpperCase()] = Multi.prototype[command] = function () {
60 var arr;
61 var len = arguments.length;
62 var callback;
63 var i = 0;
64 if (Array.isArray(arguments[0])) {
65 arr = arguments[0];
66 if (len === 2) {
67 callback = arguments[1];
68 }
69 } else if (len > 1 && Array.isArray(arguments[1])) {
70 if (len === 3) {
71 callback = arguments[2];
72 }
73 len = arguments[1].length;
74 arr = new Array(len + 1);
75 arr[0] = arguments[0];
76 for (; i < len; i += 1) {
77 arr[i + 1] = arguments[1][i];
78 }
79 } else {
80 // The later should not be the average use case
81 if (len !== 0 && (typeof arguments[len - 1] === 'function' || typeof arguments[len - 1] === 'undefined')) {
82 len--;
83 callback = arguments[len];
84 }
85 arr = new Array(len);
86 for (; i < len; i += 1) {
87 arr[i] = arguments[i];
88 }
89 }
90 this.queue.push(new Command(command, arr, callback));
91 return this;
92 };
93 // Alias special function names (e.g. NR.RUN becomes NR_RUN and nr_run)
94 if (commandName !== command) {
95 Multi.prototype[commandName.toUpperCase()] = Multi.prototype[commandName] = Multi.prototype[command];
96 }
97 Object.defineProperty(Multi.prototype[command], 'name', {
98 value: commandName
99 });
100 }
101};
102
103commands.list.forEach(addCommand);
104
105module.exports = addCommand;