| language: node_js | ||
| node_js: | ||
| - "4.1" | ||
| - "4.0" |
+9
-5
@@ -21,6 +21,12 @@ "use strict"; | ||
| function getFromContext (ctx, prop) { | ||
| if (ctx === null || ctx === undefined) { | ||
| return null; | ||
| } | ||
| return ctx.hasOwnProperty(prop) ? ctx[prop] : null; | ||
| } | ||
| class Identifier extends Value { | ||
| getValue (ctx) { | ||
| const value = ctx[this.value]; | ||
| return value === undefined ? null : value; | ||
| return getFromContext(ctx, this.value); | ||
| } | ||
@@ -62,5 +68,3 @@ } | ||
| getValue (ctx) { | ||
| const value = this.left.getValue(ctx), | ||
| prop = value && value[this.right.getValue(ctx)]; | ||
| return prop === undefined ? null : prop; | ||
| return getFromContext(this.left.getValue(ctx), this.right.getValue(ctx)); | ||
| } | ||
@@ -67,0 +71,0 @@ } |
+5
-2
| { | ||
| "name": "seljs", | ||
| "version": "0.1.1", | ||
| "description": "Simple Expression Languaje for JavaScript", | ||
| "version": "0.1.3", | ||
| "description": "Simple Expression Language for JavaScript", | ||
| "main": "index.js", | ||
@@ -10,2 +10,5 @@ "repository": { | ||
| }, | ||
| "engines": { | ||
| "npm": ">=4.0.0" | ||
| }, | ||
| "scripts": { | ||
@@ -12,0 +15,0 @@ "preversion": "npm test", |
+75
-2
@@ -1,3 +0,76 @@ | ||
| # seljs | ||
| # Simple Expression Language for JavaScript | ||
| Simple Expression Language for JavaScript | ||
| [](https://travis-ci.org/victorherraiz/seljs) | ||
| ## Requirements: | ||
| * Nodejs 4+ | ||
| ## Install | ||
| npm install seljs --save | ||
| ## Features | ||
| * No side effects: only and expression language | ||
| * Fast and simple: small memory footprint | ||
| * No dependencies | ||
| * Simplified property access | ||
| ## Language features | ||
| Very limited at the moment. | ||
| * Basic arithmetic: Adding, subtraction, Multiplication and Division | ||
| * Context and property access | ||
| * Literals: Strings and integers | ||
| * String concatenation | ||
| ## Work in progress | ||
| * Boolean operators: And, or, equals, not equals, negate. | ||
| * Grouping | ||
| * null keyword | ||
| * Functions | ||
| * Global functions | ||
| * Language documentation | ||
| ## Examples | ||
| ```js | ||
| const seljs = require("seljs"), | ||
| assert = require("assert"), | ||
| ctx = { | ||
| int: 123, | ||
| str: "123", | ||
| obj: { | ||
| int: 321, | ||
| str: "321", | ||
| obj: { | ||
| deep: 333 | ||
| } | ||
| } | ||
| }; | ||
| //Number literals | ||
| assert.strictEqual(seljs("123", ctx), 123); | ||
| //String literals | ||
| assert.strictEqual(seljs("'123'", ctx), "123"); | ||
| //Basic arithmetic | ||
| assert.strictEqual(seljs("1 + 3 * 2 / 4 - 1 * 2", ctx), 1 + 3 * 2 / 4 - 1 * 2); | ||
| //String concatenation | ||
| assert.strictEqual(seljs("'123' + '123'", ctx), "123123"); | ||
| //Context and property access | ||
| assert.strictEqual(seljs("int + obj.int + obj.obj.deep", ctx), 123 + 321 + 333); | ||
| ``` | ||
| ## License | ||
| [MIT](LICENSE) | ||
+12
-4
@@ -38,2 +38,5 @@ "use strict"; | ||
| assert.strictEqual(seljs("2 * int - 3 * 2 - int", ctx), 2 * 123 - 3 * 2 - 123); | ||
| assert.strictEqual(seljs("str.length", ctx), 3); | ||
| //no proto | ||
| assert.strictEqual(seljs("str.substr", ctx), null); | ||
@@ -47,6 +50,7 @@ //Null | ||
| //cache | ||
| assert.strictEqual(seljs.cache.clear(), undefined); | ||
| assert.strictEqual(seljs("1", ctx), 1); | ||
| assert.strictEqual(seljs.cache.size, 26); | ||
| assert.strictEqual(seljs.cache.size, 1); | ||
| assert.strictEqual(seljs("1", ctx), 1); | ||
| assert.strictEqual(seljs.cache.size, 26); | ||
| assert.strictEqual(seljs.cache.size, 1); | ||
| assert.strictEqual(seljs.cache.clear(), undefined); | ||
@@ -59,7 +63,11 @@ assert.strictEqual(seljs.cache.size, 0); | ||
| function wrapper(text, ctx) { | ||
| return () => seljs(text, ctx); | ||
| return function () { | ||
| return seljs(text, ctx); | ||
| }; | ||
| } | ||
| function message(text) { | ||
| return (error) => error.message === text; | ||
| return function (error) { | ||
| return error.message === text; | ||
| }; | ||
| } | ||
@@ -66,0 +74,0 @@ |
13753
15.09%10
11.11%325
3.5%77
1825%