Also you can use it within typescript, like this ↓
import * as utils from 'utility';
md5
import { md5 } from 'utility';md5('苏千');// '5f733c47c58a077d61257102b2d44481'md5(Buffer.from('苏千'));// '5f733c47c58a077d61257102b2d44481'// md5 base64 formatmd5('苏千', 'base64'); // 'X3M8R8WKB31hJXECstREgQ=='// Object md5 hash. Sorted by key, and JSON.stringify. See source code for detailmd5({foo: 'bar', bar: 'foo'}).should.equal(md5({bar: 'foo', foo: 'bar'}));
sha1
import { sha1 } from 'utility';sha1('苏千');// '0a4aff6bab634b9c2f99b71f25e976921fcde5a5'sha1(Buffer.from('苏千'));// '0a4aff6bab634b9c2f99b71f25e976921fcde5a5'// sha1 base64 formatsha1('苏千', 'base64');// 'Ckr/a6tjS5wvmbcfJel2kh/N5aU='// Object sha1 hash. Sorted by key, and JSON.stringify. See source code for detailsha1({foo: 'bar', bar: 'foo'}).should.equal(sha1({bar: 'foo', foo: 'bar'}));
sha256
import { sha256 } from 'utility';sha256(Buffer.from('苏千'));// '75dd03e3fcdbba7d5bec07900bae740cc8e361d77e7df8949de421d3df5d3635'
hmac
import { hmac } from 'utility';// hmac-sha1 with base64 output encodinghmac('sha1', 'I am a key', 'hello world');// 'pO6J0LKDxRRkvSECSEdxwKx84L0='
decode and encode
import { base64encode, base64decode, escape, unescape, encodeURIComponent, decodeURIComponent } from 'utility';// base64 encodebase64encode('你好¥');// '5L2g5aW977+l'base64decode('5L2g5aW977+l');// '你好¥'// urlsafe base64 encodebase64encode('你好¥', true);// '5L2g5aW977-l'base64decode('5L2g5aW977-l', true);// '你好¥'// html escape and unescapeescape('<script/>"& &');// '<script/>"& &amp;'unescape('<script/>"& &amp;');// '<script/>"& &'// Safe encodeURIComponent and decodeURIComponent
others
[WARNNING] getIP() remove, PLEASE use https://github.com/node-modules/address module instead.
// get a function parameter's namesutils.getParamNames(function (key1, key2) {}); // ['key1', 'key2']// get a random string, default length is 16.utils.randomString(32, '1234567890'); //18774480824014856763726145106142// check if object has this propertyutils.has({hello: 'world'}, 'hello'); //true// empty functionutils.noop = function () {}// throw out an assertion error if you were given an invalid "func"try { utils.getParamNames(null); // Only function is allowed} catch (err) { console.error(err); // Assertion Error}
Date utils
// accessLogDateutils.accessLogDate(); // '16/Apr/2013:16:40:09 +0800'// logDate,// 'YYYY-MM-DD HH:mm:ss.SSS' format date stringutils.logDate(); // '2013-04-17 14:43:02.674'utils.YYYYMMDDHHmmssSSS(); // '2013-04-17 14:43:02.674'utils.YYYYMMDDHHmmssSSS(','); // '2013-04-17 14:43:02,674'// 'YYYY-MM-DD HH:mm:ss' format date stringutils.YYYYMMDDHHmmss(); // '2013-04-17 14:43:02'utils.YYYYMMDDHHmmss(new Date(), {dateSep: '.'}); // '2013.04.17 14:43:02'// 'YYYY-MM-DD' format date stringutils.YYYYMMDD(); // '2013-04-17'utils.YYYYMMDD(''); // '20130417'utils.YYYYMMDD
Number utils
// Detect a number string can safe convert to Javascript Number.: `-9007199254740991 ~ 9007199254740991`utils.isSafeNumberString('9007199254740991'); // trueutils.isSafeNumberString('9007199254740993'); // false// Convert string to number safe:utils.toSafeNumber('9007199254740991'); // 9007199254740991utils.toSafeNumber('9007199254740993'); // '9007199254740993'// Produces a random integer between the inclusive `lower` and exclusive `upper` bounds.utils.random(100); // [0, 100)utils.random(2, 1000); // [2, 1000)utils.random(); // 0
Timeout
runWithTimeout(scope, timeout)
Executes a scope promise with a specified timeout duration. If the promise doesn't resolve within the timeout period, it will reject with a TimeoutError.
import { runWithTimeout } from 'utility';await runWithTimeout(async () => { // long run operation here}, 1000);
map
Create a real map in javascript.
use Object.create(null)
const map = utils.map({a: 1});// should.not.exist(map.constructor);// should.not.exist(map.__proto__);// should.not.exist(map.toString);// should not exist any propertyconsole.log(map); // {a: 1}
String utils
// split string by seputils.split('foo,bar,,,', ','); // ['foo', 'bar']// replace string work with special chars which `String.prototype.replace` can't handleutils.replace('<body> hi', '<body>', '$& body'); // '$& body hi'// replace http header invalid charactersutils.replaceInvalidHttpHeaderChar('abc你好11'); // {invalid: true, val: 'abc 11'}