{"id":22021545,"url":"https://github.com/lamansky/vfn","last_synced_at":"2026-04-16T12:36:37.813Z","repository":{"id":57391977,"uuid":"127126985","full_name":"lamansky/vfn","owner":"lamansky","description":"[Node.js] Variadic Function. Lets you specify a “rest parameter” that’s NOT at the end.","archived":false,"fork":false,"pushed_at":"2018-05-14T08:16:29.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T15:50:22.790Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lamansky.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-28T10:57:32.000Z","updated_at":"2018-05-14T08:16:31.000Z","dependencies_parsed_at":"2022-08-25T18:21:06.313Z","dependency_job_id":null,"html_url":"https://github.com/lamansky/vfn","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Fvfn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Fvfn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Fvfn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Fvfn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lamansky","download_url":"https://codeload.github.com/lamansky/vfn/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245085033,"owners_count":20558324,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-30T06:12:45.019Z","updated_at":"2026-04-16T12:36:32.781Z","avatar_url":"https://github.com/lamansky.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Variadic Function (vfn)\n\nLets you specify a “[rest parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters)” that’s _not_ at the end.\n\n## Installation\n\nRequires [Node.js](https://nodejs.org/) 6.0.0 or above.\n\n```bash\nnpm i vfn\n```\n\n## API\n\nThe module exports a single function.\n\n### Parameters\n\n1. Object argument (_or_ a value for `arg`):\n    * Optional: `arg` (positive integer): The zero-based index of which parameter in `fn`’s parameters list should be the “rest parameter.” Defaults to `0`.\n    * Optional: `oo` (boolean): If `true`, an optional options argument at the end of the parameter list will be ignored. (This will only work if you can be sure that none of your other arguments will be plain objects.) Defaults to `false`.\n2. `fn` (function): The function that has a parameter you want to convert into a “rest parameter.”\n\n### Return Value\n\nA wrapper function that turns all excess arguments into an array which is passed to `fn` at parameter index `arg`.\n\n## Example\n\nJavaScript supports “rest parameters,” but only at the end. Anything else will throw an error:\n\n```javascript\nfunction func (...a, b, c) {} // Uncaught SyntaxError: Rest parameter must be last formal parameter\n```\n\nThe `vfn` module lets you accomplish this. The index of `a` in the parameters list is `0`, so we pass `0` as the first argument to `vfn()`:\n\n```javascript\nconst vfn = require('vfn')\n\nconst func = vfn(0, function (a, b, c) {})\n\nfunc(1, 2, 3, 4, 5) // a = [1, 2, 3]; b = 4; c = 5\nfunc(1, 2, 3) // a = [1]; b = 2; c = 3\nfunc('test', 'example') // a = []; b = 'test'; c = 'example'\nfunc('hello world') // a = []; b = 'hello world'; c = undefined\n```\n\n### With Optional Options Argument\n\nNormally, `vfn` does not support optional parameters, because there’s no way to tell which arguments go to the optional parameter and which go to the variadic parameter. Therefore, `vfn` ignores optional parameters altogether. Take for example the common design pattern of including an optional options parameter at the end of the parameter list:\n\n```javascript\nconst vfn = require('vfn')\n\nconst func = vfn(0, function (a, b, c, {option} = {}) {})\n\nfunc(1, 2, 3, 4, 5) // a = [1, 2, 3]; b = 4; c = 5; option = undefined\nfunc(1, 2, 3, 4, 5, {option: 123}) // a = [1, 2, 3, 4]; b = 5; c = {option: 123}; option = undefined\n```\n\nAs you can see, the first call works as expected, because the optional options parameter is ignored completely. But it continues to be ignored when we introduce the options object in the second call. `vfn` collapses one too many arguments into `a` and puts the options object into `c`.\n\nTo make use of an optional options argument, set the `oo` flag when calling `vfn`:\n\n```javascript\nconst vfn = require('vfn')\n\nconst func = vfn({arg: 0, oo: true}, function (a, b, c, {option} = {}) {})\n\nfunc(1, 2, 3, 4, 5) // a = [1, 2, 3]; b = 4; c = 5; option = undefined\nfunc(1, 2, 3, 4, 5, {option: 123}) // a = [1, 2, 3]; b = 4; c = 5; option = 123\n```\n\nWith `oo` set, `vfn` will recognize that a plain object at the end belongs to an optional ending parameter.\n\nThis _only_ works if your options parameter is optional and is at the end, and if you can be sure that none of your other arguments will be plain objects.\n\n## Related\n\nFor more projects like this, check out [@lamansky/fn](https://github.com/lamansky/fn).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flamansky%2Fvfn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flamansky%2Fvfn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flamansky%2Fvfn/lists"}