doublescore
Advanced tools
Comparing version 0.3.1 to 0.3.2
@@ -14,2 +14,5 @@ 'use strict'; | ||
}, | ||
close: function() { | ||
return Close.close( obj ); | ||
}, | ||
getType: function() { | ||
@@ -16,0 +19,0 @@ return Types.getType( obj ); |
@@ -6,11 +6,12 @@ 'use strict'; | ||
var Close = module.exports = { | ||
close: function( params ) { | ||
close: function ( params ) { | ||
params = Mixin.mixin( { | ||
max: 1, | ||
max: 1, | ||
maxException: false, | ||
ttl: 30000 | ||
maxLog: false, | ||
ttl: 30000 | ||
}, params ); | ||
return function( cb ) { | ||
return function ( cb ) { | ||
@@ -21,3 +22,3 @@ var calls = 0; | ||
if ( params.ttl && params.ttl > 0 ) { | ||
timeout = setTimeout( function() { | ||
timeout = setTimeout( function () { | ||
calls++; | ||
@@ -29,3 +30,3 @@ timeout = null; | ||
return function( err, data ) { | ||
return function ( err, data ) { | ||
@@ -36,7 +37,11 @@ calls++; | ||
if ( params.maxException ) { | ||
throw new Error( 'max callbacks ' + params.max ); | ||
err = new Error( 'max callbacks ' + params.max ); | ||
if ( params.maxLog ) { | ||
console.error( err.stack); | ||
} else if ( params.maxException ) { | ||
throw err; | ||
} | ||
return false; | ||
return; | ||
} | ||
@@ -43,0 +48,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"author": "Anthony Hildoer <anthony@bluerival.com>", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
@@ -8,2 +8,20 @@ doublescore | ||
```javascript | ||
var __ = require( 'doublescore' ); | ||
close = __( { | ||
timeout: 30000 | ||
} ).close() | ||
function do( params, done ) { | ||
// ensure done is called within 30 seconds, multiple calls ignored | ||
done = close( done ); | ||
someIoCall( params, done ); | ||
} | ||
``` | ||
Returns a function used to generate callbacks with a service level of max calls and minimum TTL until callback errors | ||
@@ -16,3 +34,17 @@ | ||
```javascript | ||
var __ = require( 'doublescore' ); | ||
__.isObject( new Date() ); // true | ||
__.isObject( {} ); // true | ||
__.isObject( null ); // false | ||
__.isObject( "hello" ); // false | ||
__.isObject( Infinity ); // false | ||
__.isObject( 5 ); // false | ||
__.isObject( true ); // false | ||
``` | ||
getType() | ||
@@ -30,19 +62,4 @@ | ||
Will return a clone of the original object, with any passed in objects merged in recursively up to 100 levels max. mixin() accepts an arbitrary number of arguments, they will be mixed in from left to right. | ||
Applies clone() to params and then recursively mixes all values into default. If types do not match, params type is taken. mixin() accepts an arbitrary number of arguments, they will be mixed in from left to right. | ||
timer() | ||
Will return a function that will track time deltas with 1ms resolution and < 0.1% error on average. The returned function accepts a single parameter: reset, which if true will reset the timer, returning the last interval | ||
NOTE: timer() is experimental. 0.5% margin of error for both precision and accuracy at best, probably closer to 1% for most intervals of time. However, deviance is constant regardless of actual time interval measured. Thus, error rates for higher intervals (> 100ms) will be considerably lower than those for short intervals (< 50ms). | ||
Usage | ||
==================== | ||
The usage for doublescore is patterned after other utility libraries like underscore. | ||
```javascript | ||
@@ -55,2 +72,3 @@ | ||
// sets defaults recursively | ||
params = __( { | ||
@@ -77,10 +95,20 @@ deep: [ | ||
} | ||
``` | ||
timer() | ||
``` | ||
Will return a function that will track time deltas with 1ms resolution and < 0.1% error on average. The returned function accepts a single parameter: reset, which if true will reset the timer, returning the last interval | ||
NOTE: timer() is experimental. 0.5% margin of error for both precision and accuracy at best, probably closer to 1% for most intervals of time. However, deviance is constant regardless of actual time interval measured. Thus, error rates for higher intervals (> 100ms) will be considerably lower than those for short intervals (< 50ms). | ||
Documentation | ||
Usage | ||
==================== | ||
The usage for doublescore is patterned after other utility libraries like underscore. | ||
More examples | ||
============= | ||
Please see the test cases in test/default.js for extensive examples of all supported use cases. | ||
@@ -94,3 +122,3 @@ | ||
Copyright (c) 2016 BlueRival Software <anthony@bluerival.com> | ||
Copyright (c) 2016 BlueRival Software <support@bluerival.com> | ||
@@ -97,0 +125,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation |
@@ -6,5 +6,5 @@ 'use strict'; | ||
describe( 'close', function() { | ||
describe( 'close', function () { | ||
it( 'should return the correct number of arguments', function() { | ||
it( 'should return the correct number of arguments', function () { | ||
@@ -14,3 +14,3 @@ var c = __.close( { max: 100 } ); | ||
var args = null; | ||
var cb = c( function() { | ||
var cb = c( function () { | ||
args = arguments.length; | ||
@@ -51,3 +51,3 @@ } ); | ||
it( 'should return data once', function() { | ||
it( 'should return data once', function () { | ||
@@ -59,3 +59,3 @@ var c = __.close(); | ||
var cb = c( function( err, data ) { | ||
var cb = c( function ( err, data ) { | ||
lastData = data; | ||
@@ -73,4 +73,40 @@ calls++; | ||
it( 'should return data twice', function() { | ||
it( 'should return data once and log error', function () { | ||
var tempConsole = console.error; | ||
var consoleMessage = null; | ||
console.error = function ( message ) { | ||
consoleMessage = message; | ||
}; | ||
var c = __.close( { maxLog: true } ); | ||
var calls = 0; | ||
var lastData = null; | ||
var cb = c( function ( err, data ) { | ||
lastData = data; | ||
calls++; | ||
} ); | ||
var expectedMessage = "Error: max callbacks 1\n at /Users/ahildoer/src/BlueRival/doublescore/lib/close.js:34:12\n at Context.<anonymous> (/Users/ahildoer/src/BlueRival/doublescore/test/close.js:#:3)\n at callFn (/Users/ahildoer/src/BlueRival/doublescore/node_modules/mocha/lib/runnable.js:286:21)\n at Test.Runnable.run (/Users/ahildoer/src/BlueRival/doublescore/node_modules/mocha/lib/runnable.js:279:7)\n at Runner.runTest (/Users/ahildoer/src/BlueRival/doublescore/node_modules/mocha/lib/runner.js:421:10)\n at /Users/ahildoer/src/BlueRival/doublescore/node_modules/mocha/lib/runner.js:528:12\n at next (/Users/ahildoer/src/BlueRival/doublescore/node_modules/mocha/lib/runner.js:341:14)\n at /Users/ahildoer/src/BlueRival/doublescore/node_modules/mocha/lib/runner.js:351:7\n at next (/Users/ahildoer/src/BlueRival/doublescore/node_modules/mocha/lib/runner.js:283:14)\n at Immediate._onImmediate (/Users/ahildoer/src/BlueRival/doublescore/node_modules/mocha/lib/runner.js:319:5)\n at processImmediate [as _immediateCallback] (timers.js:383:17)"; | ||
cb( null, 200 ); | ||
cb( null, 'hi' ); | ||
console.error = tempConsole; | ||
// account for editing of this file and line numbers changing in stack trace | ||
if ( typeof consoleMessage === 'string' ) { | ||
consoleMessage = consoleMessage.replace( /\/test\/close.js:[0-9]+:3/, '/test/close.js:#:3' ); | ||
} | ||
assert.strictEqual( lastData, 200 ); | ||
assert.strictEqual( calls, 1 ); | ||
assert.strictEqual( consoleMessage, expectedMessage ); | ||
} ); | ||
it( 'should return data twice', function () { | ||
var c = __.close( { | ||
@@ -83,3 +119,3 @@ max: 2 | ||
var cb = c( function( err, datum ) { | ||
var cb = c( function ( err, datum ) { | ||
data.push( datum ); | ||
@@ -98,3 +134,3 @@ calls++; | ||
it( 'should timeout error', function( done ) { | ||
it( 'should timeout error', function ( done ) { | ||
@@ -105,3 +141,3 @@ var c = __.close( { | ||
var cb = c( function( err, data ) { | ||
var cb = c( function ( err, data ) { | ||
try { | ||
@@ -118,3 +154,3 @@ | ||
setTimeout( function() { | ||
setTimeout( function () { | ||
cb( null, true ); | ||
@@ -125,7 +161,7 @@ }, 100 ); | ||
it( 'should throw exception on extra callbacks', function() { | ||
it( 'should throw exception on extra callbacks', function () { | ||
var c = __.close( { | ||
var c = __( { | ||
maxException: true | ||
} ); | ||
} ).close(); | ||
@@ -135,3 +171,3 @@ var calls = 0; | ||
var cb = c( function( err, data ) { | ||
var cb = c( function ( err, data ) { | ||
lastData = data; | ||
@@ -143,3 +179,3 @@ calls++; | ||
assert.throws( function() { | ||
assert.throws( function () { | ||
cb( null, 'exception' ); | ||
@@ -146,0 +182,0 @@ }, /max callbacks 1/ ); |
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
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
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
44860
1241
130
1