Skip to content

Commit 9bcf9e3

Browse files
authored
Reference Value Algorithm (#6)
* feat: reference function create & result type set * feat: function parameter change & referenceArray find * feat: add sample data & fix referenceArray finding method * feat: find rangeValue [scan referenceArray and notReference Array] * feat: complete referenceValue algorithm ✅ * feat: add test case1 * feat: add test case2 * feat: add LAST test case ✅ * style: edit comment lines * refactor: improvement code style and change variable * style: remove space * fix: null type issue fixed * fix: type changed. All issues fixed ✅ * style: variable name changed
1 parent 056ee49 commit 9bcf9e3

File tree

4 files changed

+164
-3
lines changed

4 files changed

+164
-3
lines changed

src/algorithms/reference-value.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { ReferenceResult, FormulaType } from '../types';
2+
const _ = require('lodash');
3+
4+
export function ReferenceValue(
5+
x: number,
6+
formulae: FormulaType[]
7+
): ReferenceResult | null {
8+
/* First, try to calculate using reference Formula */
9+
const referenceArray = formulae.filter((formula) => formula.is_reference);
10+
let formulaInRange = referenceArray.filter((r) => {
11+
return r.min <= x && r.max >= x;
12+
});
13+
14+
/* Second, if reference value is not found, try to calculate using non-reference Formula */
15+
if (formulaInRange.length === 0) {
16+
const notReferenceArray = formulae.filter(
17+
(formula) => formula.is_reference === false
18+
);
19+
20+
formulaInRange = notReferenceArray.filter((r) => {
21+
return r.min <= x && r.max >= x;
22+
});
23+
}
24+
25+
// if there is no range value both reference and not reference value are in the range of x then return null
26+
if (formulaInRange.length === 0) {
27+
return null;
28+
}
29+
30+
// Lastly, calculate the value using the formula
31+
const chainArray = formulaInRange.map((r) => {
32+
const formulaResult = eval(r.formula.replace(/X/g, x.toString()));
33+
return {
34+
method: r.method,
35+
value: formulaResult,
36+
};
37+
});
38+
39+
return _.maxBy(chainArray, 'value');
40+
41+
}

src/lib.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ export * from './algorithms/q-hampel';
66

77
export * from './algorithms/a-algorithm';
88

9+
export * from './algorithms/reference-value';
10+
911
export * from './grubbs';

src/types.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,16 @@ export type AResult = {
66
robust: number;
77
robustDeviation: number;
88
highLimit: number;
9-
lowLimit: number;
10-
};
9+
lowLimit: number;
10+
};
11+
export type ReferenceResult = {
12+
value: number;
13+
method: string;
14+
};
15+
export type FormulaType = {
16+
is_reference: boolean;
17+
method: string;
18+
formula: string;
19+
min: number;
20+
max: number;
21+
};

tests/algorithms.test.ts

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,113 @@
1-
import { MADe, M_Estimator, Q, Hampel, A_Algorithm } from '../src/lib';
1+
import {
2+
MADe,
3+
M_Estimator,
4+
Q,
5+
Hampel,
6+
A_Algorithm,
7+
ReferenceValue,
8+
} from '../src/lib';
29

310
describe('Algorithms', () => {
11+
it('Reference Value (CASE1: x value is not range)', () => {
12+
let x = 0.4;
13+
const formulae = [
14+
{
15+
formula: '0.075*X+4.6',
16+
method: 'TS EN 13132',
17+
min: 60.1,
18+
max: 500.0,
19+
is_reference: true,
20+
},
21+
{
22+
formula: '0.016*X+3.70 ',
23+
method: 'TS EN ISO 13032',
24+
min: 8.0,
25+
max: 50.0,
26+
is_reference: true,
27+
},
28+
{
29+
formula: '0.6*X',
30+
method: 'ASTM D381',
31+
min: 1.0,
32+
max: 30.0,
33+
is_reference: false,
34+
},
35+
];
36+
const output = ReferenceValue(x, formulae);
37+
38+
expect(output).toBe(null);
39+
});
40+
41+
it('Reference Value (CASE2: is not reference array)', () => {
42+
let x = 0.4;
43+
const formulae = [
44+
{
45+
formula: '0.075*X+4.6',
46+
method: 'TS EN 13132',
47+
min: 60.1,
48+
max: 500.0,
49+
is_reference: false,
50+
},
51+
{
52+
formula: '0.016*X+3.70 ',
53+
method: 'TS EN ISO 13032',
54+
min: 8.0,
55+
max: 50.0,
56+
is_reference: false,
57+
},
58+
{
59+
formula: '0.6*X',
60+
method: 'ASTM D381',
61+
min: 1.0,
62+
max: 30.0,
63+
is_reference: false,
64+
},
65+
];
66+
let output = ReferenceValue(x, formulae);
67+
expect(output).toBe(null);
68+
69+
x = 10;
70+
output = ReferenceValue(x, formulae);
71+
72+
expect(output?.value).toBeCloseTo(6, 3);
73+
expect(output?.method).toBe('ASTM D381');
74+
});
75+
76+
it('Reference Value (CASE3: is reference array)', () => {
77+
let x = 0.4;
78+
const formulae = [
79+
{
80+
formula: '0.075*X+4.6',
81+
method: 'TS EN 13132',
82+
min: 60.1,
83+
max: 500.0,
84+
is_reference: true,
85+
},
86+
{
87+
formula: '0.016*X+3.70 ',
88+
method: 'TS EN ISO 13032',
89+
min: 8.0,
90+
max: 50.0,
91+
is_reference: true,
92+
},
93+
{
94+
formula: '0.6*X',
95+
method: 'ASTM D381',
96+
min: 1.0,
97+
max: 30.0,
98+
is_reference: false,
99+
},
100+
];
101+
let output = ReferenceValue(x, formulae);
102+
expect(output).toBe(null);
103+
104+
x = 10;
105+
output = ReferenceValue(x, formulae);
106+
107+
expect(output?.value).toBeCloseTo(3.86, 3);
108+
expect(output?.method).toBe('TS EN ISO 13032');
109+
});
110+
4111
it('A algorithm', () => {
5112
const samples = [
6113
0.04, 0.055, 0.178, 0.202, 0.206, 0.227, 0.228, 0.23, 0.23, 0.235, 0.236,

0 commit comments

Comments
 (0)