-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
110 lines (100 loc) · 2.64 KB
/
index.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
var commands = require('./lua');
var asCallback = require('standard-as-callback');
function RedisTree(redis) {
commands.forEach(function (command) {
redis.defineCommand(command.name, {
numberOfKeys: 1,
lua: command.lua
});
});
// Setup transformers
(function (tchildren) {
redis.tchildren = function (key, node, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
var argv = [key, node];
if (options && options.level != null) {
argv.push('LEVEL', options.level);
}
return asCallback(tchildren.apply(redis, argv).then(function (res) {
if (!Array.isArray(res)) {
return res;
}
return res.map(convertNode);
}), callback);
};
})(redis.tchildren);
(function (tancestors) {
redis.tancestors = function (key, node, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
var argv = [key, node];
if (options && options.level != null) {
argv.push('LEVEL', options.level);
}
return asCallback(
tancestors.apply(redis, argv),
callback
);
};
})(redis.tancestors);
(function (tinsert) {
redis.tinsert = function (key, parent, node, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
var argv = [key, parent, node];
options = options || {};
if (options.index != null) {
argv.push('INDEX', options.index);
} else if (options.before != null) {
argv.push('BEFORE', options.before);
} else if (options.after != null) {
argv.push('AFTER', options.after);
} else {
argv.push('INDEX', -1);
}
return asCallback(
tinsert.apply(redis, argv),
callback
);
};
})(redis.tinsert);
(function (tmrem) {
redis.tmrem = function (key, node, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
var argv = [key, node];
options = options || {};
if (options.not != null) {
argv.push('NOT', options.not);
}
return asCallback(
tmrem.apply(redis, argv),
callback
);
};
})(redis.tmrem);
return redis;
}
function convertNode(node) {
var ret = {
node: node[0],
hasChild: !!node[1]
};
if (node.length > 2) {
ret.children = [];
for (var i = 2; i < node.length; i++) {
ret.children.push(convertNode(node[i]));
}
}
return ret;
};
module.exports = RedisTree;