From 60ea545ee9d77ff832771b9912961bee1a3876a5 Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Tue, 2 Feb 2021 16:36:23 +0200 Subject: [PATCH] Started implementing `constant` property --- app/imports/api/properties/Constants.js | 35 +++++++++++++++++++++++ app/imports/parser/parseTree/ArrayNode.js | 17 +++++++++++ 2 files changed, 52 insertions(+) create mode 100644 app/imports/api/properties/Constants.js diff --git a/app/imports/api/properties/Constants.js b/app/imports/api/properties/Constants.js new file mode 100644 index 00000000..3a805547 --- /dev/null +++ b/app/imports/api/properties/Constants.js @@ -0,0 +1,35 @@ +import SimpleSchema from 'simpl-schema'; +import VARIABLE_NAME_REGEX from '/imports/constants/VARIABLE_NAME_REGEX.js'; + +/* + * Constants are primitive values that can be used elsewhere in computations + */ +let ConstantSchema = new SimpleSchema({ + name: { + type: String, + optional: true, + }, + // The technical, lowercase, single-word name used in formulae + variableName: { + type: String, + regEx: VARIABLE_NAME_REGEX, + min: 2, + defaultValue: 'newConstant', + }, + // The input value to be parsed, must return a constant node or an array + // of constant nodes to be valid + calculation: { + type: String, + optional: true, + }, + // The value, or array of values + result: { + type: SimpleSchema.oneOf(String, Number, Boolean, Array), + maxSize: 32, + }, + 'result.$': { + type: SimpleSchema.oneOf(String, Number, Boolean), + } +}); + +export { ConstantSchema }; diff --git a/app/imports/parser/parseTree/ArrayNode.js b/app/imports/parser/parseTree/ArrayNode.js index 66b3edab..62e0bbd7 100644 --- a/app/imports/parser/parseTree/ArrayNode.js +++ b/app/imports/parser/parseTree/ArrayNode.js @@ -1,10 +1,27 @@ import ParseNode from '/imports/parser/parseTree/ParseNode.js'; +import ConstantNode from '/imports/parser/parseTree/ConstantNode.js'; export default class ArrayNode extends ParseNode { constructor({values}) { super(...arguments); this.values = values; } + static fromConstantArray(array){ + let values = array.map( value => { + let type = typeof value; + if ( + type === 'string' || + type === 'number' || + type === 'boolean' || + type === 'undefined' + ){ + return new ConstantNode({value, type}); + } else { + throw `Unexpected type in constant array: ${type}` + } + }); + return new ArrayNode({values}); + } resolve(fn, scope, context){ let values = this.values.map(node => node[fn](scope, context)); return new ArrayNode({values});