{"id":18924621,"url":"https://github.com/dash-os/invoke-if","last_synced_at":"2026-03-14T04:30:18.271Z","repository":{"id":57275617,"uuid":"92695435","full_name":"Dash-OS/invoke-if","owner":"Dash-OS","description":"Invoke an array who's first element is predicate and second is a function.","archived":false,"fork":false,"pushed_at":"2017-11-17T00:49:34.000Z","size":32,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-19T15:08:08.369Z","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/Dash-OS.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-29T00:30:08.000Z","updated_at":"2017-05-29T00:30:32.000Z","dependencies_parsed_at":"2022-09-15T19:12:53.964Z","dependency_job_id":null,"html_url":"https://github.com/Dash-OS/invoke-if","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dash-OS%2Finvoke-if","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dash-OS%2Finvoke-if/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dash-OS%2Finvoke-if/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Dash-OS%2Finvoke-if/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Dash-OS","download_url":"https://codeload.github.com/Dash-OS/invoke-if/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239921875,"owners_count":19718842,"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-08T11:07:36.842Z","updated_at":"2026-03-14T04:30:18.189Z","avatar_url":"https://github.com/Dash-OS.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# invoke-if\n\nA dependency-free micro-package providing composable control-flow that works the\nway we always wished `switch` would work. It evaluates groups of tests and\nprovides a pattern for if, if/and, if/else, if/then.\n\nEach test that passes invokes that tests function or array of functions.\nOptionally aggregates the results of the passing invokes to the caller.\n\n## Installation\n\n```\nyarn add invoke-if\n```\n\n**or**\n\n```\nnpm install --save invoke-if\n```\n\n## Flow Coverage\n\nProudly built with 100% Flow Coverage. Includes flow definitions so if you are\nusing flow, it will type-check for you.\n\n## Breaking Update\n\nVersion 2.0 implements a change of the API for `invoke-if`. Previous versions\nare not likely to work. These changes were done during perf tests when we\nrealized 90% speed improvements by slightly adjusting the data structure that\ninvoke-if operates upon.\n\n## Examples\n\n```js\n/* @flow */\nimport { log } from '../utils/log';\nimport invokeIf, { invokeMap } from '../src/main';\n\nlog('Begin InvokeIf Basic Tests');\n\n// Note that `log()` returns the result of performance.now()\n\nconst INVOKE_TESTS = [\n  [\n    [true, () =\u003e log('one')],\n    [true, () =\u003e log('two')],\n    [false, () =\u003e log('three')],\n    [false, () =\u003e log('four')],\n  ],\n  new Map([\n    [() =\u003e true, () =\u003e log('five')],\n    [() =\u003e 'true', () =\u003e log('six')],\n    [() =\u003e 'seven', (arg: string) =\u003e log(arg)],\n  ]),\n];\n\nfunction one() {\n  log('Running Example One (invokeReduce / default)');\n  return invokeIf(...INVOKE_TESTS);\n}\n\nfunction two() {\n  log('Running Example Two (invokeMap)');\n  return invokeMap(...INVOKE_TESTS);\n}\n\nlog('Example One Result: \\n', one());\n\nlog('Example Two Result: \\n', two());\n\n/*\n  +1.5743    695406726.602197     Begin InvokeIf Basic Tests\n\n  +1.3778    695406727.980045     Running Example One (invokeReduce / default)\n  +0.3265    695406728.306522     one\n  +0.0703    695406728.376835     two\n  +0.0808    695406728.457588     five\n  +0.0625    695406728.520046     six\n  +0.0531    695406728.573129     seven\n  +0.0336    695406728.606769     Example One Result:\n  [\n    695406728.306522,\n    695406728.376835,\n    695406728.457588,\n    695406728.520046,\n    695406728.573129\n  ]\n\n  +1.2038    695406729.810552     Running Example Two (invokeMap)\n  +0.0957    695406729.906205     one\n  +0.0450    695406729.951161     two\n  +0.0308    695406729.981922     five\n  +0.0267    695406730.008638     six\n  +0.0256    695406730.034196     seven\n  +0.0245    695406730.058679     Example Two Result:\n  [\n    [ 695406729.906205, 695406729.951161 ],\n    [ 695406729.981922, 695406730.008638, 695406730.034196 ]\n  ]\n*/\n```\n\n\u003e There are many examples / tests available in the\n\u003e [examples directory](https://github.com/Dash-OS/invoke-if/tree/master/examples)\n\n---\n\n## Type Signatures\n\n```js\nexport type $NonFunction = boolean | string | number | { [key: string]: * };\n\nexport type InvokeCheck\u003c+A\u003e = (() =\u003e A) | A;\nexport type InvokeFn\u003cA\u003e = (arg: A) =\u003e mixed;\n\nexport type Invoker\u003cA\u003e =\n  | Array\u003cInvokeFn\u003cA\u003e | $NonFunction\u003e\n  | (InvokeFn\u003cA\u003e | $NonFunction);\n\nexport type ElseInvoker\u003cA\u003e =\n  | Array\u003cInvokeFn\u003cA\u003e | $NonFunction\u003e\n  | (InvokeFn\u003cA\u003e | $NonFunction);\n\nexport type InvokeTest\u003cA\u003e =\n  | [InvokeCheck\u003cA\u003e, Invoker\u003cA\u003e]\n  | [InvokeCheck\u003cA\u003e, Invoker\u003cA\u003e, ElseInvoker\u003cA\u003e];\n\nexport type FactoryFn\u003cA\u003e = () =\u003e void | false | null | InvokeTesters\u003cA\u003e;\n\nexport type InvokeTesters\u003cA\u003e =\n  | Array\u003cInvokeTest\u003cA\u003e\u003e\n  | Map\u003cInvokeCheck\u003cA\u003e, Invoker\u003cA\u003e\u003e\n  | FactoryFn\u003cA\u003e;\n```\n\n---\n\n## Module Exports\n\n### invokeReduce (Function) (default, named)\n\nThe default export, reduces the results of the tests into a simple array of\nresults.\n\n```js\ndeclare function invokeReduce(...tests: Array\u003cInvokeTesters\u003c*\u003e\u003e): Array\u003cmixed\u003e;\n```\n\n### invokeMap (Function)\n\nInstead of reducing and merging the responses together, `invokeMap` simply runs\nthe tests and maps the responses directly to the caller. The result is an array\nof results which are a closer match to the original array.\n\n```js\ndeclare function invokeMap(...tests: Array\u003cInvokeTesters\u003c*\u003e\u003e): Array\u003cmixed\u003e;\n```\n\n### invokeAny (Function)\n\nA shortcut for handling the situation when you want to evaluate every entry\nregardless of if a `falsey` value is found within the chain.\n\nIt is an alias for doing something like:\n\n```js\ninvokeIf(\n  [[true, () =\u003e console.log(1)]],\n  [[true, () =\u003e console.log(2)]],\n  [[false, () =\u003e console.log(3)]],\n  [[true, () =\u003e console.log(4)]],\n);\n\n// Same as\n\ninvokeAny(\n  [true, () =\u003e console.log(1)],\n  [true, () =\u003e console.log(2)],\n  [false, () =\u003e console.log(3)],\n  [true, () =\u003e console.log(4)],\n);\n```\n\n```js\ndeclare function invokeAny(...tests: Array\u003cInvokeTest\u003c*\u003e\u003e): Array\u003cmixed\u003e;\n```\n\n---\n\n## Control Flow\n\n`invoke-if` makes composing various situations much simpler then nesting if/else\nstatements of using switches in many cases. Below is a description of how the\narguments will be evaluated.\n\n### if/if...\n\n`invoke-if` evaluates entry within an argument until the it encounters a check\nthat results in a `falsey` response. In the nested example below, we would\ninvoke entries 1 and 2 while 3 and 4 would not occur since evaluating 3\nresulting in a `falsey` result.\n\n```js\ninvokeIf([\n  ['true', () =\u003e console.log(1)],\n  [() =\u003e true, () =\u003e console.log(2)],\n  [false, () =\u003e console.log(3)],\n  [true, () =\u003e console.log(4)],\n]);\n```\n\n### if/then\n\n`invoke-if` evaluates each argument independently of the others. Each separate\nargument sent to one of `invoke-if`'s functions can be thought of as `then`\narguments.\n\nIn the example below, we would invoke arguments 1, 2, 5, and 6.\n\n```js\ninvokeIf(\n  [\n    ['true', () =\u003e console.log(1)],\n    [() =\u003e true, () =\u003e console.log(2)],\n    [false, () =\u003e console.log(3)],\n    [true, () =\u003e console.log(4)],\n  ],\n  [\n    ['true', () =\u003e console.log(5)],\n    [() =\u003e true, () =\u003e console.log(6)],\n    [false, () =\u003e console.log(7)],\n    [true, () =\u003e console.log(8)],\n  ],\n);\n```\n\n### if/else\n\nWhen encountering a failed argument, `invoke-if` will check if there is a third\nelement to the entry (not compatible with `Map`). If there is, it will invoke\nthe third element and add it to the results before breaking evaluation of the\ngiven argument.\n\nIn the example above it will print `1` then `2` then `'done'`\n\n```js\nconst done = () =\u003e console.log('done');\n\ninvokeIf([\n  ['true', () =\u003e console.log(1), done],\n  [() =\u003e true, () =\u003e console.log(2), done],\n  [false, () =\u003e console.log(3), done],\n  [true, () =\u003e console.log(4), done],\n]);\n```\n\n### Customized Flow\n\nWhen encountering a function rather than an Array or Map, it will assume that\nthe function is a factory. It will first call the function and will evaluate the\nresult if it can.\n\nThe example below will toggle which case is executed each time any of the\ncurrent elements tests encounters a `falsey` response.\n\n```js\nlet i = 0;\n\nconst done = () =\u003e {\n  i += 1;\n  console.log('done');\n};\n\nfunction doinvoke() {\n  return invokeIf(\n    () =\u003e\n      i % 2 === 0 \u0026\u0026 [\n        ['true', () =\u003e console.log(1), done],\n        [() =\u003e true, () =\u003e console.log(2), done],\n        [false, () =\u003e console.log(3), done],\n        [true, () =\u003e console.log(4), done],\n      ],\n    () =\u003e\n      i % 2 !== 0 \u0026\u0026 [\n        ['true', () =\u003e console.log(5), done],\n        [() =\u003e true, () =\u003e console.log(6), done],\n        [false, () =\u003e console.log(7), done],\n        [true, () =\u003e console.log(8), done],\n      ],\n  );\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdash-os%2Finvoke-if","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdash-os%2Finvoke-if","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdash-os%2Finvoke-if/lists"}