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/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/lib/util.js b/lib/util.js index e1bf005..49e10a4 100644 --- a/lib/util.js +++ b/lib/util.js @@ -7,6 +7,8 @@ module.exports = { copy: copy, objectToPromise: objectToPromise, promiseMapSerial: promiseMapSerial, + isPromise: isPromise, + toPromise: toPromise, toAbsolutePointer: toAbsolutePointer }; @@ -20,65 +22,54 @@ 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