Comparing version 1.1.0 to 1.2.0
@@ -15,2 +15,3 @@ /*! | ||
var Benchmark = require('benchmark'); | ||
var benchmarks = require('beautify-benchmark'); | ||
var suite = new Benchmark.Suite(); | ||
@@ -50,10 +51,37 @@ | ||
}) | ||
.add("utils.getParamNames(newfn)", function () { | ||
function newfn(foo, bar) {} | ||
utils.getParamNames(newfn); | ||
}) | ||
.add("utils.getParamNames(newfn, false) no cache", function () { | ||
function newfn(foo, bar) {} | ||
utils.getParamNames(newfn, false); | ||
}) | ||
// add listeners | ||
.on('cycle', function (event) { | ||
console.log(String(event.target)); | ||
.on('cycle', function(event) { | ||
benchmarks.add(event.target); | ||
}) | ||
.on('complete', function () { | ||
console.log('Fastest is ' + this.filter('fastest').pluck('name')); | ||
.on('start', function(event) { | ||
console.log('\n get param names Benchmark\n node version: %s, date: %s\n Starting...', | ||
process.version, Date()); | ||
}) | ||
.run({ async: false }); | ||
.on('complete', function done() { | ||
benchmarks.log(); | ||
}) | ||
.run({ 'async': false }); | ||
// $ node benchmark/get_paramnames.js | ||
// | ||
// cache: [ 'cid', 'startDate', 'endDate', 'rate', 'callback' ] | ||
// no cache: [ 'cid', 'startDate', 'endDate', 'rate', 'callback' ] | ||
// ------------------------ | ||
// | ||
// get param names Benchmark | ||
// node version: v0.11.12, date: Sat Aug 23 2014 22:45:38 GMT+0800 (CST) | ||
// Starting... | ||
// 4 tests completed. | ||
// | ||
// utils.getParamNames(foo) x 44,782,022 ops/sec ±1.63% (100 runs sampled) | ||
// utils.getParamNames(foo, false) no cache x 1,108,420 ops/sec ±0.96% (97 runs sampled) | ||
// utils.getParamNames(newfn) x 1,332,794 ops/sec ±1.08% (98 runs sampled) | ||
// utils.getParamNames(newfn, false) no cache x 1,366,816 ops/sec ±0.89% (98 runs sampled) |
1.2.0 / 2014-09-14 | ||
================== | ||
* add utility.try | ||
1.1.0 / 2014-08-23 | ||
@@ -3,0 +8,0 @@ ================== |
@@ -22,3 +22,3 @@ /*! | ||
*/ | ||
exports.noop = function () {}; | ||
exports.noop = function noop() {}; | ||
@@ -66,3 +66,3 @@ function sortObject(o) { | ||
*/ | ||
exports.md5 = function (s, format) { | ||
exports.md5 = function md5(s, format) { | ||
return exports.hash('md5', s, format); | ||
@@ -79,3 +79,3 @@ }; | ||
*/ | ||
exports.sha1 = function (s, format) { | ||
exports.sha1 = function sha1(s, format) { | ||
return exports.hash('sha1', s, format); | ||
@@ -101,3 +101,3 @@ }; | ||
*/ | ||
exports.hmac = function (algorithm, key, data, encoding) { | ||
exports.hmac = function hmac(algorithm, key, data, encoding) { | ||
encoding = encoding || 'base64'; | ||
@@ -117,3 +117,3 @@ var hmac = crypto.createHmac(algorithm, key); | ||
*/ | ||
exports.base64encode = function (s, urlsafe) { | ||
exports.base64encode = function base64encode(s, urlsafe) { | ||
if (!Buffer.isBuffer(s)) { | ||
@@ -137,3 +137,3 @@ s = new Buffer(s); | ||
*/ | ||
exports.base64decode = function (encode, urlsafe) { | ||
exports.base64decode = function base64decode(encode, urlsafe) { | ||
if (urlsafe) { | ||
@@ -153,3 +153,3 @@ encode = encode.replace(/\-/g, '+').replace(/_/g, '/'); | ||
*/ | ||
exports.escape = function (html) { | ||
exports.escape = function escape(html) { | ||
return String(html) | ||
@@ -168,3 +168,3 @@ .replace(/&(?!\w+;)/g, '&') | ||
*/ | ||
exports.randomSlice = function (arr, num) { | ||
exports.randomSlice = function randomSlice(arr, num) { | ||
if (!num || num >= arr.length) { | ||
@@ -191,3 +191,3 @@ return arr.slice(); | ||
*/ | ||
exports.encodeURIComponent = function (text) { | ||
exports.encodeURIComponent = function encodeURIComponent_(text) { | ||
try { | ||
@@ -207,3 +207,3 @@ return encodeURIComponent(text); | ||
*/ | ||
exports.decodeURIComponent = function (encodeText) { | ||
exports.decodeURIComponent = function decodeURIComponent_(encodeText) { | ||
try { | ||
@@ -397,3 +397,3 @@ return decodeURIComponent(encodeText); | ||
*/ | ||
exports.getParamNames = function (func, cache) { | ||
exports.getParamNames = function getParamNames(func, cache) { | ||
cache = cache !== false; | ||
@@ -422,7 +422,8 @@ if (cache && func.__cache_names) { | ||
*/ | ||
exports.isSafeNumberString = function (s) { | ||
exports.isSafeNumberString = function isSafeNumberString(s) { | ||
if (s[0] === '-') { | ||
s = s.substring(1); | ||
} | ||
if (s.length < MAX_SAFE_INTEGER_STR_LENGTH || (s.length === MAX_SAFE_INTEGER_STR_LENGTH && s <= MAX_SAFE_INTEGER_STR)) { | ||
if (s.length < MAX_SAFE_INTEGER_STR_LENGTH || | ||
(s.length === MAX_SAFE_INTEGER_STR_LENGTH && s <= MAX_SAFE_INTEGER_STR)) { | ||
return true; | ||
@@ -439,3 +440,3 @@ } | ||
*/ | ||
exports.toSafeNumber = function (s) { | ||
exports.toSafeNumber = function toSafeNumber(s) { | ||
if (typeof s === 'number') { | ||
@@ -452,3 +453,3 @@ return s; | ||
*/ | ||
exports.timestamp = function (t) { | ||
exports.timestamp = function timestamp(t) { | ||
if (t) { | ||
@@ -473,3 +474,3 @@ var v = t; | ||
exports.randomString = function (length, charSet) { | ||
exports.randomString = function randomString(length, charSet) { | ||
var result = []; | ||
@@ -485,3 +486,3 @@ length = length || 16; | ||
exports.has = function (obj, prop) { | ||
exports.has = function has(obj, prop) { | ||
return Object.prototype.hasOwnProperty.call(obj, prop); | ||
@@ -495,3 +496,3 @@ }; | ||
*/ | ||
exports.map = function (obj) { | ||
exports.map = function map(obj) { | ||
var map = Object.create(null); | ||
@@ -548,1 +549,25 @@ if (!obj) { | ||
}; | ||
/** | ||
* optimize try catch | ||
* @param {Function} fn | ||
* @return {Object} | ||
* - {Error} error | ||
* - {Mix} value | ||
*/ | ||
exports.try = function (fn) { | ||
var res = { | ||
error: undefined, | ||
value: undefined | ||
}; | ||
try { | ||
res.value = fn(); | ||
} catch (err) { | ||
res.error = err instanceof Error | ||
? err | ||
: new Error(err); | ||
} | ||
return res; | ||
}; |
{ | ||
"name": "utility", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A collection of useful utilities.", | ||
@@ -29,3 +29,3 @@ "main": "index.js", | ||
"moment": "~2.8.2", | ||
"optimized": "~1.1.0", | ||
"optimized": "~1.2.0", | ||
"should": "~4.0.4" | ||
@@ -32,0 +32,0 @@ }, |
@@ -173,2 +173,13 @@ utility | ||
### Try | ||
```js | ||
var res = utils.try(function () { | ||
return JSON.parse(str); | ||
}); | ||
// {error: undefined, value: {foo: 'bar'}} | ||
// {error: Error, value: undefined} | ||
``` | ||
## benchmark | ||
@@ -175,0 +186,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44464
16
1000
250