{"id":22021542,"url":"https://github.com/lamansky/pfn","last_synced_at":"2026-05-06T07:41:18.453Z","repository":{"id":57322547,"uuid":"120182880","full_name":"lamansky/pfn","owner":"lamansky","description":"[Node.js] Possible Function. Wraps what might be a function, with fallback behavior in case it’s not. Perfect for use in functions that accept optional callback arguments.","archived":false,"fork":false,"pushed_at":"2019-11-23T19:01:43.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-18T11:24:54.629Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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-02-04T12:27:14.000Z","updated_at":"2019-11-23T19:01:45.000Z","dependencies_parsed_at":"2022-08-26T01:11:27.381Z","dependency_job_id":null,"html_url":"https://github.com/lamansky/pfn","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%2Fpfn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Fpfn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Fpfn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lamansky%2Fpfn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lamansky","download_url":"https://codeload.github.com/lamansky/pfn/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:43.026Z","updated_at":"2026-05-06T07:41:13.435Z","avatar_url":"https://github.com/lamansky.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Possible Function (pfn)\n\nWraps what might be a function, with fallback behavior in case it’s not. Perfect for use in functions that accept optional callback arguments.\n\n## Installation\n\nRequires [Node.js](https://nodejs.org/) 6.0.0 or above.\n\n```bash\nnpm i pfn\n```\n\n## API\n\nThere are two ways you can import the module: `require('pfn')` or `require('pfn/strict')`. Each exposes a single function with the same signature. The difference is that strict mode will throw an error if `fn` is anything other than a function, `null`, or `undefined`. Normal mode will silently defer to `or` if `fn` is of an unexpected type.\n\n### Parameters\n\n1. `fn` (any): The value that may or may not be a function.\n2. Optional: `or` (any): The function that will be called, or the value that will be returned, if `fn` is not a function. Defaults to a passthrough function (`a =\u003e a`).\n\n### Return Value\n\nAlways returns a Function.\n\n* `fn`, if `fn` is a Function\n* `or`, if `or` is a Function\n* A Function that returns `or`, if neither `fn` nor `or` is a function\n\n## Examples\n\n`pfn` wraps a value that may or may not be a function. If the underlying value is not a function, then `pfn` will execute one of the following fallback behaviors.\n\n### Passthrough Fallback\n\nIf the value turns out to not be a function, `pfn` will, by default, pass through whatever is given as the first argument. This is useful for optional filters.\n\n```javascript\nconst pfn = require('pfn')\n\nfunction sayHello (name, filter) {\n  filter = pfn(filter)\n  return filter('Hello, ' + name)\n}\n\n// No filter is provided, so the hello message is returned without change:\nsayHello('world') // 'Hello, world'\n\n// A filter is provided which changes the hello message:\nsayHello('world', m =\u003e m + '!!') // 'Hello, world!!'\n```\n\n### Return-Value Fallback\n\nIf the value turns out to not be a function, `pfn` can be configured to return a value of your choosing.\n\n```javascript\nconst pfn = require('pfn')\n\nfunction sayHello (nameCallback) {\n  nameCallback = pfn(nameCallback, 'world')\n  return 'Hello, ' + nameCallback()\n}\n\nsayHello() // 'Hello, world'\nsayHello(() =\u003e 'Dolly') // 'Hello, Dolly'\n```\n\n### Self Fallback\n\nIf you provide the possible function as its own fallback, then you can accept either a value or a function as an argument for your code. For example, the `sayHello` function in the following example can accept either a string or a function.\n\nDon’t use the module’s strict mode (`require('pfn/strict')`) if you want self-fallback behavior, because strict mode will throw an error if the first parameter is anything other than a function, `null`, or `undefined`.\n\n```javascript\nconst pfn = require('pfn')\n\nfunction sayHello (name) {\n  name = pfn(name, name)\n  return 'Hello, ' + name()\n}\n\nsayHello('world') // 'Hello, world'\nsayHello(() =\u003e 'world') // 'Hello, world'\n```\n\n### Custom Fallback\n\nIf the wrapped value turns out to not be a function, the wrapping function can execute a custom fallback function instead:\n\n```javascript\nconst pfn = require('pfn')\nconst mightBeAFunction = null\nconst callback = pfn(mightBeAFunction, (...args) =\u003e args.length)\ncallback('arg 1', 'arg 2') // 2\n```\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* [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* [xfn](https://github.com/lamansky/xfn): Extended Function\n* [3fn](https://github.com/lamansky/3fn): Three-Way Comparison Function\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flamansky%2Fpfn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flamansky%2Fpfn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flamansky%2Fpfn/lists"}