nesh
Version:
An enhanced, extensible shell for Node.js
113 lines (96 loc) • 2.63 kB
JavaScript
// Generated by CoffeeScript 1.11.1
/*
Builtin Utilities Plugin
*/
(function() {
var _, crypto, querystring;
_ = require('underscore');
crypto = require('crypto');
querystring = require('querystring');
exports.name = 'builtins';
exports.description = 'Exposes built-in convenience methods';
exports.postStart = function(context) {
var cmd, repl;
repl = context.repl;
/*
Underscore utilities, see:
http://documentcloud.github.io/underscore
*/
repl.context.__ = _;
/*
Hashing functions
*/
repl.context.md5 = function(value) {
return crypto.createHash('md5').update(value).digest('hex');
};
repl.context.sha = function(value) {
return crypto.createHash('sha1').update(value).digest('hex');
};
/*
Random functions
*/
repl.context.rand = function(start, end) {
if (start == null) {
start = 0;
end = 1;
} else if (end == null) {
end = start;
start = 0;
}
return Math.random() * (end - start) + start;
};
repl.context.randInt = function(start, end) {
return Math.round(repl.context.rand(start, end));
};
repl.context.randChoices = function(choices, length) {
var result;
if (length == null) {
length = 1;
}
result = [];
while (--length >= 0) {
result.push(choices[Math.floor(Math.random() * choices.length)]);
}
return result;
};
repl.context.randString = function(length, charSet) {
if (charSet == null) {
charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
}
return repl.context.randChoices(charSet.split(''), length).join('');
};
repl.context.randHex = function(length) {
return repl.context.randString(length, 'abcdef0123456789');
};
/*
Number representation shortcuts
*/
repl.context.bin = function(val) {
return val.toString(2);
};
repl.context.oct = function(val) {
return val.toString(8);
};
repl.context.hex = function(val) {
return val.toString(16);
};
/*
URL encoding / decoding
*/
repl.context.querystring = querystring;
repl.context.urlenc = querystring.escape;
repl.context.urldec = querystring.unescape;
/*
REPL Commands
*/
cmd = {
help: 'Clear the screen',
action: function() {
repl.outputStream.write('\u001B[2J\u001B[0;0f');
return repl.displayPrompt();
}
};
return repl.defineCommand('cls', cmd);
};
}).call(this);
//# sourceMappingURL=builtins.js.map