UNPKG

jalhyd

Version:

JaLHyd, a Javascript Library for Hydraulics

207 lines 7.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ParamValues = void 0; const internal_modules_1 = require("../internal_modules"); const internal_modules_2 = require("../internal_modules"); /** * Represents the value(s) taken by a Parameter, along with and depending on * the value mode; linked values are not managed here, only the LINK value * mode is defined here */ class ParamValues { clone() { const res = new ParamValues(); res.valueMode = this.valueMode; res.extensionStrategy = this.extensionStrategy; res._singleValue = this._singleValue; res._currentValue = this._currentValue; res.min = this.min; res.max = this.max; res.step = this.step; res.valueList = this.valueList === undefined ? undefined : [...this.valueList]; return res; } get singleValue() { return this._singleValue; } set singleValue(v) { this._singleValue = v; this._currentValue = v; } get currentValue() { return this._currentValue; } count() { return this._iterator.count(); } setValues(o, max, step) { if (typeof (o) === "number") { if (max === undefined) { this.valueMode = internal_modules_2.ParamValueMode.SINGLE; this.singleValue = o; } else { this.valueMode = internal_modules_2.ParamValueMode.MINMAX; this.min = o; this.max = max; this.step = step; } } else if (Array.isArray(o)) { this.valueMode = internal_modules_2.ParamValueMode.LISTE; this.valueList = o; } else { throw new Error(`ParamValues.setValues() : appel invalide`); } } check() { switch (this.valueMode) { case internal_modules_2.ParamValueMode.SINGLE: if (this.singleValue === undefined) { throw new Error(`ParamValues : valeur fixe non définie`); } break; case internal_modules_2.ParamValueMode.MINMAX: if (this.min === undefined) { throw new Error(`ParamValues : valeur min non définie`); } if (this.max === undefined) { throw new Error(`ParamValues : valeur max non définie`); } if (this.step === undefined) { throw new Error(`ParamValues : valeur du pas non définie`); } if (this.min > this.max) { throw new Error(`ParamValues : min > max`); } break; case internal_modules_2.ParamValueMode.LISTE: if (this.valueList === undefined) { throw new Error(`ParamValues : liste de valeurs non définie`); } break; case internal_modules_2.ParamValueMode.LINK: case internal_modules_2.ParamValueMode.CALCUL: default: break; } } /** * Returns values as a number list by running through the iterator, * taking in account reverse, extendTo and addLastStep if defined */ getInferredValuesList(reverse = false, extendTo, addLastStep = false) { if ([internal_modules_2.ParamValueMode.MINMAX, internal_modules_2.ParamValueMode.LISTE].includes(this.valueMode)) { if ((this.valueMode === internal_modules_2.ParamValueMode.LISTE) || (this.valueMode === internal_modules_2.ParamValueMode.MINMAX // protection against infinite loops && this.step !== undefined && this.step > 0 && this.min !== undefined && this.min !== null && this.max !== undefined && this.max !== null)) { const it = this.initValuesIterator(reverse, extendTo, addLastStep); const values = []; for (const v of it) { values.push(v); } return values; } else { return []; } } throw new Error("ParamValues.getInferredValuesList() : incorrect value mode" + internal_modules_2.ParamValueMode[this.valueMode]); } // -- iterator-related methods /** * Returns a ParamValueIterator over the current values * @param reverse if true, will iterate starting at the end * @param extendTo if defined, will extend values list until this boundary * @param addLastStep if true, if (max - min) is not a multiple of step, one more * iteration will be done return last value (max) */ getValuesIterator(reverse = false, extendTo, addLastStep = false) { return new internal_modules_1.ParamValueIterator(this, reverse, extendTo, addLastStep); } // interface IterableValues get valuesIterator() { return this.getValuesIterator(false, undefined, true); } get hasMultipleValues() { try { // will throw an error if no value is defined at all this.check(); } catch (e) { return false; } const it = this.getValuesIterator(); if (it) { let n = 0; for (const v of it) { n++; if (n > 1) { break; } } return n > 1; } else { return false; } } /** * Checks that values are in LIST or MINMAX mode, then retrieves an iterator * @param reverse if true, will iterate starting at the end * @param extendTo if defined, will extend values list until this boundary * @param addLastStep if true, if (max - min) is not a multiple of step, one more * iteration will be done return last value (max) */ initValuesIterator(reverse = false, extendTo, addLastStep = false) { switch (this.valueMode) { case internal_modules_2.ParamValueMode.LISTE: case internal_modules_2.ParamValueMode.MINMAX: this._iterator = this.getValuesIterator(reverse, extendTo, addLastStep); break; default: throw new Error(`ParamValues : mode de valeurs ${internal_modules_2.ParamValueMode[this.valueMode]} incorrect`); } return this._iterator; } /** * @return true si il reste des valeurs à parcourir par l'itérateur courant */ get hasNext() { return this._iterator.hasNext; } /** * fixe la valeur courante à la prochaine valeur à parcourir par l'itérateur courant * @return prochaine valeur à parcourir par l'itérateur courant */ next() { let res; res = this._iterator.next(); if (!res.done) { this._currentValue = res.value; } return res; } /** trick method - use .next() instead, unless you are explicitely in jalhyd#222 case */ nextValue() { return this.next(); } /** * Trick method to explicitely set _currentValue when using a dedicated iterator, for * params having multiple links on them @see jalhyd#222 ; usually calling .next() on * ParamDefinition sets _currentValue, but not when a dedicated iterator is used; this * method should only be called by ParamValueIterator.nextValue() */ setCurrentValueFromIterator(v) { this._currentValue = v; } [Symbol.iterator]() { return this; } } exports.ParamValues = ParamValues; //# sourceMappingURL=param-values.js.map