diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-05-28 00:38:50 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-05-28 00:40:43 +0200 |
commit | 7fff4499fd915bcea3fa93b1aa8b35f4fe7a6027 (patch) | |
tree | 6de9a1aebd150a23b7f8c273ec657a5d0a18fe3e /node_modules/onetime | |
parent | 963b7a41feb29cc4be090a2446bdfe0c1f1bcd81 (diff) |
add linting (and some initial fixes)
Diffstat (limited to 'node_modules/onetime')
-rw-r--r-- | node_modules/onetime/index.js | 39 | ||||
-rw-r--r-- | node_modules/onetime/license | 21 | ||||
-rw-r--r-- | node_modules/onetime/package.json | 40 | ||||
-rw-r--r-- | node_modules/onetime/readme.md | 65 |
4 files changed, 165 insertions, 0 deletions
diff --git a/node_modules/onetime/index.js b/node_modules/onetime/index.js new file mode 100644 index 000000000..0d76476b0 --- /dev/null +++ b/node_modules/onetime/index.js @@ -0,0 +1,39 @@ +'use strict'; +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') { + throw new TypeError('Expected a function'); + } + + opts = opts || {}; + + let ret; + let called = false; + const fnName = fn.displayName || fn.name || '<anonymous>'; + + const onetime = function () { + if (called) { + if (opts.throw === true) { + throw new Error(`Function \`${fnName}\` can only be called once`); + } + + return ret; + } + + called = true; + ret = fn.apply(this, arguments); + fn = null; + + return ret; + }; + + mimicFn(onetime, fn); + + return onetime; +}; diff --git a/node_modules/onetime/license b/node_modules/onetime/license new file mode 100644 index 000000000..654d0bfe9 --- /dev/null +++ b/node_modules/onetime/license @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/node_modules/onetime/package.json b/node_modules/onetime/package.json new file mode 100644 index 000000000..73223ff96 --- /dev/null +++ b/node_modules/onetime/package.json @@ -0,0 +1,40 @@ +{ + "name": "onetime", + "version": "2.0.1", + "description": "Ensure a function is only called once", + "license": "MIT", + "repository": "sindresorhus/onetime", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "sindresorhus.com" + }, + "engines": { + "node": ">=4" + }, + "scripts": { + "test": "xo && ava" + }, + "files": [ + "index.js" + ], + "keywords": [ + "once", + "function", + "one", + "onetime", + "func", + "fn", + "single", + "call", + "called", + "prevent" + ], + "dependencies": { + "mimic-fn": "^1.0.0" + }, + "devDependencies": { + "ava": "*", + "xo": "*" + } +} diff --git a/node_modules/onetime/readme.md b/node_modules/onetime/readme.md new file mode 100644 index 000000000..95eb3b7c9 --- /dev/null +++ b/node_modules/onetime/readme.md @@ -0,0 +1,65 @@ +# onetime [![Build Status](https://travis-ci.org/sindresorhus/onetime.svg?branch=master)](https://travis-ci.org/sindresorhus/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](https://github.com/isaacs/once), this one isn't naughty extending `Function.prototype`.* + + +## Install + +``` +$ npm install --save onetime +``` + + +## Usage + +```js +let i = 0; + +const foo = onetime(() => i++); + +foo(); //=> 0 +foo(); //=> 0 +foo(); //=> 0 +``` + +```js +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` + +Function that should only be called once. + +#### options + +Type: `Object` + +##### throw + +Type: `boolean`<br> +Default: `false` + +Throw an error when called more than once. + + +## License + +MIT © [Sindre Sorhus](https://sindresorhus.com) |