1 | var Item = require('./item.js');
|
2 | var shuffle = require('./helpers.js').shuffle;
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | exports.sadd = function (mockInstance, key) {
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | if (arguments.length <= 3) {
|
13 | return;
|
14 | }
|
15 |
|
16 | var callback = null;
|
17 | var membersToAdd = [];
|
18 |
|
19 | for (var i = 2; i < arguments.length; i++) {
|
20 |
|
21 | if ('function' !== typeof arguments[i]) {
|
22 | membersToAdd.push(arguments[i]);
|
23 | } else {
|
24 | callback = arguments[i];
|
25 | break;
|
26 | }
|
27 | }
|
28 |
|
29 | if (mockInstance.storage[key] && mockInstance.storage[key].type !== 'set') {
|
30 | var err = new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
31 | return mockInstance._callCallback(callback, err);
|
32 | }
|
33 |
|
34 | mockInstance.storage[key] = mockInstance.storage[key] || Item.createSet();
|
35 |
|
36 | var set = mockInstance.storage[key].value;
|
37 | var addCount = 0;
|
38 | for (var j = 0; j < membersToAdd.length; j++) {
|
39 | if (set.indexOf(Item._stringify(membersToAdd[j])) < 0) {
|
40 | set.push(Item._stringify(membersToAdd[j]));
|
41 | addCount++;
|
42 | }
|
43 | }
|
44 |
|
45 | mockInstance._callCallback(callback, null, addCount);
|
46 | }
|
47 |
|
48 | /**
|
49 | * Srem
|
50 | */
|
51 | exports.srem = function (mockInstance, key) {
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | if (arguments.length <= 3) {
|
57 | return;
|
58 | }
|
59 |
|
60 | var callback = null;
|
61 | var membersToRemove = [];
|
62 |
|
63 | for (var i = 2; i < arguments.length; i++) {
|
64 |
|
65 | if ('function' !== typeof arguments[i]) {
|
66 | membersToRemove.push(arguments[i]);
|
67 | } else {
|
68 | callback = arguments[i];
|
69 | break;
|
70 | }
|
71 | }
|
72 |
|
73 | var remCount = 0;
|
74 |
|
75 | if (mockInstance.storage[key]) {
|
76 | if (mockInstance.storage[key].type !== 'set') {
|
77 | var err = new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
78 | return mockInstance._callCallback(callback, err);
|
79 | }
|
80 |
|
81 | var set = mockInstance.storage[key].value;
|
82 |
|
83 | for (var j = 0; j < membersToRemove.length; j++) {
|
84 | for (var k = 0; k < set.length; k++) {
|
85 | if (set[k] === Item._stringify(membersToRemove[j])) {
|
86 | set.splice(k, 1);
|
87 | remCount++;
|
88 | }
|
89 | }
|
90 | }
|
91 | }
|
92 |
|
93 | mockInstance._callCallback(callback, null, remCount);
|
94 | }
|
95 |
|
96 | /**
|
97 | * Smembers
|
98 | */
|
99 | exports.smembers = function (mockInstance, key, callback) {
|
100 | var members = [];
|
101 |
|
102 | if (mockInstance.storage[key]) {
|
103 | if (mockInstance.storage[key].type !== 'set') {
|
104 | var err = new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
105 | return mockInstance._callCallback(callback, err);
|
106 | } else {
|
107 | members = mockInstance.storage[key].value;
|
108 | }
|
109 | }
|
110 |
|
111 | mockInstance._callCallback(callback, null, members);
|
112 | }
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | exports.sismember = function (mockInstance, key, member, callback) {
|
118 | if (mockInstance.storage[key]) {
|
119 | if (mockInstance.storage[key].type !== 'set') {
|
120 | var err = new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
121 | return mockInstance._callCallback(callback, err);
|
122 | }
|
123 | }
|
124 | member = Item._stringify(member);
|
125 | var count = (mockInstance.storage[key] && (mockInstance.storage[key].value.indexOf(member) > -1)) ? 1 : 0;
|
126 | mockInstance._callCallback(callback, null, count);
|
127 | }
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | exports.scard = function (mockInstance, key, callback) {
|
133 | var count = 0;
|
134 |
|
135 | if (mockInstance.storage[key]) {
|
136 | if (mockInstance.storage[key].type !== 'set') {
|
137 | var err = new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
138 | return mockInstance._callCallback(callback, err);
|
139 | } else {
|
140 | var set = mockInstance.storage[key].value;
|
141 | count = set.length;
|
142 | }
|
143 | }
|
144 |
|
145 | mockInstance._callCallback(callback, null, count);
|
146 | }
|
147 |
|
148 | /**
|
149 | * Sadd
|
150 | */
|
151 | exports.smove = function (mockInstance, source, destination, member, callback) {
|
152 | if (mockInstance.storage[source] && mockInstance.storage[source].type !== 'set') {
|
153 | var err = new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
154 | return mockInstance._callCallback(callback, err);
|
155 | }
|
156 |
|
157 | if (mockInstance.storage[destination] && mockInstance.storage[destination].type !== 'set') {
|
158 | err = new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
159 | return mockInstance._callCallback(callback, err);
|
160 | }
|
161 |
|
162 | mockInstance.storage[source] = mockInstance.storage[source] || Item.createSet();
|
163 | mockInstance.storage[destination] = mockInstance.storage[destination] || Item.createSet();
|
164 |
|
165 | var set = mockInstance.storage[source].value;
|
166 | if (set.indexOf(Item._stringify(member)) < 0) {
|
167 | return mockInstance._callCallback(callback, null, 0);
|
168 | }
|
169 |
|
170 | for (var j = 0; j < set.length; j++) {
|
171 | if (set[j] === Item._stringify(member)) {
|
172 | set.splice(j, 1);
|
173 | }
|
174 | }
|
175 |
|
176 | set = mockInstance.storage[destination].value;
|
177 | if (set.indexOf(Item._stringify(member)) < 0) {
|
178 | set.push(Item._stringify(member));
|
179 | }
|
180 |
|
181 | mockInstance._callCallback(callback, null, 1);
|
182 | }
|
183 |
|
184 | /**
|
185 | * Srandmember
|
186 | */
|
187 | exports.srandmember = function (mockInstance, key, count, callback) {
|
188 | if (typeof count === 'function') {
|
189 | callback = count;
|
190 | count = null;
|
191 | }
|
192 |
|
193 | if (mockInstance.storage[key] && mockInstance.storage[key].type !== 'set') {
|
194 | var err = new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
|
195 | return mockInstance._callCallback(callback, err);
|
196 | }
|
197 |
|
198 | if (count !== null && (typeof count !== 'number' || count < 0)) {
|
199 | err = new Error('ERR value is not an integer or out of range');
|
200 | return mockInstance._callCallback(callback, err);
|
201 | }
|
202 |
|
203 | if (!mockInstance.storage[key]) {
|
204 | if (count === null) {
|
205 | return mockInstance._callCallback(callback, null, null);
|
206 | } else {
|
207 | return mockInstance._callCallback(callback, null, []);
|
208 | }
|
209 | }
|
210 |
|
211 | var members = mockInstance.storage[key].value;
|
212 | var result;
|
213 |
|
214 | if (count !== null) {
|
215 | var shuffled = shuffle(members);
|
216 | result = shuffled.slice(0, count);
|
217 | } else {
|
218 | result = members[Math.floor(Math.random() * members.length)];
|
219 | }
|
220 |
|
221 | mockInstance._callCallback(callback, null, result);
|
222 | }
|