{"id":22021634,"url":"https://github.com/lamansky/xfn","last_synced_at":"2026-04-09T22:43:50.687Z","repository":{"id":57401349,"uuid":"134438189","full_name":"lamansky/xfn","owner":"lamansky","description":"[Node.js] Extends a function object with configured versions of itself.","archived":false,"fork":false,"pushed_at":"2018-05-22T15:46:24.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-28T16:45:43.221Z","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-05-22T15:45:31.000Z","updated_at":"2018-05-22T15:46:25.000Z","dependencies_parsed_at":"2022-09-19T04:51:20.234Z","dependency_job_id":null,"html_url":"https://github.com/lamansky/xfn","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Fxfn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Fxfn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Fxfn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Fxfn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lamansky","download_url":"https://codeload.github.com/lamansky/xfn/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245085144,"owners_count":20558344,"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:13:26.326Z","updated_at":"2026-04-09T22:43:50.647Z","avatar_url":"https://github.com/lamansky.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Extended Function (xfn)\n\nExtends a function object with configured versions of itself.\n\n## Installation\n\nRequires [Node.js](https://nodejs.org/) 7.0.0 or above.\n\n```bash\nnpm i xfn\n```\n\n## Tutorial\n\n### Singular/Plural\n\n`xfn` simplifies the process of creating singular and plural versions of the same function (e.g. `get()` and `get.all()`). The function that you write is the plural version, and `xfn` creates the singular version. The following example uses `each` as the name of the plural function:\n\n```javascript\nconst xfn = require('xfn')\n\nconst getFirstOf = xfn({\n  pluralProp: 'each',\n  pluralReturn: true,\n}, arrs =\u003e arrs.map(arr =\u003e arr[0]))\n\nconst arr = [[1, 2], ['a', 'b']]\n\ngetFirstOf(arr) // [1, 2]\ngetFirstOf.each(arr) // [[1], ['a']]\n```\n\nThe first call treats `arr` as a single array, while the second call treats `arr` as an array of arrays.\n\n### Preset Options\n\n`xfn` lets you create subfunctions that have certain options preconfigured. For example, you can replace `fn(arg, {option: true})` with `fn.option(arg)`.\n\n```javascript\nconst getOwnProperty = require('get-own-property')\nconst xfn = require('xfn')\n\nconst get = xfn({\n  optionArg: 2, // The index of the parameter that contains the options\n  optionProps: {own: {own: true}},\n}, (obj, key, {own} = {}) =\u003e own ? getOwnProperty(obj, key) : obj[key])\n\nclass Cls {\n  get inherited () { return 123 }\n}\n\nconst obj = new Cls()\nobj.mine = 456\n\nget(obj, 'mine') // 456\nget(obj, 'inherited') // 123\nget.own(obj, 'mine') // 456\nget.own(obj, 'inherited') // undefined\n```\n\n## API\n\nThe module exports a single function.\n\n### Parameters\n\n1. Object argument:\n    * Optional: `pluralArg` (positive integer): The zero-based index of the argument that should be singularized if `pluralProp` is set. Defaults to `0`.\n    * Optional: `pluralFirst` (boolean): If `true`, the `pluralProp` comes before the `optionProps` when they are both set. Defaults to `false`. (For example: a `pluralProp` of `all` and an `optionProps` key of `in` will result in a property chain of `all.in()` if `true`, or `in.all()` if `false`.)\n    * Optional: `pluralProp` (string or symbol): If set, the returned function will be a singularized version of `fn`, and the original `fn` will be attached to the `pluralProp` property of the returned function (e.g. if `pluralProp` is set to `'all'` and the returned function is assigned to the variable `get`, the available functions will be `get()` and `get.all()`).\n    * Optional: `pluralReturn` (boolean): Only applies if `pluralProp` is set. Set to `true` if `fn` returns an array of one result per argument. (This will cause the one-element result array to be unwrapped when the singularized function is called.) Set to `false` if `fn` returns a result that does not correspond to the number of arguments. Defaults to `false`.\n    * Required if `optionProps` is set: `optionArg` (positive integer): The zero-based index of the argument into which the preconfigured options of `optionProps` should be inserted.\n    * Optional: `optionProps` (object or Map): A collection whose keys are the desired `fn` property keys and whose values are the option objects that should be merged into the plain object argument at index `optionArg`.\n    * Optional: `sbo` (object or false): Options to be passed to the [`sbo`](https://github.com/lamansky/sbo) module (which adds support for the bind operator), or `false` if you do not want the `sbo` module applied.\n2. `fn` (function): The main function that should be extended on the basis of the arguments in the first parameter.\n\n### Return Value\n\nA function object with `pluralProp` and/or `optionProps` properties.\n\n## Related\n\nThis module is part of the `fn` family of modules.\n\n* [efn](https://github.com/lamansky/efn): Extracted Function\n* [ffn](https://github.com/lamansky/ffn): Filtering Function\n* [jfn](https://github.com/lamansky/jfn): Joined Function\n* [mfn](https://github.com/lamansky/mfn): Memoized Function\n* [ofn](https://github.com/lamansky/ofn): Overloaded Function\n* [pfn](https://github.com/lamansky/pfn): Possible Function\n* [qfn](https://github.com/lamansky/qfn): Qualified Function\n* [vfn](https://github.com/lamansky/vfn): Variadic Function\n* [wfn](https://github.com/lamansky/wfn): Wrapper Function\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flamansky%2Fxfn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flamansky%2Fxfn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flamansky%2Fxfn/lists"}