You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

onetime

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

onetime - npm Package Compare versions

Comparing version

to
5.1.1

15

index.d.ts

@@ -18,2 +18,17 @@ declare namespace oneTime {

@returns A function that only calls `fn` once.
@example
```
import onetime = require('onetime');
let i = 0;
const foo = onetime(() => ++i);
foo(); //=> 1
foo(); //=> 1
foo(); //=> 1
onetime.callCount(foo); //=> 3
```
*/

@@ -20,0 +35,0 @@ <ArgumentsType extends unknown[], ReturnType>(

38

index.js

@@ -6,31 +6,25 @@ 'use strict';

const oneTime = (fn, options = {}) => {
if (typeof fn !== 'function') {
const oneTime = (function_, options = {}) => {
if (typeof function_ !== 'function') {
throw new TypeError('Expected a function');
}
let ret;
let isCalled = false;
let returnValue;
let callCount = 0;
const functionName = fn.displayName || fn.name || '<anonymous>';
const functionName = function_.displayName || function_.name || '<anonymous>';
const onetime = function (...args) {
const onetime = function (...arguments_) {
calledFunctions.set(onetime, ++callCount);
if (isCalled) {
if (options.throw === true) {
throw new Error(`Function \`${functionName}\` can only be called once`);
}
return ret;
if (callCount === 1) {
returnValue = function_.apply(this, arguments_);
function_ = null;
} else if (options.throw === true) {
throw new Error(`Function \`${functionName}\` can only be called once`);
}
isCalled = true;
ret = fn.apply(this, args);
fn = null;
return ret;
return returnValue;
};
mimicFn(onetime, fn);
mimicFn(onetime, function_);
calledFunctions.set(onetime, callCount);

@@ -45,8 +39,8 @@

module.exports.callCount = fn => {
if (!calledFunctions.has(fn)) {
throw new Error(`The given function \`${fn.name}\` is not wrapped by the \`onetime\` package`);
module.exports.callCount = function_ => {
if (!calledFunctions.has(function_)) {
throw new Error(`The given function \`${function_.name}\` is not wrapped by the \`onetime\` package`);
}
return calledFunctions.get(fn);
return calledFunctions.get(function_);
};
{
"name": "onetime",
"version": "5.1.0",
"version": "5.1.1",
"description": "Ensure a function is only called once",
"license": "MIT",
"repository": "sindresorhus/onetime",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},

@@ -12,0 +13,0 @@ "engines": {

@@ -1,2 +0,2 @@

# onetime [![Build Status](https://travis-ci.org/sindresorhus/onetime.svg?branch=master)](https://travis-ci.org/sindresorhus/onetime)
# onetime [![Build Status](https://travis-ci.com/sindresorhus/onetime.svg?branch=master)](https://travis-ci.com/github/sindresorhus/onetime)

@@ -9,3 +9,2 @@ > Ensure a function is only called once

## Install

@@ -17,3 +16,2 @@

## Usage

@@ -28,5 +26,5 @@

foo(); //=> 0
foo(); //=> 0
foo(); //=> 0
foo(); //=> 1
foo(); //=> 1
foo(); //=> 1

@@ -47,6 +45,5 @@ onetime.callCount(foo); //=> 3

## API
### onetime(fn, [options])
### onetime(fn, options?)

@@ -63,7 +60,7 @@ Returns a function that only calls `fn` once.

Type: `Object`
Type: `object`
##### throw
Type: `boolean`<br>
Type: `boolean`\
Default: `false`

@@ -80,2 +77,4 @@

```js
const onetime = require('onetime');
const foo = onetime(() => {});

@@ -97,5 +96,6 @@

## onetime for enterprise
## License
Available as part of the Tidelift Subscription.
MIT © [Sindre Sorhus](https://sindresorhus.com)
The maintainers of onetime and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-onetime?utm_source=npm-onetime&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)

Sorry, the diff of this file is not supported yet