{"id":13524127,"url":"https://github.com/jonschlinkert/whence","last_synced_at":"2026-02-14T03:17:13.702Z","repository":{"id":65990738,"uuid":"404023004","full_name":"jonschlinkert/whence","owner":"jonschlinkert","description":"Add context awareness to your apps and frameworks by safely evaluating user-defined conditional expressions. Useful for evaluating expressions in config files, prompts, key bindings, completions, templates, and many other user cases.","archived":false,"fork":false,"pushed_at":"2025-02-07T06:00:49.000Z","size":74,"stargazers_count":48,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-09-11T18:36:01.435Z","etag":null,"topics":["clause","compare","comparision","condition","conditional","docker","eval","evaluation","evalutate","filter","if-else","template","templates","when","where","workflow"],"latest_commit_sha":null,"homepage":"https://github.com/jonschlinkert","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/jonschlinkert.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/funding.yml","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},"funding":{"github":"jonschlinkert"}},"created_at":"2021-09-07T15:06:54.000Z","updated_at":"2025-09-07T03:30:37.000Z","dependencies_parsed_at":"2025-03-22T15:01:24.239Z","dependency_job_id":"534a43a1-15eb-4cba-abbd-842aefbcb917","html_url":"https://github.com/jonschlinkert/whence","commit_stats":{"total_commits":11,"total_committers":2,"mean_commits":5.5,"dds":0.2727272727272727,"last_synced_commit":"7077ced3395b6b2814d93c40fc1e295741603789"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/jonschlinkert/whence","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fwhence","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fwhence/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fwhence/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fwhence/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonschlinkert","download_url":"https://codeload.github.com/jonschlinkert/whence/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonschlinkert%2Fwhence/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29433304,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-14T02:20:56.896Z","status":"ssl_error","status_checked_at":"2026-02-14T02:11:29.478Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["clause","compare","comparision","condition","conditional","docker","eval","evaluation","evalutate","filter","if-else","template","templates","when","where","workflow"],"created_at":"2024-08-01T06:01:07.215Z","updated_at":"2026-02-14T03:17:13.680Z","avatar_url":"https://github.com/jonschlinkert.png","language":"JavaScript","readme":"# whence [![NPM version](https://img.shields.io/npm/v/whence.svg?style=flat)](https://www.npmjs.com/package/whence) [![NPM monthly downloads](https://img.shields.io/npm/dm/whence.svg?style=flat)](https://npmjs.org/package/whence) [![NPM total downloads](https://img.shields.io/npm/dt/whence.svg?style=flat)](https://npmjs.org/package/whence)  [![Tests](https://github.com/jonschlinkert/whence/actions/workflows/test.yml/badge.svg)](https://github.com/jonschlinkert/whence/actions/workflows/test.yml)\n\n\u003e Add context awareness to your apps and frameworks by safely evaluating user-defined conditional expressions. Useful for evaluating expressions in config files, prompts, key bindings, completions, templates, and many other user cases.\n\nPlease consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.\n\n## Install\n\nInstall with [npm](https://www.npmjs.com/):\n\n```sh\n$ npm install --save whence\n```\n\n## What is whence?\n\nThis libarary doest returneth true if thine 'when' clause doest matcheth the granted context object.\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eSeriously though, what does this library do?\u003c/strong\u003e\u003c/summary\u003e\n\nWhence uses [eval-estree-expression](https://github.com/jonschlinkert/eval-estree-expression) to _safely evaluate_ user-defined conditional expressions, sometimes referred to as \"when\" clauses.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eWhy do I need this?\u003c/strong\u003e\u003c/summary\u003e\n\nAdd _context awareness_ to your apps and frameworks.\n\nConditional expressions are useful in config files, creating prompts, determining key bindings, filtering suggestions and completions, variables in templates and snippets, and many other user cases.\n\nIt's even more useful when those conditional expressions can be evaluated safely.\n\n**Example: configuration files**\n\nFor example, when authoring configuration files for workflows, pipelines, builds, and so on, it's common for developers to define expressions with conditionals to determine if or when a job, task, or step should run based on environment variables, etc. These configurations are typically defined using YAML, JSON or a similar data format, which means that conditional expressions must be written as strings, booleans, or numbers. Whence makes it safe and easy to evaluate these expressions.\n\n**Other use cases**\n\n* Templates and snippets - Use whence to conditionally render files, sections, or variables\n* Completions and suggestions - Use whence to filter completions and suggestions in your text editor or prompt system\n* Key bindings - [VS Code](https://code.visualstudio.com/api/references/when-clause-contexts) and other text editors use `when` clauses or something similar to determine the keybindings to use when a key is pressed.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eHow safe is it?\u003c/strong\u003e\u003c/summary\u003e\n\nNo assignment operators, functions, or function calls are allowed by default to make it as safe as possible to evaluate user-defined expressions. To accomplish this, `whence` uses the [eval-estree-expression](https://github.com/jonschlinkert/eval-estree-expression) library, which takes an [estree](https://github.com/estree/estree) expression from [@babel/parser][], [esprima](http://esprima.org), [acorn](https://github.com/acornjs/acorn), or any similar library that parses and returns a valid `estree` expression.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eWhy another \"eval\" library?\u003c/strong\u003e\u003c/summary\u003e\n\n**What we found**\n\nEvery other eval library I found had one of the following shortcomings:\n\n* Uses `eval` or Node's `vm` or something similar to evaluate code. This is to risky, or too heavy for our use cases.\n* Functions are either the primary use case or are supported by default. We don't want users to be able to define functions in their config files.\n* Naive attempts to sanitize code before evaluating it\n* Brittle, incomplete, hand-rolled parsers\n\n**What whence does differently**\n\n* Whence takes a valid [estree][] AST for an _expression_, not statements, functions, etc.\n* Although functions are not supported by default, you can enable support if you really need it (see the [eval-estree-expression](https://github.com/jonschlinkert/eval-estree-expression) docs for more details)\n* Special care was taken in [eval-estree-expression](https://github.com/jonschlinkert/eval-estree-expression) to disallow assignment operators, functions, or other potentially malicious code, like setting `__proto__`, `constructor`, `prototype`, or `undefined` as property names on nested properties.\n\n\u003c/details\u003e\n\n## Usage\n\n```js\nconst whence = require('whence');\n\n// async usage\nconsole.log(await whence('name =~ /^d.*b$/', { name: 'doowb' })); //=\u003e true\nconsole.log(await whence('amount \u003e 100', { amount: 101 })); //=\u003e true\nconsole.log(await whence('a \u003c b \u0026\u0026 c \u003e d', { a: 0, b: 1, c: 3, d: 2 })); //=\u003e true\nconsole.log(await whence('platform === \"darwin\"', { platform: process.platform })); //=\u003e true if macOS\nconsole.log(await whence('platform === \"darwin\"', { platform: 'win32' })); //=\u003e false\n\n// sync usage\nconsole.log(whence.sync('name =~ /^d.*b$/', { name: 'doowb' })); //=\u003e true\nconsole.log(whence.sync('amount \u003e 100', { amount: 101 })); //=\u003e true\nconsole.log(whence.sync('a \u003c b \u0026\u0026 c \u003e d', { a: 0, b: 1, c: 3, d: 2 })); //=\u003e true\nconsole.log(whence.sync('platform === \"darwin\"', { platform: process.platform })); //=\u003e true if macOS\nconsole.log(whence.sync('platform === \"darwin\"', { platform: 'win32' })); //=\u003e false\n```\n\nSee [eval-estree-expression](https://github.com/jonschlinkert/eval-estree-expression) and that project's unit tests for many more examples of the types of expressions that are supported.\n\n## How whence works\n\nWhence's default behavior (and purpose) is to return a boolean. Most implementors will be interested in this library for that reason. However, if you need the evaluated result and do not want values to be cast to booleans, you should probably use [eval-estree-expression](https://github.com/jonschlinkert/eval-estree-expression) directly. For example:\n\n```js\n// whence behavior\nconsole.log(whence.sync('1 + 9')); //=\u003e true\n\n// eval-estree-expression behavior\nconsole.log(whence.sync('1 + 9')); //=\u003e 10\n```\n\n## API\n\n### [equal](index.js#L38)\n\nReturns true if the given value is truthy, or the `value` (\"left\") is\nequal to or contained within the `context` (\"right\") value. This method is\nused by the `whence()` function (the main export), but you can use this\nmethod directly if you don't want the values to be evaluated.\n\n**Params**\n\n* `value` **{any}**: The value to test.\n* `context` **{Object}**: The value to compare against.\n* `parent` **{[type](https://github.com/medikoo/type)}**\n* `returns` **{Boolean}**: Returns true or false.\n\n### [parse](index.js#L138)\n\nParses the given expression string with [@babel/parser][] and returns and AST. You may also an [estree][]-compatible expression AST.\n\n**Params**\n\n* `source` **{String}**: Expression string or an [estree][]-compatible expression AST.\n* `options` **{Object}**\n* `returns` **{Object}**\n\n**Example**\n\n```js\nconst { parse } = require('whence');\n\nconsole.log(parse('platform === \"darwin\"'));\n// Resuls in something like this:\n// Node {\n//   type: 'BinaryExpression',\n//   value: Node { type: 'Identifier', name: 'platform' },\n//   operator: '===',\n//   context: Node {\n//     type: 'StringLiteral',\n//     extra: { rawValue: 'darwin', raw: '\"darwin\"' },\n//     value: 'darwin'\n//   }\n// }\n```\n\n### [whence](index.js#L171)\n\nAsynchronously evaluates the given expression and returns a boolean.\n\n**Params**\n\n* `source` **{String|Object}**: Expression string or an [estree][]-compatible expression AST.\n* `context` **{Object}**\n* `options` **{Object}**\n* `returns` **{Boolean}**\n\n**Example**\n\n```js\nconst whence = require('whence');\n\nconsole.log(await whence('10 \u003c 20')); //=\u003e true\nconsole.log(whence.sync('10 \u003c 20')); //=\u003e true\n```\n\n### [whenceSync](index.js#L204)\n\nSynchronous version of [whence](#whence). Aliased as `whence.sync()`.\n\n**Params**\n\n* `source` **{String|Object}**: Expression string or an [estree][]-compatible expression AST.\n* `context` **{Object}**\n* `options` **{Object}**\n* `returns` **{Boolean}**\n\n**Example**\n\n```js\nconst { whenceSync } = require('whence');\n\nconsole.log(whenceSync('10 \u003c 20')); //=\u003e true\n```\n\n### [compile](index.js#L238)\n\nCompiles the given expression and returns an async function.\n\n**Params**\n\n* `source` **{String|Object}**: Expression string or an [estree][]-compatible expression AST.\n* `options` **{Object}**\n* `returns` **{Function}**: Returns a function that takes a `context` object.\n\n**Example**\n\n```js\nconst { compile } = require('whence');\nconst fn = compile('type === \"foo\"');\n\nconsole.log(await fn({ type: 'foo' })); //=\u003e true\nconsole.log(await fn({ type: 'bar' })); //=\u003e false\n```\n\n### [compileSync](index.js#L264)\n\nSynchronous version of [compile](#compile). This method is also alias as `.compile.sync()`.\n\n**Params**\n\n* `source` **{String|Object}**: Expression string or an [estree][]-compatible expression AST.\n* `options` **{Object}**\n* `returns` **{Function}**: Returns a function that takes a `context` object.\n\n**Example**\n\n```js\nconst { compile } = require('whence');\nconst fn = compile.sync('type === \"foo\"');\n\nconsole.log(fn({ type: 'foo' })); //=\u003e true\nconsole.log(fn({ type: 'bar' })); //=\u003e false\n```\n\n## Options\n\nSupports all options from [eval-estree-expression](https://github.com/jonschlinkert/eval-estree-expression).\n\n### functions\n\nAlthough whence doesn't like functions...\n\n```js\nconsole.log(whence.sync('/[a-c]+/.test(foo)', { foo: 'bbb' })); //=\u003e throws an error\n```\n\nYou can talk whence into evaluating them by setting the `functions` option to true.\n\n```js\nconsole.log(whence.sync('/[a-c]+/.test(foo)', { foo: 'bbb' }, { functions: true })); //=\u003e true\nconsole.log(whence.sync('/[a-c]+/.test(foo)', { foo: 'zzz' }, { functions: true })); //=\u003e false\n```\n\n## Examples\n\n## About\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eContributing\u003c/strong\u003e\u003c/summary\u003e\n\nPull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eRunning Tests\u003c/strong\u003e\u003c/summary\u003e\n\nRunning and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:\n\n```sh\n$ npm install \u0026\u0026 npm test\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e\u003cstrong\u003eBuilding docs\u003c/strong\u003e\u003c/summary\u003e\n\n_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_\n\nTo generate the readme, run the following command:\n\n```sh\n$ npm install -g verbose/verb#dev verb-generate-readme \u0026\u0026 verb\n```\n\n\u003c/details\u003e\n\n### Author\n\n**Jon Schlinkert**\n\n* [GitHub Profile](https://github.com/jonschlinkert)\n* [Twitter Profile](https://twitter.com/jonschlinkert)\n* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)\n\n### License\n\nCopyright © 2025, [Jon Schlinkert](https://github.com/jonschlinkert).\nReleased under the [MIT License](LICENSE).\n\n***\n\n_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on February 07, 2025._\n","funding_links":["https://github.com/sponsors/jonschlinkert"],"categories":["JavaScript","Packages"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonschlinkert%2Fwhence","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonschlinkert%2Fwhence","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonschlinkert%2Fwhence/lists"}