{"id":18055019,"url":"https://github.com/mk-pmb/es-fallback-first-defined-value","last_synced_at":"2025-07-02T14:34:26.602Z","repository":{"id":66055218,"uuid":"81358386","full_name":"mk-pmb/es-fallback-first-defined-value","owner":"mk-pmb","description":"Suggesting a new fallback operator for ECMAScript, like || but stops at first defined (!== undefined) value.","archived":false,"fork":false,"pushed_at":"2017-10-13T22:35:54.000Z","size":39,"stargazers_count":0,"open_issues_count":3,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-17T17:45:01.359Z","etag":null,"topics":["ecmascript","ecmascript-proposal","ecmascript-syntax"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mk-pmb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2017-02-08T17:49:19.000Z","updated_at":"2017-02-08T17:57:51.000Z","dependencies_parsed_at":null,"dependency_job_id":"7ed4ca25-f8d2-4b57-b69b-eea05ce8e737","html_url":"https://github.com/mk-pmb/es-fallback-first-defined-value","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/mk-pmb%2Fes-fallback-first-defined-value","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mk-pmb%2Fes-fallback-first-defined-value/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mk-pmb%2Fes-fallback-first-defined-value/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mk-pmb%2Fes-fallback-first-defined-value/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mk-pmb","download_url":"https://codeload.github.com/mk-pmb/es-fallback-first-defined-value/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247312094,"owners_count":20918341,"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":["ecmascript","ecmascript-proposal","ecmascript-syntax"],"created_at":"2024-10-31T00:13:20.483Z","updated_at":"2025-04-05T08:41:20.182Z","avatar_url":"https://github.com/mk-pmb.png","language":"JavaScript","readme":"﻿\n\u003c!-- SSI tags powered by npm://readme-ssi --\u003e\n\nSuppose a robot that can add sugar cubes to beverages.\nIt has several methods for deciding how many sugars to add,\nso there must be some code that determines the highest-priority answer.\n\n\nThe nonsolutions\n----------------\n\nEach of these have a code demo and comments about their problems\nin [sugars.js](sugars.js):\n\n| Strategy                  | nice code   | lazy eval   | correct result¹ |\n|:------------------------- |:-----------:|:-----------:|:-----------:|\n| temporary variable        | ![☐][ck-no] | ![☑][ck-hz] | ![☑][ck-hz] |\n| just use \u0026#124;\u0026#124;     | ![☑][ck-hz] | ![☑][ck-pt] | ![☐][ck-no] |\n| \u0026#124;\u0026#124; wrap(…)      | ![☐][ck-no] | ![☑][ck-hz] | ![☑][ck-hz] |\n| [ …values ].find()        | ![☑][ck-hz] | ![☐][ck-no] | ![☑][ck-pt] |\n| [ …functions ].reduce()   | ![☐][ck-no] | ![☑][ck-hz] | ![☑][ck-hz] |\n\n¹ Without lazy eval, \"correct result\" can only be half because\ngratuitous evaluation of the later expressions might have modified\nthe (meaning of the) result or might have thrown an Error.\n\n\nNew syntax to the rescue!\n-------------------------\n\n#### First defined value:\n\n\u003c!--#include file=\"sugars.js\" start=\"  //§new-syntax\" stop=\"  //§\"\n  code=\"javascript\" --\u003e\n\u003c!--#verbatim lncnt=\"7\" --\u003e\n```javascript\n  var n = (guessFromColor(bev)\n    ?| queryHwdb(bev.idVendor, bev.idProduct)\n    ?| (cfg || false).defaultSugars\n    ?| surpriseMe(bev)\n    );\n```\n\u003c!--/include--\u003e\n\n  * Precedence like `||`\n  * Picks the next value in the chain until it encounters a value\n    that is defined (`!== undefined`).\n  * Code from [sugars.js](sugars.js). More details there.\n\n\n#### Custom decider function:\n\n\u003c!--#include file=\"sugars.js\" start=\"  //§custom-decider-func\" stop=\"  //§\"\n  code=\"javascript\" --\u003e\n\u003c!--#verbatim lncnt=\"11\" --\u003e\n```javascript\n  var n = ( ?| checkAcceptable\n    : guessFromColor(bev)\n    : n2u(queryHwdb(bev.idVendor, bev.idProduct))\n    : (cfg || false).defaultSugars\n    : surpriseMe(bev)\n    // Beware: If there was no acceptable value, you still get the last one!\n    // So better append either a default value, or a last resort:\n    : test.fail('No acceptable value!')\n    );\n```\n\u003c!--/include--\u003e\n\n  * Precedence like `… ? … : …`\n  * Picks the next value in the chain until it encounters a value\n    for which the decider function returns a truthy value.\n  * Code from [sugars.js](sugars.js). More details there.\n\n\n\nQ\u0026amp;A\n-------\n\n### Neat, where's the babel plugin?\n\nI wish I had one.\nFor now the tests use a RegExp-based pseudo-transpiler.\n\n\n### What if I want to accept `null` or `undefined` but not `false`?\n\nUse a custom decider function.\nIf it's just about `null`, vote a thumbs-up emoji on\n[issue #4](https://github.com/mk-pmb/es-fallback-first-defined-value/issues/4).\n\n  * Update: The [Null Coalescing proposal][tc39-null-coal]\n    seems to be just what you're looking for.\n\n\n### Is this a [Safe Navigation Operator][safe-nav-op]?\n\nNo. I'd really like to have an SNO in JavaScript as well,\nbut SNO is for diving deep into an object, whereas this proposal\nis about a decision chain with several unrelated values.\n\n  * Update: [It's on the horizon.][tc39-opt-chain]\n\n\n### I don't care about people who want 0 sugars.\n\nThen instead let's calclulare the amount of damage your character takes,\nor how many ads to show before a funny cat video.\n\n\n\nOptional extensions\n-------------------\n\nFeature creep galore.\n\n#### Individual criteria:\n\n\u003c!--#include file=\"flakes.js\" start=\"  //§pseudo-method-if\" stop=\"  //§\"\n  code=\"javascript\" --\u003e\n\u003c!--#verbatim lncnt=\"16\" --\u003e\n```javascript\n  (undefined        // \u003c- redundant   // [1]\n    ?|: pureWhite           cloud1()  // [2]\n    ?|: colorful            cloud2()\n    ?|: colorful            cloud3()\n    ?|  undefined   // \u003c- just to show you can mix them.\n    ?|: (codepoint.odd)     cloud4()\n    ?|: (codepoint.odd)     cloud5()\n    ?|: (codepoint.even)    cloud6()\n    ?|: ((Date.now() % 3) ? codepoint.odd : pureWhite)   cloud7()\n    ?|: (codepoint.ascii)   cloud8()  // [3]\n    ?|: dontBotherMe        cloud9()  // [4]\n    ?|: colorful            auroraBorealis()\n    ?|  { error: \"Couldn't find any. :-(\" }\n  ),\n```\n\u003c!--/include--\u003e\n\n  * Precedence of `|?:` is same as `|?`\n  * `|?:` works mostly like `|?` but with a custom criterion\n    for the (one) next candidate value.\n    * If you want to avoid the above repetitions, just open a new\n      level or parens for a chain with a custom decider function.\n  * Code from [flakes.js](flakes.js). More details there.\n\n\n#### Hindsight\n\n  * Custom decider functions get the chain's previous value as 2nd argument.\n    Details: [hindsight.js](hindsight.js)\n\n\n\n\n\n\n\n\n\u0026nbsp;\n\n  [safe-nav-op]: https://en.wikipedia.org/wiki/Safe_navigation_operator\n  [ck-hz]: https://raw.githubusercontent.com/mk-pmb/misc/master/gfm-util/img/checkmark-has.gif \"☑\"\n  [ck-up]: https://raw.githubusercontent.com/mk-pmb/misc/master/gfm-util/img/checkmark-up.gif \"⟎\"\n  [ck-pt]: https://raw.githubusercontent.com/mk-pmb/misc/master/gfm-util/img/checkmark-partial.gif \"◪\"\n  [ck-no]: https://raw.githubusercontent.com/mk-pmb/misc/master/gfm-util/img/checkmark-minus.gif \"☐\"\n  [tc39-null-coal]: https://github.com/gisenberg/proposal-null-coalescing\n  [tc39-opt-chain]: https://github.com/TC39/proposal-optional-chaining\n\n-----\n\nLicense: ISC\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmk-pmb%2Fes-fallback-first-defined-value","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmk-pmb%2Fes-fallback-first-defined-value","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmk-pmb%2Fes-fallback-first-defined-value/lists"}