New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

emit-error

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

emit-error - npm Package Compare versions

Comparing version 1.1.0 to 2.0.0

28

example/usage.js
var EventEmitter = require('events').EventEmitter,
emitError = require('..')
function callback (err) {
console.log('did not get an error')
var emitter = new EventEmitter().on('error', function (err) {
console.error(err)
})
function fail (cb) {
cb(new Error('failed'))
}
function onError (err) {
console.error('got error: ' + err)
function succeed (cb) {
cb(null, 'success')
}
var emitter = new EventEmitter().on('error', onError)
var doSomething = emitError(emitter, callback)
fail(emitError(emitter, function (status) {
console.log('will not see this')
}))
doSomething(new Error('broke'))
doSomething()
fail(emitError(emitter, {alwaysCall: true}, function (err, status) {
// When always call specified, err is passed
console.log('will see this')
}))
succeed(emitError(emitter, function (status) {
// Gets called with status of success
console.log(status)
}))

@@ -6,8 +6,10 @@ module.exports = function emitError (emitter, options, cb) {

return function () {
var args = Array.prototype.slice.call(arguments, 0)
if (args[0])
emitter.emit('error', args[0])
if (cb && (!args[0] || options.alwaysCall))
return cb.apply(cb, args)
if (arguments[0])
emitter.emit('error', arguments[0])
if (!cb) return
if (options.alwaysCall)
return cb.apply(cb, arguments)
if (arguments[0]) return
return cb.apply(cb, Array.prototype.slice.call(arguments, 1))
}
}
{
"name": "emit-error",
"version": "1.1.0",
"version": "2.0.0",
"description": "Emit error if callback is executed with a non-null 1st argument",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -8,8 +8,4 @@ # emit-error

[![browser support](https://ci.testling.com/jasonpincin/emit-error.png)
](https://ci.testling.com/jasonpincin/emit-error)
Emit error if callback is executed with a truthy 1st argument
Emit error if callback is executed with a non-null 1st argument
## example

@@ -19,35 +15,31 @@

var EventEmitter = require('events').EventEmitter,
emitError = require('..')
emitError = require('emit-error')
function callback (err) {
console.log('did not get an error')
var emitter = new EventEmitter().on('error', function (err) {
console.error(err)
})
function fail (cb) {
cb(new Error('failed'))
}
function onError (err) {
console.error('got error: ' + err)
function succeed (cb) {
cb(null, 'success')
}
var emitter = new EventEmitter().on('error', onError)
var doSomething = emitError(emitter, callback)
fail(emitError(emitter, function (status) {
console.log('will not see this')
}))
doSomething(new Error('broke'))
doSomething()
```
fail(emitError(emitter, {alwaysCall: true}, function (err, status) {
// When always call specified, err is passed
console.log('will see this')
}))
output of above:
succeed(emitError(emitter, function (status) {
// Gets called with status of success
console.log(status)
}))
```
got error: Error: broke
did not get an error
```
# toc
- [api](#api)
- [emitError](#emiterror)
- [var wrappedCb = emitError(emitter, [options,] cb)](#var-wrappedcb-=-emiterroremitter-options-cb)
- [testing](#testing)
- [options](#options)
- [patterns](#patterns)
- [html coverage report](#html-coverage-report)
## api

@@ -61,7 +53,7 @@

Returns a function that when called with a non-falsey first argument, will emit that value
as an `error` event on the given event `emitter`. If a falsey first argument is passed to the
generated function, or if an `options` object is provided with a property of `alwaysCall` set to
`true`, then the provided callback `cb` will be executed with all arguments passed to the
generated function.
Returns a function that when called with a truthy first argument, will emit that value
as an `error` event on the supplied event `emitter`. If the option `alwaysCall` is defined,
the provided `cb` will be executed in all cases with all arguments supplied to `wrappedCb`, otherwise
if the 1st argument to `wrappedCb` is falsey, the supplied `cb` will be executed with all but the
1st argument supplied to `wrappedCb`.

@@ -72,4 +64,5 @@ If no callback `cb` is provided, then the generated callback will simply emit error events when

*options:*
- alwaysCall: `true` or `false` - if true, the provided callback `cb` will always be called, otherwise
it will only be called when a the first argument is falsey
- alwaysCall: `true` or `false` - if true, the provided callback `cb` will always be called (and include
the 1st argument), otherwise it will only be called when a the first argument is falsey (and without the
1st argument)

@@ -76,0 +69,0 @@

@@ -19,3 +19,3 @@ var test = require('tape'),

t.deepEqual(emitError(emitter, {alwaysCall:true}, callback)(new Error('error 2')), [new Error('error 2')], 'should return args on error with alwaysCall')
t.deepEqual(emitError(emitter, callback)(null, 1, 2), [null, 1, 2], 'should return args on no error')
t.deepEqual(emitError(emitter, callback)(null, 1, 2), [1, 2], 'should return args on no error')
t.notOk(emitError(emitter)(new Error('error 2')), 'should return undefined on error with no cb')

@@ -22,0 +22,0 @@ t.deepEqual(emitError(emitter)(), undefined, 'should return undefined on no error with no cb')

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc