Gathering detailed insights and metrics for whence
Gathering detailed insights and metrics for whence
Gathering detailed insights and metrics for whence
Gathering detailed insights and metrics for whence
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.
npm install whence
Typescript
Module System
Min. Node Version
Node Version
NPM Version
95.7
Supply Chain
100
Quality
77.3
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
3,622,798
Last Day
16,114
Last Week
77,429
Last Month
319,564
Last Year
2,740,552
MIT License
43 Stars
14 Commits
1 Forks
3 Watchers
1 Branches
1 Contributors
Updated on Apr 15, 2025
Minified
Minified + Gzipped
Latest Version
2.0.2
Package Id
whence@2.0.2
Unpacked Size
21.67 kB
Size
6.30 kB
File Count
4
NPM Version
10.9.2
Node Version
22.13.0
Published on
Feb 07, 2025
Cumulative downloads
Total Downloads
Last Day
57.3%
16,114
Compared to previous day
Last Week
8.4%
77,429
Compared to previous week
Last Month
0.6%
319,564
Compared to previous month
Last Year
227.5%
2,740,552
Compared to previous year
2
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.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.
Install with npm:
1$ npm install --save whence
This libarary doest returneth true if thine 'when' clause doest matcheth the granted context object.
Whence uses eval-estree-expression to safely evaluate user-defined conditional expressions, sometimes referred to as "when" clauses.
Add context awareness to your apps and frameworks.
Conditional 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.
It's even more useful when those conditional expressions can be evaluated safely.
Example: configuration files
For 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.
Other use cases
when
clauses or something similar to determine the keybindings to use when a key is pressed.No 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 library, which takes an estree expression from [@babel/parser][], esprima, acorn, or any similar library that parses and returns a valid estree
expression.
What we found
Every other eval library I found had one of the following shortcomings:
eval
or Node's vm
or something similar to evaluate code. This is to risky, or too heavy for our use cases.What whence does differently
__proto__
, constructor
, prototype
, or undefined
as property names on nested properties.1const whence = require('whence'); 2 3// async usage 4console.log(await whence('name =~ /^d.*b$/', { name: 'doowb' })); //=> true 5console.log(await whence('amount > 100', { amount: 101 })); //=> true 6console.log(await whence('a < b && c > d', { a: 0, b: 1, c: 3, d: 2 })); //=> true 7console.log(await whence('platform === "darwin"', { platform: process.platform })); //=> true if macOS 8console.log(await whence('platform === "darwin"', { platform: 'win32' })); //=> false 9 10// sync usage 11console.log(whence.sync('name =~ /^d.*b$/', { name: 'doowb' })); //=> true 12console.log(whence.sync('amount > 100', { amount: 101 })); //=> true 13console.log(whence.sync('a < b && c > d', { a: 0, b: 1, c: 3, d: 2 })); //=> true 14console.log(whence.sync('platform === "darwin"', { platform: process.platform })); //=> true if macOS 15console.log(whence.sync('platform === "darwin"', { platform: 'win32' })); //=> false
See eval-estree-expression and that project's unit tests for many more examples of the types of expressions that are supported.
Whence'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 directly. For example:
1// whence behavior 2console.log(whence.sync('1 + 9')); //=> true 3 4// eval-estree-expression behavior 5console.log(whence.sync('1 + 9')); //=> 10
Returns true if the given value is truthy, or the value
("left") is
equal to or contained within the context
("right") value. This method is
used by the whence()
function (the main export), but you can use this
method directly if you don't want the values to be evaluated.
Params
value
{any}: The value to test.context
{Object}: The value to compare against.parent
{type}returns
{Boolean}: Returns true or false.Parses the given expression string with [@babel/parser][] and returns and AST. You may also an [estree][]-compatible expression AST.
Params
source
{String}: Expression string or an [estree][]-compatible expression AST.options
{Object}returns
{Object}Example
1const { parse } = require('whence'); 2 3console.log(parse('platform === "darwin"')); 4// Resuls in something like this: 5// Node { 6// type: 'BinaryExpression', 7// value: Node { type: 'Identifier', name: 'platform' }, 8// operator: '===', 9// context: Node { 10// type: 'StringLiteral', 11// extra: { rawValue: 'darwin', raw: '"darwin"' }, 12// value: 'darwin' 13// } 14// }
Asynchronously evaluates the given expression and returns a boolean.
Params
source
{String|Object}: Expression string or an [estree][]-compatible expression AST.context
{Object}options
{Object}returns
{Boolean}Example
1const whence = require('whence'); 2 3console.log(await whence('10 < 20')); //=> true 4console.log(whence.sync('10 < 20')); //=> true
Synchronous version of whence. Aliased as whence.sync()
.
Params
source
{String|Object}: Expression string or an [estree][]-compatible expression AST.context
{Object}options
{Object}returns
{Boolean}Example
1const { whenceSync } = require('whence'); 2 3console.log(whenceSync('10 < 20')); //=> true
Compiles the given expression and returns an async function.
Params
source
{String|Object}: Expression string or an [estree][]-compatible expression AST.options
{Object}returns
{Function}: Returns a function that takes a context
object.Example
1const { compile } = require('whence'); 2const fn = compile('type === "foo"'); 3 4console.log(await fn({ type: 'foo' })); //=> true 5console.log(await fn({ type: 'bar' })); //=> false
Synchronous version of compile. This method is also alias as .compile.sync()
.
Params
source
{String|Object}: Expression string or an [estree][]-compatible expression AST.options
{Object}returns
{Function}: Returns a function that takes a context
object.Example
1const { compile } = require('whence'); 2const fn = compile.sync('type === "foo"'); 3 4console.log(fn({ type: 'foo' })); //=> true 5console.log(fn({ type: 'bar' })); //=> false
Supports all options from eval-estree-expression.
Although whence doesn't like functions...
1console.log(whence.sync('/[a-c]+/.test(foo)', { foo: 'bbb' })); //=> throws an error
You can talk whence into evaluating them by setting the functions
option to true.
1console.log(whence.sync('/[a-c]+/.test(foo)', { foo: 'bbb' }, { functions: true })); //=> true 2console.log(whence.sync('/[a-c]+/.test(foo)', { foo: 'zzz' }, { functions: true })); //=> false
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Running 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:
1$ npm install && npm test
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
1$ npm install -g verbose/verb#dev verb-generate-readme && verb
Jon Schlinkert
Copyright © 2025, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.8.0, on February 07, 2025.
No vulnerabilities found.
No security vulnerabilities found.