Giter Club home page Giter Club logo

onetime's Introduction

onetime

Ensure a function is only called once

When called multiple times it will return the return value from the first call.

Unlike the module once, this one isn't naughty and extending Function.prototype.

Install

npm install onetime

Usage

import onetime from 'onetime';

let index = 0;

const foo = onetime(() => ++index);

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

onetime.callCount(foo); //=> 3
import onetime from 'onetime';

const foo = onetime(() => {}, {throw: true});

foo();

foo();
//=> Error: Function `foo` can only be called once

API

onetime(fn, options?)

Returns a function that only calls fn once.

fn

Type: Function

The function that should only be called once.

options

Type: object

throw

Type: boolean
Default: false

Throw an error when called more than once.

onetime.callCount(fn)

Returns a number representing how many times fn has been called.

Note: It throws an error if you pass in a function that is not wrapped by onetime.

import onetime from 'onetime';

const foo = onetime(() => {});

foo();
foo();
foo();

console.log(onetime.callCount(foo));
//=> 3

fn

Type: Function

The function to get call count from.

onetime's People

Contributors

bendingbender avatar chocolateboy avatar coreyfarrell avatar fregante avatar kevva avatar richienb avatar samverschueren avatar sindresorhus avatar yuler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

onetime's Issues

I read the source and find a Bug

var onetime = require('onetime');

var i = 0;

var foo = onetime(function (a) {
    i++;
    a = a + i;
    return a;
});

console.log(foo(1));
console.log(foo(2));
console.log(foo(3));

Hello,When I use this moudles,I Think I can get the value as 0 every time,But I get the result like this :2,2,2

Boolean flag for call state

It'd be roses and rainbows if there was an accessible property/flag to indicate if the function had been called the one time or not. Please let me know if you'd like me to elaborate. I thought about filing this under mem but this seemed like an appropriate first stop.

`onetime` or `oneTime`?

Every example uses onetime but IDEs will import it as oneTime because that's the name in index.js and index.d.ts

Should I update the documentation (to use oneTime) or the code/types (to onetime)?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.