/
mock.js
83 lines (83 loc) · 1.91 KB
/
mock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(function() {
var Mock, dec, enc, normalize, startswith;
var __hasProp = Object.prototype.hasOwnProperty;
normalize = function(k) {
if (k instanceof Buffer) {
return k;
} else {
return new Buffer(k);
}
};
enc = function(k) {
return k.toString('base64');
};
dec = function(b64) {
return new Buffer(b64, 'base64');
};
startswith = function(buf1, buf2) {
var i, _ref;
if (buf2.length > buf1.length) {
return false;
}
for (i = 0, _ref = buf2.length; 0 <= _ref ? i < _ref : i > _ref; 0 <= _ref ? i++ : i--) {
if (buf1[i] !== buf2[i]) {
return false;
}
}
return true;
};
Mock = (function() {
function Mock() {
this.items = {};
}
Mock.prototype.get = function(k, cb) {
var v;
if (cb == null) {
cb = function() {};
}
k = normalize(k);
v = this.items[enc(k)] || null;
return cb(null, v);
};
Mock.prototype.set = function(k, v, cb) {
if (cb == null) {
cb = function() {};
}
k = normalize(k);
v = normalize(v);
this.items[enc(k)] = v;
return cb(null);
};
Mock.prototype.getRange = function(_arg, cb) {
var items, k, k64, k__prefix, max, v, _ref;
k__prefix = _arg.k__prefix, max = _arg.max;
if (cb == null) {
cb = function() {};
}
if (k__prefix) {
k__prefix = normalize(k__prefix);
}
items = [];
_ref = this.items;
for (k64 in _ref) {
if (!__hasProp.call(_ref, k64)) continue;
v = _ref[k64];
if (max && items.length >= max) {
break;
}
k = dec(k64);
if (k__prefix && !startswith(k, k__prefix)) {
continue;
}
items.push([k, v]);
}
return cb(null, items);
};
return Mock;
})();
module.exports = {
Mock: Mock,
MockKV: Mock,
MockKVR: Mock
};
}).call(this);