forked from 3rd-Eden/memcached
/
utils.js
109 lines (86 loc) · 3.02 KB
/
utils.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
"use strict";
var createHash = require('crypto').createHash;
exports.validateArg = function validateArg(args, config){
var toString = Object.prototype.toString
, err
, callback;
args.validate.forEach(function(tokens){
var key = tokens[0]
, value = args[key];
switch(tokens[1]){
case Number:
if (toString.call(value) !== '[object Number]') err = 'Argument "' + key + '" is not a valid Number.';
break;
case Boolean:
if (toString.call(value) !== '[object Boolean]') err = 'Argument "' + key + '" is not a valid Boolean.';
break;
case Array:
if (toString.call(value) !== '[object Array]') err = 'Argument "' + key + '" is not a valid Array.';
break;
case Object:
if (toString.call(value) !== '[object Object]') err = 'Argument "' + key + '" is not a valid Object.';
break;
case Function:
if (toString.call(value) !== '[object Function]') err = 'Argument "' + key + '" is not a valid Function.';
break;
case String:
if (toString.call(value) !== '[object String]') err = 'Argument "' + key + '" is not a valid String.';
if (!err && key === 'key' && value.length > config.maxKeySize){
if (config.keyCompression){
args[key] = createHash('md5').update(value).digest('hex');
// also make sure you update the command
args.command.replace(value, args[key]);
} else {
err = 'Argument "' + key + '" is longer than the maximum allowed length of ' + config.maxKeySize;
}
}
break;
default:
if (toString.call(value) === '[object global]' && !tokens[2]) err = 'Argument "' + key + '" is not defined.';
}
});
if (err){
if(callback) callback(err, false);
return false;
}
return true;
};
// a small util to use an object for eventEmitter
exports.fuse = function fuse (target, handlers) {
for(var i in handlers)
if (handlers.hasOwnProperty(i)){
target.on(i, handlers[i]);
}
};
// merges a object's proppertys / values with a other object
exports.merge = function merge (target, obj) {
for (var i in obj) {
target[i] = obj[i];
}
return target;
};
// curry/bind functions
exports.curry = function curry (context, fn) {
var copy = Array.prototype.slice
, args = copy.call(arguments, 2);
return function bowlofcurry () {
return fn.apply(context || this, args.concat(copy.call(arguments)));
};
};
// a small items iterator
exports.Iterator = function iterator (collection, callback) {
var arr = Array.isArray(collection)
, keys = !arr ? Object.keys(collection) : false
, index = 0
, maximum = arr ? collection.length : keys.length
, self = this;
// returns next item
this.next = function next () {
var obj = arr ? collection[index] : { key: keys[index], value: collection[keys[index]] };
callback(obj, index++, collection, self);
};
// check if we have more items
this.hasNext = function hasNext () {
return index < maximum;
};
};