{"id":27001808,"url":"https://github.com/manferlo81/selective-option","last_synced_at":"2025-04-04T04:20:10.920Z","repository":{"id":36989815,"uuid":"309342962","full_name":"manferlo81/selective-option","owner":"manferlo81","description":"A simple selective option resolver","archived":false,"fork":false,"pushed_at":"2025-04-03T00:01:32.000Z","size":1882,"stargazers_count":0,"open_issues_count":5,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-03T11:02:35.641Z","etag":null,"topics":["config","option","options","resolve","resolver","selective"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/manferlo81.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-02T11:00:09.000Z","updated_at":"2025-03-13T01:26:17.000Z","dependencies_parsed_at":"2024-10-28T04:01:42.629Z","dependency_job_id":"87c56eb7-d499-46e6-9dfc-abb169475523","html_url":"https://github.com/manferlo81/selective-option","commit_stats":{"total_commits":440,"total_committers":2,"mean_commits":220.0,"dds":0.4363636363636364,"last_synced_commit":"2543ed761f12e2f7b5dc759423961985717057f7"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/manferlo81%2Fselective-option","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/manferlo81%2Fselective-option/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/manferlo81%2Fselective-option/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/manferlo81%2Fselective-option/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/manferlo81","download_url":"https://codeload.github.com/manferlo81/selective-option/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247118643,"owners_count":20886591,"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":["config","option","options","resolve","resolver","selective"],"created_at":"2025-04-04T04:20:10.335Z","updated_at":"2025-04-04T04:20:10.912Z","avatar_url":"https://github.com/manferlo81.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Selective Option\n\n[![CI](https://github.com/manferlo81/selective-option/actions/workflows/ci.yml/badge.svg?branch=main\u0026event=push)](https://github.com/manferlo81/selective-option/actions/workflows/ci.yml)\n[![NPM Version](https://badgen.net/npm/v/selective-option)](https://www.npmjs.com/package/selective-option)\n[![install size](https://packagephobia.com/badge?p=selective-option)](https://packagephobia.com/result?p=selective-option)\n[![jsDelivr](https://data.jsdelivr.com/v1/package/npm/selective-option/badge?style=rounded)](https://www.jsdelivr.com/package/npm/selective-option)\n[![Libraries.io dependency status for GitHub repo](https://img.shields.io/librariesio/github/manferlo81/selective-option)](https://libraries.io/npm/selective-option)\n[![Codecov](https://codecov.io/gh/manferlo81/selective-option/branch/main/graph/badge.svg?token=U0GIRWISBJ)](https://codecov.io/gh/manferlo81/selective-option)\n[![Known Vulnerabilities](https://snyk.io/test/github/manferlo81/selective-option/badge.svg)](https://snyk.io/test/github/manferlo81/selective-option)\n\nA simple selective option resolver\n\n\u003e Version `0.1.0` is a complete rewrite. Please read the documentation before you consider to switch, many exports have been removed and new ones have been added. It is highly recommended to switch to `0.1.0`, just read the documentation before you do.\n\n## In this page\n\n* [Install](#install)\n  * [npm](#npm)\n  * [yarn](#yarn)\n  * [pnpm](#pnpm)\n* [CDN](#cdn)\n  * [jsDelivr](#jsdelivr)\n  * [unpkg](#unpkg)\n* [API](#api)\n  * Resolvers\n    * *function* [`createValueBasedResolver`](#function-createvaluebasedresolver)\n    * *function* [`createBoolBasedResolver`](#function-createboolbasedresolver)\n  * Potential Resolvers\n    * *function* [`createValueResolver`](#function-createvalueresolver)\n    * *function* [`createFunctionResolver`](#function-createfunctionresolver)\n    * *function* [`createKeyResolver`](#function-createkeyresolver)\n    * *function* [`createKeyListResolver`](#function-createkeylistresolver)\n    * *function* [`createObjectResolver`](#function-createobjectresolver)\n  * Others\n    * *function* [`createResolver`](#function-createresolver)\n    * *function* [`createResult`](#function-createresult)\n* [Exported Types](#exported-types)\n  * Input Types\n    * *type* [`FunctionOption`](#type-functionoption)\n    * *type* [`SingleKeyOption`](#type-singlekeyoption)\n    * *type* [`KeyListOption`](#type-keylistoption)\n    * *type* [`KeyOption`](#type-keyoption)\n    * *type* [`ObjectOption`](#type-objectoption)\n    * *type* [`ValueBasedSelectiveOption`](#type-valuebasedselectiveoption)\n    * *type* [`BoolBasedSelectiveOption`](#type-boolbasedselectiveoption)\n  * Resolver related Types\n    * *type* [`KeyList`](#type-keylist)\n    * *type* [`SpecialKeys`](#type-specialkeys)\n    * *type* [`Resolved`](#type-resolved)\n    * *type* [`PotentiallyResolved`](#type-potentiallyresolved)\n    * *type* [`PotentialResolver`](#type-potentialresolver)\n    * *type* [`Resolver`](#type-resolver)\n    * *type* [`ValueBasedResolver`](#type-valuebasedresolver)\n    * *type* [`BoolBasedResolver`](#type-boolbasedresolver)\n* [Other Types](#other-types)\n  * *type* [`PositiveKey`](#type-positivekey)\n  * *type* [`NegativeKey`](#type-negativekey)\n  * *type* [`TypeCheckFunction`](#type-typecheckfunction)\n\n## Install\n\n### npm\n\n```bash\nnpm install selective-option\n```\n\n### yarn\n\n```bash\nyarn add selective-option\n```\n\n### pnpm\n\n```bash\npnpm add selective-option\n```\n\n## CDN\n\n### jsDelivr\n\n* ***UMD***\n\n```html\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/selective-option@latest/dist/umd/selective.umd.js\"\u003e\u003c/script\u003e\n```\n\n```html\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/selective-option@latest/dist/umd/selective.umd.min.js\"\u003e\u003c/script\u003e\n```\n\n* ***ES Module***\n\n```html\n\u003cscript type=\"module\"\u003e\n  import selective from \"https://cdn.jsdelivr.net/npm/selective-option@latest/dist/esm/selective.mjs\";\n\u003c/script\u003e\n```\n\n```html\n\u003cscript type=\"module\"\u003e\n  import selective from \"https://cdn.jsdelivr.net/npm/selective-option@latest/dist/esm/selective.min.mjs\";\n\u003c/script\u003e\n```\n\n[*more options...*](https://www.jsdelivr.com/package/npm/selective-option?version=latest)\n\n### unpkg\n\n* ***UMD***\n\n```html\n\u003cscript src=\"https://unpkg.com/selective-option@latest/dist/umd/selective.umd.js\"\u003e\u003c/script\u003e\n```\n\n```html\n\u003cscript src=\"https://unpkg.com/selective-option@latest/dist/umd/selective.umd.min.js\"\u003e\u003c/script\u003e\n```\n\n* ***ES Module***\n\n```html\n\u003cscript type=\"module\"\u003e\n  import selective from \"https://unpkg.com/selective-option@latest/dist/esm/selective.mjs\";\n\u003c/script\u003e\n```\n\n```html\n\u003cscript type=\"module\"\u003e\n  import selective from \"https://unpkg.com/selective-option@latest/dist/esm/selective.min.mjs\";\n\u003c/script\u003e\n```\n\n[*more options...*](https://unpkg.com/selective-option@latest/)\n\n## API\n\n### *function* `createValueBasedResolver`\n\nCreates a `value based resolver`. It resolves input as `valid value` (`V`), `null`, `undefined`, [`FunctionOption`](#type-functionoption) or [`ObjectOption`](#type-objectoption). It internally uses [`createValueResolver`](#function-createvalueresolver), [`createFunctionResolver`](#function-createfunctionresolver) and [`createObjectResolver`](#function-createobjectresolver) to create a [`Resolver`](#type-resolver)  using [`createResolver`](#function-createresolver). See the examples for more info.\n\n```typescript\nfunction createValueBasedResolver\u003cK extends string, S extends string, V, O extends string, D = V\u003e(\n  keys: readonly K[],\n  isValidValue: TypeCheckFunction\u003cV\u003e,\n  defaultValue: D,\n  overrideKey: O,\n  special?: SpecialKeys\u003cS, K\u003e | null | undefined,\n): ValueBasedResolver\u003cK, S | O, V, D\u003e;\n```\n\n* *Arguments*\n  * `keys`: An `array` of `string` to be used as `keys` in the final [`Resolved`](#type-resolved) object. They will also be used to validate `keys` if input is an `object`.\n  * `isValidValue`: A `function` which returns wether or not a `value` is `valid`.\n  * `defaultValue`: A `valid` `value` to be used as default in case the value is `null` or `undefined`.\n  * `overrideKey`: A `string` to be used to detect the `override key` if the input is an `object`.\n  * `special`: An optional object mapping `special keys` to multiple `regular keys`. They can be used as `keys` if the input is an `object`.\n\nSee [`TypeCheckFunction`](#type-typecheckfunction), [`SpecialKeys`](#type-specialkeys) and [`ValueBasedResolver`](#type-valuebasedresolver).\n\n* *Example*\n\n```typescript\nconst resolveNumber = createValueBasedResolver(\n  ['a', 'b', 'c'] as const,\n  (value: unknown): value is number =\u003e typeof value === 'number',\n  0,\n  'override',\n  { ac: ['a', 'c'] },\n);\n\nresolveNumber(18); // set value { a: 18, b: 18, c: 18 }\nresolveNumber(true); // Throws because true doesn't pass the test\nresolveNumber({}); // default value { a: 0, b: 0, c: 0 }\nresolveNumber({ override: 40 }); // overridden value { a: 40, b: 40, c: 40 }\nresolveNumber({ override: 'string' }); // Throws because 'string' doesn't pass the test\nresolveNumber({ b: 40 }); // default + set value { a: 0, b: 40 c: 0 }\nresolveNumber({ c: [] }); // Throws because [] doesn't pass the test\nresolveNumber({ ac: 40 }); // default + special set value { a: 40, b: 0 c: 40 }\nresolveNumber({ override: 40, a: 12 }); // overridden + set value { a: 12, b: 40, c: 40 }\nresolveNumber({ override: 40, ac: 12 }); // overridden + special set value { a: 12, b: 40, c: 12 }\n// ... you get the idea...\n```\n\n### *function* `createBoolBasedResolver`\n\nCreates a `boolean based resolver`. It resolves input as `valid value` (`V`), `boolean`, `null`, `undefined`, [`FunctionOption`](#type-functionoption), [`KeyOption`](#type-keyoption) or [`ObjectOption`](#type-objectoption). It internally uses [`createValueResolver`](#function-createvalueresolver), [`createFunctionResolver`](#function-createfunctionresolver), [`createKeyResolver`](#function-createkeyresolver), [`createKeyListResolver`](#function-createkeylistresolver) and [`createObjectResolver`](#function-createobjectresolver) to create a [`Resolver`](#type-resolver) using [`createResolver`](#function-createresolver). See the examples for more info.\n\n```typescript\nfunction createBoolBasedResolver\u003cK extends string, S extends string, V, O extends string, D = V\u003e(\n  keys: readonly K[],\n  isValidValue: AllowNullish\u003cTypeCheckFunction\u003cV\u003e\u003e,\n  defaultValue: D,\n  overrideKey: O,\n  special?: SpecialKeys\u003cS, K\u003e | null | undefined,\n): BoolBasedResolver\u003cK, S, V | boolean, O, D\u003e;\n```\n\n* *Arguments*\n  * `keys`: An `array` of `string` to be used as `keys` in the final [`Resolved`](#type-resolved) object. They will also be used to validate `positive keys` and `negative keys` if input is a `string` or an `array`, and to validate `keys` if input is an `object`.\n  * `isValidValue`: A `function` which returns wether or not a `value` is `valid`. Note that this function doesn't need to test for `boolean` values as they will be included by default. If pass `null` or `undefined`, it will only test for `boolean` values.\n  * `defaultValue`: A `valid` `value` to be used as default in case the value is `null` or `undefined`.\n  * `overrideKey`: A `string` to be used to detect the `override key` if the input is an `object`.\n  * `special`: An optional object mapping `special keys` to multiple `regular keys`. They can be used as `keys` if the input is an `object` and as `positive keys` or `negative keys` if input is a `string` or an `array`.\n\nSee [`TypeCheckFunction`](#type-typecheckfunction), [`SpecialKeys`](#type-specialkeys) and [`BoolBasedResolver`](#type-boolbasedresolver).\n\n* *Example*\n\n```typescript\nconst resolve = createBoolBasedResolver(\n  ['a', 'b', 'c'] as const,\n  (value: unknown): value is ('yes' | 'not' | 'unknown') =\u003e {\n    return ['yes', 'no', 'unknown'].includes(value as never);\n  },\n  'unknown',\n  'default',\n  { ab: ['a', 'b'] },\n);\n\nresolveEvenNumber(null); // default value { a: 'unknown', b: 'unknown', c: 'unknown' }\nresolveEvenNumber('yes'); // set value { a: 'yes', b: 'yes', c: 'yes' }\nresolveEvenNumber(17); // Throws because 17 doesn't pass the test\nresolveEvenNumber('a'); // set key { a: true, b: false, c: false }\nresolveEvenNumber('ab'); // set key { a: true, b: true, c: false }\nresolveEvenNumber(['a', 'c']); // set key { a: true, b: false, c: true }\nresolveEvenNumber(['a', 'b', 'c']); // set key { a: true, b: true, c: true }\nresolveEvenNumber(['ab', 'c']); // set key { a: true, b: true, c: true }\nresolveEvenNumber({}); // default value { a: 'unknown', b: 'unknown', c: 'unknown' }\nresolveEvenNumber({ default: true }); // overridden value { a: true, b: true, c: true }\nresolveEvenNumber({ default: 15 }); // Throws because 15 doesn't pass the test\nresolveEvenNumber({ default: 'yes', a: true }); // overridden + set value { a: true, b: 'yes', c: 'yes' }\n// ... you get the idea...\n```\n\n### *function* `createValueResolver`\n\nCreates a `potential resolver` function that resolves to the `input value` if it satisfies `isValidValue` function, or `defaultValue` if input is `null` or `undefined`. It returns `undefined` otherwise.\n\n```typescript\nfunction createValueResolver\u003cK extends string, V\u003e(\n  keys: readonly K[],\n  isValidValue: TypeCheckFunction\u003cV\u003e,\n  defaultValue: V,\n): PotentialResolver\u003cK, V\u003e;\n```\n\n* *Arguments*\n  * `keys`: An `array` of `string` to be used as `keys` in the final [`Resolved`](#type-resolved) object.\n  * `isValidValue`: A `function` which returns wether or not a `value` is `valid`.\n  * `defaultValue`: A `value` to be used as default in case the input value is `null` or `undefined`.\n\nSee [`TypeCheckFunction`](#type-typecheckfunction) and [`PotentialResolver`](#type-potentialresolver).\n\n### *function* `createFunctionResolver`\n\nCreates a `potential resolver` function that resolves if the `input value` is a `function`. It returns `undefined` otherwise.\n\nThe `input function` will receive the `result key` as only argument. The value returned by the `input function` will be used as value for the specific result key if it satisfies `isValidValue`, if it's `null` or `undefined`, `defaultValue` will be used instead. It will `throw` otherwise. See example below...\n\n```typescript\nfunction createFunctionResolver\u003cK extends string, V, D = V\u003e(\n  keys: readonly K[],\n  isValidValue: TypeCheckFunction\u003cV\u003e,\n  defaultValue: D,\n): PotentialResolver\u003cK, V | D\u003e;\n```\n\n* *Arguments*\n  * `keys`: An `array` of `string` to be used as `keys` in the final [`Resolved`](#type-resolved) object.\n  * `isValidValue`: A `function` which returns wether or not a `value` is `valid`.\n  * `defaultValue`: A `value` to be used as default in case the input function returns `null` or `undefined`.\n\n* *Example*\n\n```typescript\nconst resolve = createFunctionResolver(['a', 'b', 'c'], isNumber, 'none');\n\nresolve((key) =\u003e key === 'b' ? 40 : 10); // resolves to { a: 10, b: 40, c: 10 }\nresolve((key) =\u003e key === 'a' ? null : 33); // resolves to { a: 'none', b: 33, c: 33 }\n\n// function returning an invalid value will throw\nresolve((key) =\u003e 40) // throws\n\n// non function inputs will be ignored by this resolver\nresolve(40) // resolves to undefined\n```\n\nSee [`TypeCheckFunction`](#type-typecheckfunction) and [`PotentialResolver`](#type-potentialresolver).\n\n### *function* `createKeyResolver`\n\nCreates a `potential resolver` function that resolves if the `input value` is a `string` and is present in `keys` `array`, or if it is one of the `special` object keys. It also resolves if input follow the [`PositiveKey`](#type-positivekey) or [`NegativeKey`](#type-negativekey) format. It returns undefined otherwise.\n\nThis `key` determines the result. A positive `key` means \"only that key\". A negative `key` means \"all other keys but that one\". See example.\n\n```typescript\nfunction createKeyResolver\u003cK extends string, S extends string\u003e(\n  keys: KeyList\u003cK\u003e,\n  special?: SpecialKeys\u003cS, K\u003e | null | undefined,\n): PotentialResolver\u003cK, boolean\u003e;\n```\n\n* *Arguments*\n  * `keys`: An `array` of `string` to be used as `keys` in the final [`Resolved`](#type-resolved) object. They will also be used to validate `positive keys` or `negative keys`.\n  * `special`: An optional object mapping `special keys` to multiple `regular keys`. These `special keys` can also be used as `positive keys` or `negative keys`.\n\nSee [`KeyList`](#type-keylist), [`SpecialKeys`](#type-specialkeys) and [`PotentialResolver`](#type-potentialresolver).\n\n* *Example*\n\n```typescript\nconst resolve = createKeyResolver\u003c'a' | 'b' | 'c', 'd'\u003e(\n  ['a', 'b', 'c'],\n  { d: ['a', 'c'] },\n);\n\nresolve('c'); // Resolves to { a: false, b: false, c: true }\nresolve('b'); // Resolves to { a: false, b: true, c: false }\n\nresolve('a') // Resolves to { a: true, b: false, c: false } ::: Only \"a\" is set\nresolve('!a') // Resolves to { a: false, b: true, c: true } ::: Everything but \"a\" is set\n\nresolve('d'); // Resolves to { a: true, b: false, c: true } ::: Only \"a\" \u0026 \"c\" is set\nresolve('!d'); // Resolves to { a: false, b: true, c: false } ::: Everything but \"a\" \u0026 \"c\" is set\n```\n\n### *function* `createKeyListResolver`\n\nCreates a `potential resolver` function that resolves if the `input value` is an `array` of `string` and every `string` is present in `keys` `array`, or if it is one of the `special` object keys. It also resolves if any of the `string` in the array follow the [`PositiveKey`](#type-positivekey) or [`NegativeKey`](#type-negativekey) format. It returns undefined otherwise.\n\nThe `keys` will be processed in the order they were added. The first `key` in the `array` will determine the default result, and the rest will mutate the result accordingly. A positive first `key` means \"only that key\". A negative first `key` means \"all other keys but that one\". See example.\n\n```typescript\nfunction createKeyListResolver\u003cK extends string, S extends string\u003e(\n  keys: KeyList\u003cK\u003e,\n  special?: SpecialKeys\u003cS, K\u003e | null | undefined,\n): PotentialResolver\u003cK, boolean\u003e;\n```\n\nSee [`KeyList`](#type-keylist), [`SpecialKeys`](#type-specialkeys) and [`PotentialResolver`](#type-potentialresolver).\n\n* *Example*\n\n```typescript\nconst resolve = createKeyListResolver\u003c'a' | 'b' | 'c', 'd'\u003e(\n  ['a', 'b', 'c'],\n  { d: ['a', 'c'] },\n);\n\nresolve([]); // Resolves to { a: false, b: false, c: false }\nresolve(['a', 'b']); // Resolves to { a: true, b: true, c: false }\n\n// The first item sets the default\nresolve(['a']) // Resolves to { a: true, b: false, c: false } ::: Only \"a\" is set\nresolve(['!a']) // Resolves to { a: false, b: true, c: true } ::: Everything but \"a\" is set\n\n// Watch the items order!!!\n\nresolve(['a', '!a']) // Resolves to { a: false, b: false, c: false }\n// because...\n// step1 - 'a' =\u003e { a: true, b: false, c: false }\n// step2 - '!a' =\u003e { ...step1, a: false }\n// return step2\n\nresolve(['!a', 'a']) // Resolves to { a: true, b: true, c: true }\n// because...\n// step1 - '!a' =\u003e { a: false, b: true, c: true }\n// step2 - 'a' =\u003e { ...step1, a: true }\n// return step2\n\nresolve(['d', '!c']); // Resolves to { a: true, b: false, c: false }\n// because...\n// step1 = 'd' =\u003e { a: true, b: false, c: true }\n// step2 = '!c' =\u003e { ...step1, c: false }\n// return step2\n\nresolve(['!c', 'd']); // Resolves to { a: true, b: true, c: true }\n// because...\n// step1 '!c' =\u003e { a: true, b: true, c: false }\n// step2 'd' =\u003e { ...step1, a: true, c: true }\n// return step2\n```\n\n### *function* `createObjectResolver`\n\nCreates a `potential resolver` function that resolves if the `input value` is an `object` and it follows the [`ObjectOption`](#type-objectoption) format. It returns undefined otherwise.\n\n```typescript\nfunction createObjectResolver\u003cK extends string, S extends string, V, O extends string\u003e(\n  keys: KeyList\u003cK\u003e,\n  isValidValue: TypeCheckFunction\u003cV\u003e,\n  defaultValue: V,\n  overrideKey: O,\n  special?: SpecialKeys\u003cS, K\u003e | null | undefined,\n): PotentialResolver\u003cK, V\u003e;\n```\n\n* *Arguments*\n  * `keys`: An `array` of `string` to be used as `keys` in the final [`Resolved`](#type-resolved) object. They will also be used to validate input object `keys`.\n  * `isValidValue`: A `function` which returns wether or not a `value` is `valid`.\n  * `defaultValue`: A `valid` `value` to be used as default in case the value is `null` or `undefined`.\n  * `overrideKey`: A `string` to be used to detect the `override key` if the input is an `object`.\n  * `special`: An optional object mapping `special keys` to multiple `regular keys`. They can also be used as `keys` in input `object`.\n\nSee [`KeyList`](#type-keylist), [`TypeCheckFunction`](#type-typecheckfunction), [`SpecialKeys`](#type-specialkeys) and [`PotentialResolver`](#type-potentialresolver).\n\n### *function* `createResolver`\n\nCreates a resolver base on a series of `potential resolvers`. I will iterate through every `potential resolver` until one resolves and return the [`Resolved`](#type-resolved) object result. It will `throw` if no `potential resolver` resolves.\n\n```typescript\nfunction createResolver\u003cK extends string, V, I = unknown\u003e(\n  ...resolvers: Array\u003cPotentialResolver\u003cK, V\u003e\u003e\n): Resolver\u003cK, V, I\u003e;\n```\n\nSee [`PotentialResolver`](#type-potentialresolver) and [`Resolver`](#type-resolver).\n\n### *function* `createResult`\n\nCreates a [`Resolved`](#type-resolved) `object`. Used internally in every `potential resolver` function. It is exported in case you need to write your own `potential resolver` function.\n\n```typescript\nfunction createResult\u003cK extends string, V\u003e(\n  keys: KeyList\u003cK\u003e,\n  value: V,\n): Resolved\u003cK, V\u003e;\n```\n\n* *Arguments*\n  * `keys`: An `array` of `string` to be used as `keys` in the [`Resolved`](#type-resolved) object.\n  * `value`: A value to be assigned to every `key` in the [`Resolved`](#type-resolved) object.\n\nSee [`KeyList`](#type-keylist) and [`Resolved`](#type-resolved).\n\n* *Example*\n\nCreating a [`Resolved`](#type-resolved) `object`.\n\n```typescript\ncreateResult(['a', 'b', 'c'], true); // { a: true, b: true, c: true }\ncreateResult(['a', 'b', 'c'], 10); // { a: 10, b: 10, c: 10 }\n```\n\n***DEPRECATION NOTICE***\n\nThis function accepts a third argument as input object, this is deprecated now and will be removed in the future. To extend a previous result use the object spread operator or `Object.assign`.\n\n```typescript\n/** @deprecated */\nfunction createResult\u003cK extends string, V\u003e(\n  keys: KeyList\u003cK\u003e,\n  value: V,\n  input?: Resolved\u003cK, V\u003e\n): Resolved\u003cK, V\u003e;\n```\n\n* *Arguments*\n  * `keys`: An `array` of `string` to be used as `keys` in the [`Resolved`](#type-resolved) object.\n  * `value`: A value to be assigned to every `key` in the [`Resolved`](#type-resolved) object.\n  * `input`: An optional [`Resolved`](#type-resolved) object to be used as base for the new [`Resolved`](#type-resolved) object. This `input` object won't be modified, a new one will be created instead. If you pass an empty array as `keys`, the input object will be returned, unless `input` is `null` or `undefined` in which case a new empty `object` will be returned.\n\n* *Example*\n\nExtending a previously created [`Resolved`](#type-resolved) `object` (**DEPRECATED**).\n\n```typescript\nconst base = createResult(['a', 'b', 'c'], 0); // base = { a: 0, b: 0, c: 0 }\nconst result = createResult(['a', 'c'], 40, base); // { a: 40, b: 0, c: 40 }\n```\n\nTo extend a previously create result use...\n\n```typescript\nconst base = createResult(['a', 'b', 'c'], 0); // base = { a: 0, b: 0, c: 0 }\nconst override = createResult(['a', 'c'], 40); // { a: 40, c: 40 }\nconst result = { ...base, ...override }; // { a: 40, b: 0, c: 40 };\n```\n\n## Exported Types\n\n### *type* `FunctionOption`\n\nA function to be called for every `key` in the the final [`Resolved`](#type-resolved) object.\n\n```typescript\nexport type FunctionOption\u003cK extends string, V\u003e = (key: K) =\u003e V | null | undefined;\n```\n\n### *type* `SingleKeyOption`\n\n```typescript\ntype SingleKeyOption\u003cK extends string\u003e = PositiveKey\u003cK\u003e | NegativeKey\u003cK\u003e;\n```\n\nSee [`PositiveKey`](#type-positivekey) and [`NegativeKey`](#type-negativekey). Used in *type* [`KeyListOption`](#type-keylistoption) and [`KeyOption`](#type-keyoption).\n\n* *Example*\n\n```typescript\nfunction logKey(key: SingleKeyOption\u003c'a' | 'b'\u003e) {\n  console.log(key);\n}\n\nlogKey('a'); // OK\nlogKey('b'); // OK\nlogKey('!a'); // OK\nlogKey('!b'); // OK\nlogKey('-a'); // OK\nlogKey('-b'); // OK\n\nlogKey('x') // Type Error\nlogKey('z') // Type Error\nlogKey('!z') // Type Error\n```\n\n### *type* `KeyListOption`\n\n```typescript\ntype KeyListOption\u003cK extends string\u003e = KeyList\u003cSingleKeyOption\u003cK\u003e\u003e;\n```\n\nSee [`KeyList`](#type-keylist) and [`SingleKeyOption`](#type-singlekeyoption). Used in *type* [`KeyOption`](#type-keyoption).\n\n* *Example*\n\n```typescript\nfunction logKeys(keys: KeyListOption\u003c'a' | 'b'\u003e) {\n  console.log(keys);\n}\n\nlogKeys(['a']); // OK\nlogKeys(['a', '!b']); // OK\nlogKeys(['b', '-a']); // OK\nlogKeys(['a', 'b', '-a']); // OK\n\nlogKeys(['x']) // Type Error\nlogKeys(['!z']) // Type Error\nlogKeys(['x', 'a']) // Type Error\nlogKeys(['a', '!z']) // Type Error\n```\n\n### *type* `KeyOption`\n\n```typescript\ntype KeyOption\u003cK extends string\u003e = SingleKeyOption\u003cK\u003e | KeyListOption\u003cK\u003e;\n```\n\nSee [`SingleKeyOption`](#type-singlekeyoption) and [`KeyListOption`](#type-keylistoption). Used in *type* [`BoolBasedSelectiveOption`](#type-boolbasedselectiveoption).\n\n* *Example*\n\n```typescript\nfunction logOption(option: KeyOption\u003c'a' | 'b'\u003e) {\n  console.log(option);\n}\n\nlogOption('a'); // OK\nlogOption('b'); // OK\nlogOption('!a'); // OK\nlogOption('-b'); // OK\nlogOption(['-b', 'a']); // OK\nlogOption(['-b', '!a']); // OK\n\nlogOption('z') // Type Error\nlogOption('!z') // Type Error\nlogOption(['!z']) // Type Error\nlogOption(['!z', 'a']) // Type Error\n```\n\n### *type* `ObjectOption`\n\nAn object containing the keys defined by `K` | `O` and the values of `V`, `null`  or `undefined`. It will be used by the [`PotentialResolver`](#type-potentialresolver) created by [`createObjectResolver`](#function-createobjectresolver) in order to create a `result` using [`createResult`](#function-createresult).\n\n```typescript\ntype ObjectOption\u003cK extends string, V\u003e = Partial\u003cRecord\u003cK, V | null | undefined\u003e\u003e;\n```\n\n* *Generics*\n  `K`: `keys` allowed in the input `object`.\n  `V`: `value` type allowed inside input `object`.\n\nUsed in *type* [`ValueBasedSelectiveOption`](#type-valuebasedselectiveoption).\n\n* *Example*\n\n```typescript\nfunction logOption(option: ObjectOption\u003c'a' | 'b' | 'override', number\u003e) {\n  console.log(option);\n}\n\nlogOption({}); // OK\nlogOption({ override: 10 }); // OK\nlogOption({ override: 10, a: 8 }); // OK\nlogOption({ a: 45 }); // OK\nlogOption({ a: 45, b: 10 }); // OK\n\nlogOption('z') // Type Error\nlogOption([]) // Type Error\nlogOption({ override: 'string' }) // Type Error\nlogOption({ override: 'string', b: 0 }) // Type Error\nlogOption({ a: 10, b: true }) // Type Error\n```\n\n### *type* `ValueBasedSelectiveOption`\n\n```typescript\ntype ValueBasedSelectiveOption\u003cK extends string, X extends string, V\u003e =\n  | V\n  | null\n  | undefined\n  | ObjectOption\u003cK | X, V\u003e;\n```\n\n* *Generics*\n  `K`: `keys` allowed if the input is an `object`.\n  `V`: `value` type allowed as input value and inside input if it's an `object`.\n\nSee [`ObjectOption`](#type-objectoption). Used in *type* [`BoolBasedSelectiveOption`](#type-boolbasedselectiveoption) and [`ValueBasedResolver`](#type-valuebasedresolver).\n\n### *type* `BoolBasedSelectiveOption`\n\n```typescript\ntype BoolBasedSelectiveOption\u003cK extends string, S extends string, V, O extends string\u003e =\n  | KeyOption\u003cK | S\u003e\n  | ValueBasedSelectiveOption\u003cK, S | O, V | boolean\u003e;\n```\n\n* *Generics*\n  `K`: `keys` allowed as `positive keys` or `negative keys`, and as keys if input is an `object`.\n  `V`: `value` type allowed as input value and inside input if it's an `object`. It will also allow `boolean` as valid value.\n  `O`: `key` name allowed for the `override` key if input is an `object`.\n\nSee [`KeyOption`](#type-keyoption) and [`ValueBasedSelectiveOption`](#type-valuebasedselectiveoption). Used in *type* [`BoolBasedResolver`](#type-boolbasedresolver).\n\n### *type* `KeyList`\n\nAn immutable list of keys.\n\n```typescript\ntype KeyList\u003cK\u003e = readonly K[];\n```\n\nUsed in *function* [`createKeyResolver`](#function-createkeyresolver), [`createKeyListResolver`](#function-createkeylistresolver), [`createObjectResolver`](#function-createobjectresolver), [`createResult`](#function-createresult) and *type* [`SpecialKeys`](#type-specialkeys).\n\n### *type* `SpecialKeys`\n\nAn object mapping `special keys` to a list of `regular keys`.\n\n```typescript\ntype SpecialKeys\u003cS extends string, K extends string\u003e = Readonly\u003cRecord\u003cS, KeyList\u003cK\u003e\u003e\u003e;\n```\n\nSee type [`KeyList`](#type-keylist). Used in *function* [`createValueBasedResolver`](#function-createvaluebasedresolver), [`createBoolBasedResolver`](#function-createboolbasedresolver), [`createKeyResolver`](#function-createkeyresolver), [`createKeyListResolver`](#function-createkeylistresolver) and [`createObjectResolver`](#function-createobjectresolver).\n\n* *Example*\n\n```typescript\ntype Country = 'american' | 'japanese';\ntype Car = 'chevrolet' | 'toyota' | 'suzuki' | 'ford';\n\nconst special: SpecialKeys\u003cCountry, Car\u003e = {\n  american: ['ford', 'chevrolet'],\n  japanese: ['toyota', 'suzuki'],\n};\n```\n\n### *type* `Resolved`\n\n```typescript\ntype Resolved\u003cK extends string, V\u003e = Readonly\u003cRecord\u003cK, V\u003e\u003e;\n```\n\nUsed in *function* [`createResult`](#function-createresult) and *type* [`PotentialResolver`](#type-potentialresolver), [`Resolver`](#type-resolver).\n\n### *type* `PotentiallyResolved`\n\n```typescript\ntype PotentiallyResolved\u003cK extends string, V\u003e = Resolved\u003cK, V\u003e | null | undefined;\n```\n\nUsed in *type* [`PotentialResolver`](#type-potentialresolver).\n\n### *type* `PotentialResolver`\n\n```typescript\ntype PotentialResolver\u003cK extends string, V\u003e = (input: unknown) =\u003e PotentiallyResolved\u003cK, V\u003e | void;\n```\n\nSee [`PotentiallyResolved`](#type-potentiallyresolved). Used in *function* [`createValueResolver`](#function-createvalueresolver), [`createFunctionResolver`](#function-createfunctionresolver), [`createKeyResolver`](#function-createkeyresolver), [`createKeyListResolver`](#function-createkeylistresolver), [`createObjectResolver`](#function-createobjectresolver) and [`createResolver`](#function-createresolver).\n\n### *type* `Resolver`\n\n```typescript\ntype Resolver\u003cK extends string, V, I\u003e = (input: I) =\u003e Resolved\u003cK, V\u003e;\n```\n\nSee [`Resolved`](#type-resolved). Used in *function* [`createResolver`](#function-createresolver) and *type* [`ValueBasedResolver`](#type-valuebasedresolver) and [`BoolBasedResolver`](#type-boolbasedresolver).\n\n### *type* `ValueBasedResolver`\n\n```typescript\ntype ValueBasedResolver\u003cK extends string, X extends string, V, D = V\u003e = Resolver\u003cK, V | D, ValueBasedSelectiveOption\u003cK, X, V\u003e\u003e;\n```\n\nSee [`Resolver`](#type-resolver) and [`ValueBasedSelectiveOption`](#type-valuebasedselectiveoption). Used in *function* [`createValueBasedResolver`](#function-createvaluebasedresolver).\n\n### *type* `BoolBasedResolver`\n\n```typescript\ntype BoolBasedResolver\u003cK extends string, S extends string, V, O extends string, D = V\u003e = Resolver\u003cK, V | D | boolean, BoolBasedSelectiveOption\u003cK, S, V, O\u003e\u003e;\n```\n\nSee [`Resolver`](#type-resolver) and [`BoolBasedSelectiveOption`](#type-boolbasedselectiveoption). Used in *function* [`createBoolBasedResolver`](#function-createboolbasedresolver).\n\n## Other Types\n\nThese are types which are not exported but help to understand some of the exported types.\n\n### *type* `PositiveKey`\n\nA `key` name or a `key` name prefixed with a `+` sign.\n\n```typescript\ntype PositiveKey\u003cK extends string\u003e = K | `+${K}`;\n```\n\nUsed in *type* [`SingleKeyOption`](#type-singlekeyoption).\n\n### *type* `NegativeKey`\n\nA `key` name prefixed with a `!` or `-` sign.\n\n```typescript\ntype NegativeKey\u003cK extends string\u003e = `!${K}` | `-${K}`;\n```\n\nUsed in *type* [`SingleKeyOption`](#type-singlekeyoption).\n\n### *type* `TypeCheckFunction`\n\n```typescript\ntype TypeCheckFunction\u003cV\u003e = (input: unknown) =\u003e input is V;\n```\n\nUsed in *function* [`createValueBasedResolver`](#function-createvaluebasedresolver), [`createBoolBasedResolver`](#function-createboolbasedresolver), [`createValueResolver`](#function-createvalueresolver), [`createFunctionResolver`](#function-createfunctionresolver) and [`createObjectResolver`](#function-createobjectresolver).\n\n* *Example*\n\n```typescript\nconst isString: TypeCheckFunction\u003cstring\u003e = (input) =\u003e {\n  return typeof input === 'string';\n};\n\nconst isRGB: TypeCheckFunction\u003c'red' | 'green' | 'blue'\u003e = (input) =\u003e {\n  return ['red', 'green', 'blue'].includes(input as never);\n}\n```\n\n## License\n\n[MIT](LICENSE) \u0026copy; 2020-2024 [Manuel Fernández](https://github.com/manferlo81) (@manferlo81)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmanferlo81%2Fselective-option","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmanferlo81%2Fselective-option","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmanferlo81%2Fselective-option/lists"}