Newer
Older
import { NgParameter } from "./formulaire/elements/ngparam";
import { ServiceFactory } from "./services/service-factory";
mathias.chouet
committed
import { formattedValue, Nub, VariatedDetails, ParamDefinition } from "jalhyd";
francois.grand
committed
export function logObject(obj: {}, m?: string) {
// évite le message "Value below was evaluated just now" dans le debugger de Chrome
francois.grand
committed
console.log(JSON.stringify(obj));
francois.grand
committed
console.log(m + " " + JSON.stringify(obj));
francois.grand
committed
export function isNumber(s: string): boolean {
francois.grand
committed
}
* Proxy to jaLhyd.formattedValue() using number of digits from app preferences
*/
export function fv(p: NgParameter | number): string {
if (p instanceof NgParameter) {
} else if (typeof p === "number") {
const nDigits = ServiceFactory.applicationSetupService.displayPrecision;
return formattedValue(value, nDigits);
/**
* Trick to decode HTML entities in a string
* https://stackoverflow.com/a/7394787/5986614
* @param html string containing HTML entities, like
*/
export function decodeHtml(html: string): string {
const txt = document.createElement("textarea");
txt.innerHTML = html;
return txt.value;
}
/**
* Given a list of variated NgParameter, returns the parameter having the most
* values, its index in the list, and the number of values it contains
* @param varParams
*/
mathias.chouet
committed
export function longestVarNgParam(varParams: NgParameter[]): { param: NgParameter, index: number, size: number } {
const variated: VariatedDetails[] = [];
for (const vp of varParams) {
variated.push({
param: vp.paramDefinition,
values: vp.paramDefinition.paramValues
});
}
mathias.chouet
committed
const { param, index, size } = longestVarParam(variated);
return {
param: varParams[index],
index,
size
};
}
/**
* Given a list of variated ParamDefinition, returns the parameter having the most
* values, its index in the list, and the number of values it contains
* @param variated
*/
export function longestVarParam(variated: VariatedDetails[]): { param: ParamDefinition, index: number, size: number } {
const { size, longest, minLinkedResultParam } = Nub.findVariatedSize(variated);
let realSize = size;
// if at least one linked variated result was found
if (minLinkedResultParam !== undefined) {
// if the size limited by linked variated results is shorter
// than the size of the longest variating element, limit it
if (minLinkedResultParam.values.valuesIterator.count() < realSize) {
realSize = minLinkedResultParam.values.valuesIterator.count();
mathias.chouet
committed
param: longest !== undefined ? variated[longest].param : undefined,
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* Generates a combination of values from a list of parameters, by extending the values
* list of each parameter if needed, then applying a formula to every values n-uple
*
* @param nub the Nub holding the parameters
* @param params the parameters to combine
* @param formula the formula to apply, receives 2 args: the Nub, and a map of current
* values for each combined parameter symbol
* @param variatedParams an optional list of parameters used to determine maximum length
* of combined values list; if undefined, all params of the Nub will be used
*/
export function generateValuesCombination(
nub: Nub,
params: ParamDefinition[],
formula: (nub: Nub, values: { [key: string]: number }) => number,
variatedParams?: ParamDefinition[]
): number | number[] {
let variates = false;
for (const p of params) {
variates = variates || p.hasMultipleValues; // manages CALC mode too
}
if (variates) {
let size: number;
// find longest values list with standard Nub method
if (variatedParams !== undefined) {
const variated: VariatedDetails[] = [];
for (const vp of variatedParams) {
variated.push({
param: vp,
values: vp.paramValues
});
}
size = longestVarParam(variated).size;
} else {
size = longestVarParam(nub.findVariatedParams()).size;
}
// extend values list for all params
const values: { [key: string]: number[] } = {};
for (const p of params) {
if (p.isCalculated) {
values[p.symbol] = nub.result.getCalculatedValues();
} else {
if (p.hasMultipleValues) {
values[p.symbol] = p.getInferredValuesList(size);
} else {
values[p.symbol] = [ p.singleValue ];
}
}
}
// calculate Y values
const Vs: number[] = [];
for (let i = 0; i < size; i++) {
const vals: { [key: string]: number } = {};
for (const p of params) {
// using "%" because lists might have only 1 value
vals[p.symbol] = values[p.symbol][i % values[p.symbol].length];
}
Vs.push(formula(nub, vals));
}
// variated result
return Vs;
} else {
const vals: { [key: string]: number } = {};
for (const p of params) {
vals[p.symbol] = p.V;
}
// single result
return formula(nub, vals);
}
}
export function arraysAreEqual(arrayA: any[], arrayB: any[], property?: string, sort = false): boolean {
const aA: any[] = JSON.parse(JSON.stringify(arrayA)); // array copy
const aB: any[] = JSON.parse(JSON.stringify(arrayB)); // array copy