New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

once-only

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

once-only

Call function once. Create a never to be repeated function

latest
Source
npmnpm
Version
1.0.3
Version published
Maintainers
1
Created
Source

Call function once

NPM Version NPM Downloads GitHub issues Telegram

Create a once-only function and it will be never repeated

Installation

npm install once-only

or

yarn add once-only

Usage

import { onceOnly } from 'once-only'

Create:

const createUserOnce = onceOnly(createUser);

Original function will be called only once. It returns cache on a repeated call:

const currentUser = createUserOnce({ userId: 234 });

You can pass configuration object to onceOnly as a second argument:

API

Configuration options:

You can pass config as a second argument.

Default values are: { attach: false, strict: false }

attach option

Attach once function to the original function:

onceOnly(fetchCompany, { attach: true });

Then call it:

const promise = fetchCompany.once({ companyId: 11 });

strict option

You can throw an error when a function is called more than once:

const getIdOnceOnly = onceOnly(getId, { strict: true });
const users = getIdOnceOnly({ userId: 234 }); // Throws an error

Static properties of a once-only function:

Returned function has two static properties (as well as an attached once function):

.called

console.log(fetchUsersOnce.called); // true or false

.cache

console.log(fetchUserOnce.cache); // function: () => cache
const result = fetchUserOnce.cache(); // call cache getter
console.log(result); // logs the result of the first function call

Tip

If you found this packeage useful, please star it on GitHub

Definition of "once-only" in English

once-only adjective

That occurs, operates, etc., on one occasion only.

Origin:

1960s; earliest use found in The Sunday Express.

once-only in British adjective

(ˌwʌnsˈəʊnlɪ)

never to be repeated

Examples:

  • This is a once-only offer.
  • This once-only opportunity to select one of these books absolutely free
  • Membership is valid for ten years for a once-only donation of £2,500.

^Collins English Dictionary. Copyright © HarperCollins Publishers

Author

@doasync

Licence

MIT

Keywords

once

FAQs

Package last updated on 15 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts