onetime
Advanced tools
Comparing version 1.1.0 to 2.0.0
26
index.js
'use strict'; | ||
module.exports = function (fn, errMsg) { | ||
const mimicFn = require('mimic-fn'); | ||
module.exports = (fn, opts) => { | ||
// TODO: remove this in v3 | ||
if (opts === true) { | ||
throw new TypeError('The second argument is now an options object'); | ||
} | ||
if (typeof fn !== 'function') { | ||
@@ -7,11 +14,12 @@ throw new TypeError('Expected a function'); | ||
var ret; | ||
var called = false; | ||
var fnName = fn.displayName || fn.name || (/function ([^\(]+)/.exec(fn.toString()) || [])[1]; | ||
opts = opts || {}; | ||
var onetime = function () { | ||
let ret; | ||
let called = false; | ||
const onetime = function () { | ||
if (called) { | ||
if (errMsg === true) { | ||
fnName = fnName ? fnName + '()' : 'Function'; | ||
throw new Error(fnName + ' can only be called once.'); | ||
if (opts.throw === true) { | ||
const name = fn.displayName || fn.name || '<anonymous>'; | ||
throw new Error(`Function \`${name}\` can only be called once`); | ||
} | ||
@@ -29,5 +37,5 @@ | ||
onetime.displayName = fnName; | ||
mimicFn(onetime, fn); | ||
return onetime; | ||
}; |
{ | ||
"name": "onetime", | ||
"version": "1.1.0", | ||
"description": "Only call a function once", | ||
"version": "2.0.0", | ||
"description": "Ensure a function is only called once", | ||
"license": "MIT", | ||
@@ -13,3 +13,3 @@ "repository": "sindresorhus/onetime", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=4" | ||
}, | ||
@@ -24,12 +24,22 @@ "scripts": { | ||
"once", | ||
"function", | ||
"one", | ||
"onetime", | ||
"func", | ||
"fn", | ||
"single", | ||
"call", | ||
"function", | ||
"called", | ||
"prevent" | ||
], | ||
"dependencies": { | ||
"mimic-fn": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"esnext": true | ||
} | ||
} |
# onetime [![Build Status](https://travis-ci.org/sindresorhus/onetime.svg?branch=master)](https://travis-ci.org/sindresorhus/onetime) | ||
> Only call a function once | ||
> Ensure a function is only called once | ||
@@ -29,25 +29,38 @@ When called multiple times it will return the return value from the first call. | ||
```js | ||
const foo = onetime(() => {}, {throw: true}); | ||
foo(); | ||
foo(); | ||
//=> Error: Function `foo` can only be called once | ||
``` | ||
## API | ||
### onetime(function, [shouldThrow]) | ||
### onetime(fn, [options]) | ||
#### function | ||
Returns a function that only calls `fn` once. | ||
Type: `function` | ||
#### fn | ||
Type: `Function` | ||
Function that should only be called once. | ||
#### shouldThrow | ||
#### options | ||
Type: `boolean` | ||
Type: `Object` | ||
##### throw | ||
Type: `boolean`<br> | ||
Default: `false` | ||
![](screenshot-shouldthrow.png) | ||
Throw an error when called more than once. | ||
Set to `true` if you want it to fail with a nice and descriptive error when called more than once. | ||
## License | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
3514
29
66
1
+ Addedmimic-fn@^1.0.0
+ Addedmimic-fn@1.2.0(transitive)