noargs-wrapper 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -0
- package/index.js +21 -0
- package/package.json +30 -0
package/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# noargs-wrapper
|
|
2
|
+
|
|
3
|
+
Returns a wrapper that calls a function with no arguments.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
$ npm install noargs-wrapper
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
This function takes in a function, and returns a wrapper that calls the passed-in function with no arguments, even if arguments are passed in to the wrapper. (unless the function passed in takes over 100 arguments)
|
|
14
|
+
|
|
15
|
+
Here's an example:
|
|
16
|
+
|
|
17
|
+
```js
|
|
18
|
+
var noargs = require('noargs-wrapper');
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
var isOne = v => v === 1; // a simple function to check if a value is one
|
|
22
|
+
|
|
23
|
+
var wrappedIsOne = noargs(isOne);
|
|
24
|
+
|
|
25
|
+
console.log(wrappedIsOne(1)); // always returns false because no arguments are passed in, and undefined is not one.
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
var isUndefined = v => v === void 0; // a simple function to check if a value is undefined
|
|
29
|
+
|
|
30
|
+
var wrappedIsUndefined = noargs(isUndefined);
|
|
31
|
+
|
|
32
|
+
console.log(wrappedIsUndefined(1)); // always returns true because no arguments are passed in, so undefined is undefined, hence true
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Contributing
|
|
36
|
+
|
|
37
|
+
PRs and issues are welcome on the github repository!
|
package/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const GetIntrinsic = require("get-intrinsic")
|
|
2
|
+
const ignoreArgument = require("ignore-argument")
|
|
3
|
+
const isFunction = require("is-function")
|
|
4
|
+
|
|
5
|
+
const $Array = GetIntrinsic('%Array%')
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Wrap a function so it's always called with no arguments.
|
|
9
|
+
* @param {Function} fn - The function to wrap
|
|
10
|
+
* @returns {Function} - A new function that ignores all passed arguments
|
|
11
|
+
*/
|
|
12
|
+
function noArgsWrapper(fn) {
|
|
13
|
+
if (!isFunction(fn)) {
|
|
14
|
+
throw new TypeError("Expected a function")
|
|
15
|
+
}
|
|
16
|
+
const ignoreAll = $Array(100).fill(true)
|
|
17
|
+
|
|
18
|
+
return ignoreArgument(fn, ...ignoreAll)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
module.exports = noArgsWrapper
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "noargs-wrapper",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Wrap a function to always call it with no arguments, ignoring all arguments passed in.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"function",
|
|
7
|
+
"wrapper",
|
|
8
|
+
"arguments",
|
|
9
|
+
"utility",
|
|
10
|
+
"ignore",
|
|
11
|
+
"minimal"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/tj-commits/noargs-wrapper#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/tj-commits/noargs-wrapper/issues"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/tj-commits/noargs-wrapper.git"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"author": "me",
|
|
23
|
+
"type": "commonjs",
|
|
24
|
+
"main": "index.js",
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"get-intrinsic": "^1.3.0",
|
|
27
|
+
"ignore-argument": "^0.1.1",
|
|
28
|
+
"is-function": "^1.0.2"
|
|
29
|
+
}
|
|
30
|
+
}
|