UNPKG

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