{"id":24558194,"url":"https://github.com/rse/ael","last_synced_at":"2025-04-19T09:59:06.263Z","repository":{"id":57173867,"uuid":"327259998","full_name":"rse/ael","owner":"rse","description":"Advanced Expression Language","archived":false,"fork":false,"pushed_at":"2024-03-08T23:02:02.000Z","size":189,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-15T21:55:46.003Z","etag":null,"topics":["evaluation","expression","language","parser"],"latest_commit_sha":null,"homepage":"http://npmjs.com/ael","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/rse.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}},"created_at":"2021-01-06T09:18:54.000Z","updated_at":"2024-12-22T03:54:17.000Z","dependencies_parsed_at":"2022-08-24T13:20:06.151Z","dependency_job_id":null,"html_url":"https://github.com/rse/ael","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rse%2Fael","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rse%2Fael/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rse%2Fael/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rse%2Fael/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rse","download_url":"https://codeload.github.com/rse/ael/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249670098,"owners_count":21308673,"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":["evaluation","expression","language","parser"],"created_at":"2025-01-23T05:47:37.494Z","updated_at":"2025-04-19T09:59:06.243Z","avatar_url":"https://github.com/rse.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\nAEL\n===\n\n**Advanced Expression Language (AEL)**\n\n\u003cp/\u003e\n\u003cimg src=\"https://nodei.co/npm/ael.png?downloads=true\u0026stars=true\" alt=\"\"/\u003e\n\n\u003cp/\u003e\n\u003cimg src=\"https://david-dm.org/rse/ael.png\" alt=\"\"/\u003e\n\nAbout\n-----\n\nAdvanced Expression Language (AEL) is a JavaScript library for use\nin the Browser and Node.js to parse/compile and execute/evaluate\nJavaScript-style expressions. The expressions are based on sequence,\nassignment, conditional, logical, bitwise, relational, arithmetical,\nfunctional, selective and literal constructs and hence can express\narbitrary complex matchings and lookups. The result can be an arbitrary\nvalue, but usually is just a boolean one. AEL is primarily intended\nto be used for evaluating access control rules or expanding template\nvariables.\n\nInstallation\n------------\n\n```shell\n$ npm install ael\n```\n\nUsage\n-----\n\n```\n$ cat sample.js\nconst AEL = require(\"ael\")\n\nconst ael = new AEL({\n    trace: (msg) =\u003e console.log(msg)\n})\n\nconst data = {\n    session: {\n        user: {\n            login: \"rse\",\n            email: \"rse@engelschall.com\"\n        },\n        tokens: [\n            \"455c3026-50cf-11eb-8d93-7085c287160d\",\n            \"4600b07e-50cf-11eb-8d57-7085c287160d\"\n        ]\n    },\n}\n\nconst expr = `\n    grant =~ /^login:(.+)$/ ? session?.user?.login =~ $1 :\n    grant =~ /^email:(.+)$/ ? session?.user?.email =~ $1 :\n    grant =~ /^token:(.+)$/ ? session?.tokens      \u003e= $1 : false\n`\n\nconst grants = [\n    \"login:^(?:rse|foo|bar)$\",\n    \"email:^.+@engelschall\\\\.com$\",\n    \"email:^.+@example\\\\.com$\",\n    \"token:4600b07e-50cf-11eb-8d57-7085c287160d\"\n]\n\ntry {\n    let granted = false\n    for (const grant of grants) {\n        if (ael.evaluate(expr, { ...data, grant })) {\n            granted = true\n            break\n        }\n    }\n    console.log(\"GRANTED\", granted)\n}\ncatch (ex) {\n    console.log(\"ERROR\", ex.toString())\n}\n\n$ node sample.js\ncompile: +---(expression string)---------------------------------------------------------------------------------\ncompile: |\ncompile: |     grant =~ /^login:(.+)$/ ? session?.user?.login =~ $1 :\ncompile: |     grant =~ /^email:(.+)$/ ? session?.user?.email =~ $1 :\ncompile: |     grant =~ /^token:(.+)$/ ? session?.tokens      \u003e= $1 : false\ncompile: +---(abstract syntax tree)------------------------------------------------------------------------------\ncompile: | ConditionalTernary [2,5]\ncompile: | ├── Relational (op: \"=~\") [2,5]\ncompile: | │   ├── Variable (id: \"grant\") [2,5]\ncompile: | │   └── LiteralRegExp (value: /^login:(.+)$/) [2,14]\ncompile: | ├── Relational (op: \"=~\") [2,31]\ncompile: | │   ├── Select [2,31]\ncompile: | │   │   ├── Variable (id: \"session\") [2,31]\ncompile: | │   │   ├── SelectItem (optional: true) [2,38]\ncompile: | │   │   │   └── Identifier (id: \"user\") [2,40]\ncompile: | │   │   └── SelectItem (optional: true) [2,44]\ncompile: | │   │       └── Identifier (id: \"login\") [2,46]\ncompile: | │   └── Variable (id: \"$1\") [2,55]\ncompile: | └── ConditionalTernary [3,5]\ncompile: |     ├── Relational (op: \"=~\") [3,5]\ncompile: |     │   ├── Variable (id: \"grant\") [3,5]\ncompile: |     │   └── LiteralRegExp (value: /^email:(.+)$/) [3,14]\ncompile: |     ├── Relational (op: \"=~\") [3,31]\ncompile: |     │   ├── Select [3,31]\ncompile: |     │   │   ├── Variable (id: \"session\") [3,31]\ncompile: |     │   │   ├── SelectItem (optional: true) [3,38]\ncompile: |     │   │   │   └── Identifier (id: \"user\") [3,40]\ncompile: |     │   │   └── SelectItem (optional: true) [3,44]\ncompile: |     │   │       └── Identifier (id: \"email\") [3,46]\ncompile: |     │   └── Variable (id: \"$1\") [3,55]\ncompile: |     └── ConditionalTernary [4,5]\ncompile: |         ├── Relational (op: \"=~\") [4,5]\ncompile: |         │   ├── Variable (id: \"grant\") [4,5]\ncompile: |         │   └── LiteralRegExp (value: /^token:(.+)$/) [4,14]\ncompile: |         ├── Relational (op: \"\u003e=\") [4,31]\ncompile: |         │   ├── Select [4,31]\ncompile: |         │   │   ├── Variable (id: \"session\") [4,31]\ncompile: |         │   │   └── SelectItem (optional: true) [4,38]\ncompile: |         │   │       └── Identifier (id: \"tokens\") [4,40]\ncompile: |         │   └── Variable (id: \"$1\") [4,55]\ncompile: |         └── LiteralValue (value: false) [4,60]\nexecute: +---(evaluation recursion tree)-------------------------------------------------------------------------\nexecute: | ConditionalTernary {\nexecute: |     Relational {\nexecute: |         Variable {\nexecute: |         }: \"login:^(?:rse|foo|bar)$\"\nexecute: |         LiteralRegExp {\nexecute: |         }: {}\nexecute: |     }: true\nexecute: |     Relational {\nexecute: |         Select {\nexecute: |             Variable {\nexecute: |             }: {\"user\":{\"login\":\"rse\",\"email\":\"rse@enge...\nexecute: |                 Identifier {\nexecute: |                 }: \"user\"\nexecute: |                 Identifier {\nexecute: |                 }: \"login\"\nexecute: |         }: \"rse\"\nexecute: |         Variable {\nexecute: |         }: \"^(?:rse|foo|bar)$\"\nexecute: |     }: true\nexecute: | }: true\nGRANTED true\n```\n\nExpression Language\n-------------------\n\nThe following BNF-style grammar shows the supported expression language:\n\n```\n//  top-level\nexpr             ::= sequence\n                   | assignment\n                   | conditional\n                   | logical\n                   | bitwise\n                   | relational\n                   | arithmentical\n                   | functional\n                   | selective\n                   | variable\n                   | literal\n                   | parenthesis\n\n//  expressions\nsequence         ::= expr (\",\" expr)+\nassignment       ::= id \"=\" expr\nconditional      ::= expr \"?\" expr \":\" expr\n                   | expr \"??\" expr\nlogical          ::= expr (\"\u0026\u0026\" | \"||\") expr\n                   | \"!\" expr\nbitwise          ::= expr (\"\u0026\" | \"^\" | \"|\" | \"\u003c\u003c\" | \"\u003e\u003e\") expr\n                   | \"~\" expr\nrelational       ::= expr (\"==\" | \"!=\" | \"\u003c=\" | \"\u003e=\" | \"\u003c\" | \"\u003e\" | \"=~\" | \"!~\") expr\narithmethical    ::= expr (\"+\" | \"-\" | \"*\" | \"/\" | \"%\" | \"**\") expr\nfunctional       ::= expr \"?.\"? \"(\" (expr (\",\" expr)*)? \")\"\nselective        ::= expr \"?.\"? \".\" ud\n                   | expr \"?.\"? \"[\" expr \"]\"\nvariable         ::= id\nliteral          ::= array | object | template | string | regexp | number | value\nparenthesis      ::= \"(\" expr \")\"\n\n//  literals\nid               ::= /[a-zA-Z_][a-zA-Z0-9_-]*/\narray            ::= \"[\" (expr (\",\" expr)*)? \"]\"\nobject           ::= \"{\" (key \":\" expr (\",\" key \":\" expr)*)? \"}\"\nkey              ::= \"[\" expr \"]\"\n                   | id\ntemplate         ::= \"`\" (\"${\" expr \"}\" / (\"\\\\`\"|.))* \"`\"\nstring           ::= /\"(\\\\\"|.)*\"/\n                   | /'(\\\\'|.)*'/\nregexp           ::= /`(\\\\`|.)*`/\nnumber           ::= /[+-]?/ number-value\nnumber-value     ::= \"0b\" /[01]+/\n                   | \"0o\" /[0-7]+/\n                   | \"0x\" /[0-9a-fA-F]+/\n                   | /[0-9]*\\.[0-9]+([eE][+-]?[0-9]+)?/\n                   | /[0-9]+/\nvalue            ::= \"true\" | \"false\" | \"null\" | \"NaN\" | \"undefined\"\n```\n\nApplication Programming Interface (API)\n---------------------------------------\n\nThe following TypeScript definition shows the supported Application Programming Interface (API):\n\n```ts\ndeclare module \"AEL\" {\n    class AEL {\n        /*  create AEL instance  */\n        public constructor(\n            options?: {\n                cache?:    number,   /*  number of LRU-cached ASTs (default: 0)      */\n                trace?: (            /*  optional tracing callback (default: null)   */\n                    msg:   string    /*  tracing message                             */\n                ) =\u003e void\n            }\n        )\n\n        /*  individual step 1: compile (and cache) expression into AST  */\n        compile(\n            expr:          string    /*  expression string                           */\n        ): any                       /*  abstract syntax tree                        */\n\n        /*  individual step 2: execute AST  */\n        execute(\n            ast:           any,      /*  abstract syntax tree                        */\n            vars?:         object,   /*  data variables  (read-only)  (default: {})  */\n            state?:        object    /*  state variables (read-write) (default: {})  */\n        ): void\n\n        /*  all-in-one step: evaluate (compile and execute) expression  */\n        evaluate(\n            expr:          string,   /*  expression string                           */\n            vars?:         object,   /*  data variables  (read-only)  (default: {})  */\n            state?:        object    /*  state variables (read-write) (default: {})  */\n        ): any\n    }\n    export = AEL\n}\n```\n\nImplementation Notice\n---------------------\n\nAlthough AEL is written in ECMAScript 2020, it is transpiled to older\nenvironments and this way runs in really all current (as of 2021)\nJavaScript environments, of course.\n\nAdditionally, there are two transpilation results: first, there is a\ncompressed `ael.browser.js` for Browser environments. Second, there is\nan uncompressed `ael.node.js` for Node.js environments.\n\nThe Browser variant `ael.browser.js` has all external dependencies `asty`,\n`pegjs-otf`, `pegjs-util`, and `cache-lru` directly embedded. The\nNode.js variant `ael.node.js` still requires the external dependencies\n`asty`, `pegjs-otf`, `pegjs-util`, and `cache-lru`.\n\nLicense\n-------\n\nCopyright \u0026copy; 2021-2024 Dr. Ralf S. Engelschall (http://engelschall.com/)\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n\"Software\"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be included\nin all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frse%2Fael","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frse%2Fael","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frse%2Fael/lists"}