Clean up the oopsie. Rebuild the blank exercises.

This commit is contained in:
Michael Frank
2021-05-09 00:39:21 +12:00
parent b984cb4c6f
commit d28d80c46f
1565 changed files with 27186 additions and 199003 deletions
+28 -23
View File
@@ -1,39 +1,44 @@
'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');
}
const calledFunctions = new WeakMap();
if (typeof fn !== 'function') {
const onetime = (function_, options = {}) => {
if (typeof function_ !== 'function') {
throw new TypeError('Expected a function');
}
opts = opts || {};
let returnValue;
let callCount = 0;
const functionName = function_.displayName || function_.name || '<anonymous>';
let ret;
let called = false;
const fnName = fn.displayName || fn.name || '<anonymous>';
const onetime = function (...arguments_) {
calledFunctions.set(onetime, ++callCount);
const onetime = function () {
if (called) {
if (opts.throw === true) {
throw new Error(`Function \`${fnName}\` 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`);
}
called = true;
ret = fn.apply(this, arguments);
fn = null;
return ret;
return returnValue;
};
mimicFn(onetime, fn);
mimicFn(onetime, function_);
calledFunctions.set(onetime, callCount);
return onetime;
};
module.exports = onetime;
// TODO: Remove this for the next major release
module.exports.default = onetime;
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(function_);
};
+5 -17
View File
@@ -1,21 +1,9 @@
The MIT License (MIT)
MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://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:
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 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.
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.
+22 -19
View File
@@ -1,51 +1,54 @@
{
"_from": "onetime@^2.0.0",
"_id": "onetime@2.0.1",
"_from": "onetime@^5.1.0",
"_id": "onetime@5.1.2",
"_inBundle": false,
"_integrity": "sha1-BnQoIw/WdEOyeUsiu6UotoZ5YtQ=",
"_integrity": "sha512-kbpaSSGJTWdAY5KPVeMOKXSrPtr8C8C7wodJbcsd51jRnmD+GZu8Y0VoU6Dm5Z4vWr0Ig/1NKuWRKf7j5aaYSg==",
"_location": "/onetime",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "onetime@^2.0.0",
"raw": "onetime@^5.1.0",
"name": "onetime",
"escapedName": "onetime",
"rawSpec": "^2.0.0",
"rawSpec": "^5.1.0",
"saveSpec": null,
"fetchSpec": "^2.0.0"
"fetchSpec": "^5.1.0"
},
"_requiredBy": [
"/restore-cursor"
"/jest-changed-files/execa"
],
"_resolved": "https://registry.npmjs.org/onetime/-/onetime-2.0.1.tgz",
"_shasum": "067428230fd67443b2794b22bba528b6867962d4",
"_spec": "onetime@^2.0.0",
"_where": "/Users/cloyd/coderrr/odin/javascript-exercises/node_modules/restore-cursor",
"_resolved": "https://registry.npmjs.org/onetime/-/onetime-5.1.2.tgz",
"_shasum": "d0e96ebb56b07476df1dd9c4806e5237985ca45e",
"_spec": "onetime@^5.1.0",
"_where": "/home/michael/projects/javascript-exercises/node_modules/jest-changed-files/node_modules/execa",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"bugs": {
"url": "https://github.com/sindresorhus/onetime/issues"
},
"bundleDependencies": false,
"dependencies": {
"mimic-fn": "^1.0.0"
"mimic-fn": "^2.1.0"
},
"deprecated": false,
"description": "Ensure a function is only called once",
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^1.4.1",
"tsd": "^0.7.1",
"xo": "^0.24.0"
},
"engines": {
"node": ">=4"
"node": ">=6"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"funding": "https://github.com/sponsors/sindresorhus",
"homepage": "https://github.com/sindresorhus/onetime#readme",
"keywords": [
"once",
@@ -66,7 +69,7 @@
"url": "git+https://github.com/sindresorhus/onetime.git"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"version": "2.0.1"
"version": "5.1.2"
}
+44 -15
View File
@@ -1,32 +1,36 @@
# 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)
> 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`.*
*Unlike the module [once](https://github.com/isaacs/once), this one isn't naughty and extending `Function.prototype`.*
## Install
```
$ npm install --save onetime
$ npm install onetime
```
## Usage
```js
const onetime = require('onetime');
let i = 0;
const foo = onetime(() => i++);
const foo = onetime(() => ++i);
foo(); //=> 0
foo(); //=> 0
foo(); //=> 0
foo(); //=> 1
foo(); //=> 1
foo(); //=> 1
onetime.callCount(foo); //=> 3
```
```js
const onetime = require('onetime');
const foo = onetime(() => {}, {throw: true});
foo();
@@ -35,10 +39,9 @@ foo();
//=> Error: Function `foo` can only be called once
```
## API
### onetime(fn, [options])
### onetime(fn, options?)
Returns a function that only calls `fn` once.
@@ -50,16 +53,42 @@ Function that should only be called once.
#### options
Type: `Object`
Type: `object`
##### throw
Type: `boolean`<br>
Type: `boolean`\
Default: `false`
Throw an error when called more than once.
### onetime.callCount(fn)
## License
Returns a number representing how many times `fn` has been called.
MIT © [Sindre Sorhus](https://sindresorhus.com)
Note: It throws an error if you pass in a function that is not wrapped by `onetime`.
```js
const onetime = require('onetime');
const foo = onetime(() => {});
foo();
foo();
foo();
console.log(onetime.callCount(foo));
//=> 3
```
#### fn
Type: `Function`
Function to get call count from.
## onetime for enterprise
Available as part of the Tidelift Subscription.
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)