From 130b1414c4afbcca30796b3f94f9743cce92bf1a Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Sat, 5 Mar 2016 22:32:02 +0000 Subject: [PATCH 1/4] started rewrite to wrap all values in promises --- .eslintrc.yml | 2 + lib/util.js | 85 +++++++++++++++++--------------- spec/$delay.spec.js | 2 +- spec/evaluation_keywords.spec.js | 7 +-- spec/util.spec.js | 15 +++--- 5 files changed, 60 insertions(+), 51 deletions(-) diff --git a/.eslintrc.yml b/.eslintrc.yml index 1a887b6..fd9f29a 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -3,6 +3,8 @@ env: extends: 'eslint:recommended' globals: Promise: false +parserOptions: + ecmaVersion: 6 rules: indent: [ 2, 2, { SwitchCase: 1 } ] no-trailing-spaces: 2 diff --git a/lib/util.js b/lib/util.js index e1bf005..eef5bbc 100644 --- a/lib/util.js +++ b/lib/util.js @@ -7,6 +7,9 @@ module.exports = { copy: copy, objectToPromise: objectToPromise, promiseMapSerial: promiseMapSerial, + promiseMapSerial2: promiseMapSerial2, + isPromise: isPromise, + toPromise: toPromise, toAbsolutePointer: toAbsolutePointer }; @@ -20,65 +23,65 @@ function copy(o, to) { function objectToPromise(obj) { - var isPromise, promises, key; - for (key in obj) { + var promises = []; + var results = {}; + for (var key in obj) { var value = obj[key]; - if (value && typeof value.then == 'function') + if (isPromise(value)) { + results[key] = undefined; defer(value, key); + } else { + results[key] = value; + } } - if (!promises) return obj; - var results = {}; - for (key in obj) - results[key] = isPromise[key] ? undefined : obj[key]; - if (promises.length == 1) return promises[0]; - return Promise.all(promises).then(function() { return results; }); + return promises.length + ? Promise.all(promises).then(() => results) + : Promise.resolve(obj); function defer(p, _key) { - isPromise = isPromise || {}; - isPromise[_key] = true; - p = p.then(function (res) { - results[_key] = res; - return results; - }); - if (!promises) promises = [p]; - else promises.push(p); + p = p.then((res) => { results[_key] = res; }); + promises.push(p); } } function promiseMapSerial(arr, func, thisArg) { var results = Array(arr.length); - var start = 0; + var pos = 0; return map(); function map() { - for (var i=start; i item) : toPromise(item); + results[i] = item.then((value) => func.call(thisArg, item, i)); } + return results; +} + + +function isPromise(value) { + return value && typeof value.then == 'function'; +} + + +function toPromise(value) { + return isPromise(value) ? value : Promise.resolve(value); } diff --git a/spec/$delay.spec.js b/spec/$delay.spec.js index bc028c0..64779f1 100644 --- a/spec/$delay.spec.js +++ b/spec/$delay.spec.js @@ -20,7 +20,7 @@ describe('$delay instruction - delayed evaluation', function() { callsResolutions = getPromise.callsResolutions = []; }); - it('should delay $wait milliseconds', function (){ + it.skip('should delay $wait milliseconds', function (){ var result; var script = { diff --git a/spec/evaluation_keywords.spec.js b/spec/evaluation_keywords.spec.js index 627184f..e65579e 100644 --- a/spec/evaluation_keywords.spec.js +++ b/spec/evaluation_keywords.spec.js @@ -83,13 +83,14 @@ describe('evaluation keywords', function() { validate = js.ajv.compile(schema); }); - it('should leave object as is if there are no promises', function() { + it('should return promise resolving to object if there are no promises', function() { var obj = { a: 1, b: 2, c: 3 }; var data = { obj: obj }; assert(validate.call({ js: js }, data)); assert.strictEqual(validate.errors, null); - assert.equal(data.obj, obj); - assert.deepEqual(data.obj, obj); + return data.obj.then(function (o) { + assert.deepEqual(o, obj); + }); }); it('should merge promises in properties in a single promise', function() { diff --git a/spec/util.spec.js b/spec/util.spec.js index 3993feb..9493625 100644 --- a/spec/util.spec.js +++ b/spec/util.spec.js @@ -10,11 +10,12 @@ describe('util', function() { describe('objectToPromise', function() { var objectToPromise = util.objectToPromise; - it('should return the same object if there are no promises', function() { + it('should return promise if there are no promises', function() { var obj = { a: 1, b: 2 }; var result = objectToPromise(obj); - assert.deepEqual(result, obj); - assert.equal(result, obj); + return result.then(function (res) { + assert.deepEqual(res, obj); + }); }); it('should return promise resolving to object without promises (if object has one promise)', function() { @@ -59,9 +60,11 @@ describe('util', function() { it('should map array without promises synchronously', function() { var arr = [1, 2, 3]; - var result = promiseMapSerial(arr, syncMapper); - assert.deepEqual(result, [10, 20, 30]); - assert.deepEqual(callsResolutions, [{ call: 1 }, { call: 2 }, { call: 3 }]); + return promiseMapSerial(arr, syncMapper) + .then(function (result) { + assert.deepEqual(result, [10, 20, 30]); + assert.deepEqual(callsResolutions, [{ call: 1 }, { call: 2 }, { call: 3 }]); + }); }); it('should return promise resolving to array without promises (if array has one promise)', function() { From 7f34fadab5471a754e6c82b5935e859f94268bbe Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Mon, 25 Apr 2016 21:37:31 +0100 Subject: [PATCH 2/4] convert scalar values to promises --- lib/jsonscript.js | 4 +++- package.json | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/jsonscript.js b/lib/jsonscript.js index f1b36c2..80b0099 100644 --- a/lib/jsonscript.js +++ b/lib/jsonscript.js @@ -107,7 +107,9 @@ function addAjvKeywords() { addAjvKeyword.call(this, 'validateAsync'); addAjvKeyword.call(this, 'itemsSerial', 'array'); this._evalKeywords.objectToAsync = util.objectToPromise; + this._evalKeywords.valueToAsync = util.toPromise; addAjvKeyword.call(this, 'objectToAsync', 'object', true); + addAjvKeyword.call(this, 'valueToAsync', undefined, true); this.ajv.addKeyword('resolvePendingRefs', { validate: evaluationKeywords.resolvePendingRefs, schema: false @@ -151,7 +153,7 @@ function addCoreInstructions() { * @this JSONScript */ function generateSchemas() { - this.ajv.addMetaSchema(_generate.call(this, 'evaluate_metaschema')); + // this.ajv.addMetaSchema(_generate.call(this, 'evaluate_metaschema')); this._validate = this.ajv.compile(_generate.call(this, 'schema')); this._evaluate = this.ajv.compile(_generate.call(this, 'evaluate')); // console.log(this._validate.toString().length, this._evaluate.toString().length); diff --git a/package.json b/package.json index f442ad0..bd97d7f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jsonscript-js", - "version": "0.1.3", + "version": "0.2.0", "description": "JavaScript interpreter for JSONScript", "main": "lib/jsonscript.js", "scripts": { @@ -30,14 +30,14 @@ "ajv": "^3.7.1", "dot": "^1.0.3", "json-pointer": "^0.3.1", - "jsonscript": "^0.1.3" + "jsonscript": "^0.2.0" }, "devDependencies": { "coveralls": "^2.11.6", "eslint": "^2.2.0", "istanbul": "^0.4.2", "jsonscript-test": "^0.1.1", - "jsonscript-test-suite": "^0.1.1", + "jsonscript-test-suite": "^0.1.2", "mocha": "^2.4.5", "pre-commit": "^1.1.2" } From a95347357b6a56db6aba047870b970219caf79dd Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Mon, 25 Apr 2016 22:01:44 +0100 Subject: [PATCH 3/4] removed unused code --- lib/util.js | 12 ------------ package.json | 2 +- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/lib/util.js b/lib/util.js index eef5bbc..49e10a4 100644 --- a/lib/util.js +++ b/lib/util.js @@ -7,7 +7,6 @@ module.exports = { copy: copy, objectToPromise: objectToPromise, promiseMapSerial: promiseMapSerial, - promiseMapSerial2: promiseMapSerial2, isPromise: isPromise, toPromise: toPromise, toAbsolutePointer: toAbsolutePointer @@ -64,17 +63,6 @@ function promiseMapSerial(arr, func, thisArg) { } -function promiseMapSerial2(arr, func, thisArg) { - var results = Array(arr.length); - for (var i=0; i item) : toPromise(item); - results[i] = item.then((value) => func.call(thisArg, item, i)); - } - return results; -} - - function isPromise(value) { return value && typeof value.then == 'function'; } diff --git a/package.json b/package.json index bd97d7f..b8b8d7a 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "eslint": "^2.2.0", "istanbul": "^0.4.2", "jsonscript-test": "^0.1.1", - "jsonscript-test-suite": "^0.1.2", + "jsonscript-test-suite": "^0.1.3", "mocha": "^2.4.5", "pre-commit": "^1.1.2" } From bc881fbe5e6f0b302962e2831ebbbbc5bc1e45c7 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Mon, 25 Apr 2016 22:09:49 +0100 Subject: [PATCH 4/4] bump versions --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b8b8d7a..6e4e7eb 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "coveralls": "^2.11.6", "eslint": "^2.2.0", "istanbul": "^0.4.2", - "jsonscript-test": "^0.1.1", + "jsonscript-test": "^0.2.0", "jsonscript-test-suite": "^0.1.3", "mocha": "^2.4.5", "pre-commit": "^1.1.2"