{"id":31831223,"url":"https://github.com/livingdocsio/value-expression","last_synced_at":"2025-10-11T21:48:18.129Z","repository":{"id":68391073,"uuid":"592962451","full_name":"livingdocsIO/value-expression","owner":"livingdocsIO","description":"Safely evaluate javaScript-like code snippets to produce a single value.","archived":false,"fork":false,"pushed_at":"2023-12-15T14:33:27.000Z","size":80,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":5,"default_branch":"main","last_synced_at":"2024-04-14T11:56:03.437Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/livingdocsIO.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}},"created_at":"2023-01-24T22:42:45.000Z","updated_at":"2024-04-14T11:56:03.438Z","dependencies_parsed_at":null,"dependency_job_id":"b325acbb-f4be-4f11-ad75-61d8409fe187","html_url":"https://github.com/livingdocsIO/value-expression","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/livingdocsIO/value-expression","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/livingdocsIO%2Fvalue-expression","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/livingdocsIO%2Fvalue-expression/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/livingdocsIO%2Fvalue-expression/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/livingdocsIO%2Fvalue-expression/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/livingdocsIO","download_url":"https://codeload.github.com/livingdocsIO/value-expression/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/livingdocsIO%2Fvalue-expression/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279008824,"owners_count":26084517,"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","status":"online","status_checked_at":"2025-10-11T02:00:06.511Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":[],"created_at":"2025-10-11T21:48:17.169Z","updated_at":"2025-10-11T21:48:18.123Z","avatar_url":"https://github.com/livingdocsIO.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"right\"\u003e\n  \u003cimg src=\"https://img.shields.io/badge/coverage-100%25-success\"\u003e\n  \u003cimg src=\"https://img.shields.io/badge/no-dependencies-success\"\u003e\n\u003c/p\u003e\n\n# Value Expression\n\n## What can it do for you?\n\nEvaluate javaScript-like code snippets to produce a single value.\n\nSafely evaluate a Javascript-like expression to produce a single value. This library is inspired by angular expressions and their use of pipes to transform\nvalues safely from user provided code snippets.\nWithin expression you can use context variables and predefined functions.\n\nThis allows for value-expressions to be used in declarative configurations\nto transform values.\n\nNo eval or new Function is used to evaluate the expression. The goal is that it is\nsafe to evaluate user-provided snippets.\n\n\n## Introduction\n\n```js\n// create an expression instance\nconst {parseTemplate} = valueExpression()\n\n// A simple expression evaluating a name from variables\nconst expression = parseTemplate(`prename + \" \" + surname`)\nexpression({prename: 'Mac', surname: 'Gyver'}) // =\u003e Mac Gyver\n```\n\n## Supported Features\n\n\n### Numbers\n\n```js\nconst expression = parseTemplate(`1 + 2 * 3`)\nexpression() // =\u003e 7\n```\n\n### Strings\n\n```js\nconst expression = parseTemplate(`\"Hello \" + \"World\"`)\nexpression() // =\u003e \"Hello World\"\n\n// Adding a number to a string behaves the same as in javascript\nconst expression = parseTemplate(`\"Say \" + 3`)\nexpression() // =\u003e \"Say 3\"\n```\n\n### Booleans\n\n```js\nconst expression = parseTemplate(`true || false`)\nexpression() // =\u003e true\n```\n\n### Variables\n\n```js\nconst expression = parseTemplate(`foo`)\nexpression({foo: 'Hey There'}) // =\u003e Hey There\n```\n\n```js\nconst expression = parseTemplate(`metadata.prename + \" \" + metadata.surname`)\ncontext = {\n  metadata: {\n    prename: 'Mac',\n    surname: 'Gyver'\n  }\n}\nexpression(context) // =\u003e Mac Gyver\n```\n\nVariable evaluation is forgiving. If no variable is defined it will\nevaluate to `undefined`.\n\n```js\nconst expression = parseTemplate(`foo`)\nexpression({bar: 'Hey There'}) // =\u003e undefined\n```\n\n### Functions\n\nRegistering functions\n```js\nconst {registerFunction, parseTemplate} = valueExpression()\n\n// Register functions available in templates\n// Note: functions need to be registered before `parseTemplate()` is called.\nregisterFunction('sayHello', (noun = 'World') =\u003e `Hello ${noun}`)\n\n// Call without any arguments\nparseTemplate(`sayHello()`)() // =\u003e \"Hello World\"\n\n// Call with a string argument\nparseTemplate(`sayHello('Kitty')`)() // =\u003e \"Hello Kitty\"\n\n// Call with a variable\nparseTemplate(`sayHello(noun)`)({noun: 'Sweetie'}) // =\u003e \"Hello Sweetie\"\n```\n\n### Pipes\n\n```js\nconst {registerFunction, parseTemplate} = valueExpression()\n\n// Every registered function can be called with the pipe syntax.\n// The previous value will be passed as it first argument.\nregisterFunction('sayHello', (noun = 'World', greeting = 'Hello') =\u003e `${greeting} ${noun}`)\n\n\n// Call with a string argument\nparseTemplate(`'Kitty' | sayHello`)() // =\u003e \"Hello Kitty\"\n\n// Note: this is equivalent to the syntax above without parens\nparseTemplate(`'Kitty' | sayHello()`)() // =\u003e \"Hello Kitty\"\n\n// Call a piped function with a variable\nparseTemplate(`'Kitty' | sayHello(greeting)`)({greeting: 'Hey there'}) // =\u003e \"Hey there Sweetie\"\n```\n\n## String Expressions\n\nThere is a special type of expression called string expression which is\nalways evaluated to a string. They are created by parsing the template\nwith the method `parseStringTemplate()`. To run one or multiple expressions within\na string expression you have to use double curly braces: e.g. `{{ foo() }}`.\n\nThe expressions within string templates work the same as normal expressions.\n\n```js\nconst {parseStringTemplate} = valueExpression()\n\nparseStringTemplate(`Hello World`)() // =\u003e \"Hello World\"\nparseStringTemplate(`Say {{ name }}`)({name: 'my name'}) // =\u003e \"Say my name\"\nparseStringTemplate(`Say {{ 2 + 2 | sqrt }}`)() // =\u003e \"Say 2\"\n```\n\n### Supported Operators\n\nAll operators have the same behavior as their javascript equivalents.\n\n```js\nconst supportedOperators = [\n  \"+\",   // addition\n  \"-\",   // subtraction\n  \"*\",   // multiplication\n  \"/\",   // division\n  \"%\",   // remainder\n  \"\u003e\",   // greaterThan\n  \"\u003e=\",  // greaterOrEqualThan\n  \"\u003c\",   // lesserThan\n  \"\u003c=\",  // lesserOrEqualThan\n  \"===\", // strictEqual\n  \"!==\", // strictNotEqual\n  \"\u0026\u0026\",  // and\n  \"||\",  // or\n]\n```\n\nNote: `!` Negation, and paranthesis `(` `)` for grouping\nand ternary expressions are not currently supported.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flivingdocsio%2Fvalue-expression","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flivingdocsio%2Fvalue-expression","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flivingdocsio%2Fvalue-expression/lists"}