Last active
January 29, 2016 07:32
-
-
Save xizhibei/4e49b52411bb63a626d2 to your computer and use it in GitHub Desktop.
Utils functions from my projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var _ = require('underscore'); | |
exports.randomString = function (length) { | |
var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghiklmnopqrstuvwxyz'; | |
length = length ? length : 32; | |
var string = ''; | |
for (var i = 0; i < length; i++) { | |
var randomNumber = Math.floor(Math.random() * chars.length); | |
string += chars.substring(randomNumber, randomNumber + 1); | |
} | |
return string; | |
}; | |
/** | |
* | |
* @param from | |
* @param to | |
* @returns {Number} number between 0-1 | |
*/ | |
exports.normallyRandom = function (from, to) { | |
var r = (Math.random() + Math.random() + Math.random() + Math.random() + Math.random() + Math.random()) / 6; | |
return from + (to - from) * r; | |
}; | |
exports.redisScan = function (client, type, params, cb) { | |
var args = [params.key, params.cursor]; | |
if (_.isNumber(params.count)) { | |
args.push('count'); | |
args.push(params.count); | |
} | |
if (params.match) { | |
args.push('match'); | |
args.push(params.match); | |
} | |
args.push(function (err, rst) { | |
if (err) return cb(err); | |
var cursor = rst[0]; | |
var arr = rst[1]; | |
var data = {}; | |
_.times(arr.length / 2, function (i) { | |
data[arr[i * 2]] = arr[i * 2 + 1]; | |
}); | |
cb(null, {cursor: cursor, data: data}); | |
}); | |
client[type].apply(client, args); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment