utility
A collection of useful utilities.
Install
$ npm install utility
Usage
const utils = require('utility');
md5
utils.md5('苏千').should.equal('5f733c47c58a077d61257102b2d44481');
utils.md5(new Buffer('苏千')).should.equal('5f733c47c58a077d61257102b2d44481');
utils.md5('苏千', 'base64');
utils.md5({foo: 'bar', bar: 'foo'}).should.equal(utils.md5({bar: 'foo', foo: 'bar'}));
sha1
utils.sha1('苏千').should.equal('0a4aff6bab634b9c2f99b71f25e976921fcde5a5');
utils.sha1(new Buffer('苏千')).should.equal('0a4aff6bab634b9c2f99b71f25e976921fcde5a5');
utils.sha1('苏千', 'base64');
utils.sha1({foo: 'bar', bar: 'foo'}).should.equal(utils.sha1({bar: 'foo', foo: 'bar'}));
sha256
utils.sha256(new Buffer('苏千')).should.equal('75dd03e3fcdbba7d5bec07900bae740cc8e361d77e7df8949de421d3df5d3635');
hmac
utils.hmac('sha1', 'I am a key', 'hello world');
decode and encode
utils.base64encode('你好¥');
utils.base64decode('5L2g5aW977+l')
utils.base64encode('你好¥', true);
utils.base64decode('5L2g5aW977-l', true);
utils.escape('<script/>"& &');
utils.decodeURIComponent(utils.encodeURIComponent('你好, nodejs')).should.equal('你好, nodejs');
others
[WARNNING] getIP() remove, PLEASE use https://github.com/node-modules/address
module instead.
utils.getParamNames(function (key1, key2) {});
utils.randomString(32, '1234567890');
utils.has({hello: 'world'}, 'hello');
utils.noop = function () {}
}
Date utils
utils.accessLogDate();
utils.logDate();
utils.YYYYMMDDHHmmssSSS();
utils.YYYYMMDDHHmmssSSS(',');
utils.YYYYMMDDHHmmss();
utils.YYYYMMDDHHmmss(new Date(), {dateSep: '.'});
utils.YYYYMMDD();
utils.YYYYMMDD('');
utils.YYYYMMDD(',');
utils.datestruct();
utils.timestamp();
utils.timestamp(1385091596);
utils.timestamp(1385091596000);
Number utils
utils.isSafeNumberString('9007199254740991');
utils.isSafeNumberString('9007199254740993');
utils.toSafeNumber('9007199254740991');
utils.toSafeNumber('9007199254740993');
utils.random(100);
utils.random(2, 1000);
utils.random();
Timers
utils.setImmediate(function () {
console.log('hi');
});
map
Create a real
map in javascript.
use Object.create(null)
const map = utils.map({a: 1});
console.log(map);
String utils
utils.split('foo,bar,,,', ',');
utils.replace('<body> hi', '<body>', '$& body');
utils.replaceInvalidHttpHeaderChar('abc你好11');
Try
const res = utils.try(function () {
return JSON.parse(str);
});
argumentsToArray
function() {
const arr = utility.argumentsToArray(arguments);
console.log(arr.join(', '));
}
JSON
const obj = utils.strictJSONparse('"hello"');
Object.assign
utility.assign({}, { a: 1 });
utility.assign({}, [ { a: 1 }, { b: 1 } ]);
benchmark
$ node benchmark/date_format.js
moment().format("DD/MMM/YYYY:HH:mm:ss ZZ"): "16/Apr/2013:21:12:32 +0800"
utils.accessLogDate(): "16/Apr/2013:21:12:32 +0800"
fasterAccessDate(): "16/Apr/2013:21:12:32 +0800"
fasterAccessDate2(): "16/Apr/2013:21:12:32 +0800"
new Date().toString(): "Tue Apr 16 2013 21:12:32 GMT+0800 (CST)"
Date(): "Tue Apr 16 2013 21:12:32 GMT+0800 (CST)"
Date.now(): 1366117952162
------------------------
moment().format('DD/MMM/YYYY:HH:mm:ss ZZ') x 68,300 ops/sec ±5.05% (91 runs sampled)
utils.accessLogDate() x 1,341,341 ops/sec ±2.72% (90 runs sampled)
fasterAccessDate() x 357,833 ops/sec ±1.32% (98 runs sampled)
fasterAccessDate2() x 301,607 ops/sec ±5.03% (83 runs sampled)
new Date().toString() x 738,499 ops/sec ±3.54% (86 runs sampled)
Date() x 794,724 ops/sec ±2.77% (95 runs sampled)
Date.now() x 8,327,685 ops/sec ±1.85% (94 runs sampled)
Fastest is Date.now()
benchmark/date_YYYYMMDD.js
$ node benchmark/date_YYYYMMDD.js
parseInt(moment().format("YYYYMMDD"), 10): 20130416
utils.datestruct().YYYYMMDD: 20130416
new Date().toString(): "Tue Apr 16 2013 21:12:02 GMT+0800 (CST)"
------------------------
parseInt(moment().format('YYYYMMDD'), 10) x 129,604 ops/sec ±0.46% (101 runs sampled)
utils.datestruct().YYYYMMDD x 2,317,461 ops/sec ±1.38% (95 runs sampled)
new Date().toString() x 816,731 ops/sec ±3.46% (93 runs sampled)
Fastest is utils.datestruct().YYYYMMDD
License
MIT