@homelib/utils 0.0.0-wip.33a9dc48299c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +19 -0
- package/README.md +7 -0
- package/bld/library/apparent-temperature.d.ts +18 -0
- package/bld/library/apparent-temperature.js +75 -0
- package/bld/library/apparent-temperature.js.map +1 -0
- package/bld/library/index.d.ts +2 -0
- package/bld/library/index.js +3 -0
- package/bld/library/index.js.map +1 -0
- package/bld/library/state-matcher.d.ts +24 -0
- package/bld/library/state-matcher.js +47 -0
- package/bld/library/state-matcher.js.map +1 -0
- package/package.json +20 -0
- package/src/library/apparent-temperature.ts +97 -0
- package/src/library/index.ts +2 -0
- package/src/library/state-matcher.ts +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2026 vilicvane
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
14
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
15
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
16
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
17
|
+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
18
|
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
|
|
19
|
+
OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculates apparent temperature in degrees Celsius from air temperature in
|
|
3
|
+
* degrees Celsius and relative humidity as a normalized ratio from 0 to 1.
|
|
4
|
+
* Inputs are expected to be finite numbers within the formula's physical
|
|
5
|
+
* domain.
|
|
6
|
+
*
|
|
7
|
+
* Uses `AT = T - 4 + 2.01696 × RH × exp(17.67 × T / (T + 243.5))`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function getApparentTemperatureByTemperatureAndRelativeHumidity(temperature: number, relativeHumidity: number): number;
|
|
10
|
+
/**
|
|
11
|
+
* Calculates air temperature in degrees Celsius from apparent temperature in
|
|
12
|
+
* degrees Celsius and relative humidity as a normalized ratio from 0 to 1.
|
|
13
|
+
* Inputs are expected to be finite numbers within the formula's physical
|
|
14
|
+
* domain.
|
|
15
|
+
*
|
|
16
|
+
* Solves the apparent-temperature formula using Newton's method.
|
|
17
|
+
*/
|
|
18
|
+
export declare function getTemperatureByApparentTemperatureAndRelativeHumidity(apparentTemperature: number, relativeHumidity: number): number;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
const APPARENT_TEMPERATURE_OFFSET = 4;
|
|
2
|
+
const VAPOR_PRESSURE_FACTOR = 2.01696;
|
|
3
|
+
const SATURATION_EXPONENT_FACTOR = 17.67;
|
|
4
|
+
const SATURATION_TEMPERATURE_OFFSET = 243.5;
|
|
5
|
+
/**
|
|
6
|
+
* Calculates apparent temperature in degrees Celsius from air temperature in
|
|
7
|
+
* degrees Celsius and relative humidity as a normalized ratio from 0 to 1.
|
|
8
|
+
* Inputs are expected to be finite numbers within the formula's physical
|
|
9
|
+
* domain.
|
|
10
|
+
*
|
|
11
|
+
* Uses `AT = T - 4 + 2.01696 × RH × exp(17.67 × T / (T + 243.5))`.
|
|
12
|
+
*/
|
|
13
|
+
export function getApparentTemperatureByTemperatureAndRelativeHumidity(temperature, relativeHumidity) {
|
|
14
|
+
return (temperature -
|
|
15
|
+
APPARENT_TEMPERATURE_OFFSET +
|
|
16
|
+
VAPOR_PRESSURE_FACTOR *
|
|
17
|
+
relativeHumidity *
|
|
18
|
+
Math.exp((SATURATION_EXPONENT_FACTOR * temperature) /
|
|
19
|
+
(temperature + SATURATION_TEMPERATURE_OFFSET)));
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Calculates air temperature in degrees Celsius from apparent temperature in
|
|
23
|
+
* degrees Celsius and relative humidity as a normalized ratio from 0 to 1.
|
|
24
|
+
* Inputs are expected to be finite numbers within the formula's physical
|
|
25
|
+
* domain.
|
|
26
|
+
*
|
|
27
|
+
* Solves the apparent-temperature formula using Newton's method.
|
|
28
|
+
*/
|
|
29
|
+
export function getTemperatureByApparentTemperatureAndRelativeHumidity(apparentTemperature, relativeHumidity) {
|
|
30
|
+
if (relativeHumidity <= 1e-12) {
|
|
31
|
+
return apparentTemperature + APPARENT_TEMPERATURE_OFFSET;
|
|
32
|
+
}
|
|
33
|
+
let temperature;
|
|
34
|
+
if (apparentTemperature > 30) {
|
|
35
|
+
temperature = apparentTemperature + 2.5;
|
|
36
|
+
}
|
|
37
|
+
else if (apparentTemperature > 10) {
|
|
38
|
+
temperature = apparentTemperature + APPARENT_TEMPERATURE_OFFSET;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
temperature = Math.max(apparentTemperature + 5, 5);
|
|
42
|
+
}
|
|
43
|
+
const derivativeFactor = SATURATION_EXPONENT_FACTOR * SATURATION_TEMPERATURE_OFFSET;
|
|
44
|
+
for (let iteration = 0; iteration < 200; iteration++) {
|
|
45
|
+
const exponent = (SATURATION_EXPONENT_FACTOR * temperature) /
|
|
46
|
+
(temperature + SATURATION_TEMPERATURE_OFFSET);
|
|
47
|
+
const exponentValue = Math.exp(exponent);
|
|
48
|
+
const difference = temperature -
|
|
49
|
+
APPARENT_TEMPERATURE_OFFSET -
|
|
50
|
+
apparentTemperature +
|
|
51
|
+
VAPOR_PRESSURE_FACTOR * relativeHumidity * exponentValue;
|
|
52
|
+
if (Math.abs(difference) < 1e-9) {
|
|
53
|
+
return temperature;
|
|
54
|
+
}
|
|
55
|
+
const denominator = temperature + SATURATION_TEMPERATURE_OFFSET;
|
|
56
|
+
const derivative = 1 +
|
|
57
|
+
(VAPOR_PRESSURE_FACTOR *
|
|
58
|
+
relativeHumidity *
|
|
59
|
+
exponentValue *
|
|
60
|
+
derivativeFactor) /
|
|
61
|
+
(denominator * denominator);
|
|
62
|
+
if (Math.abs(derivative) < 1e-15) {
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
const nextTemperature = temperature - difference / derivative;
|
|
66
|
+
if (nextTemperature < -273.15) {
|
|
67
|
+
temperature = -273.15 + 1e-6;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
temperature = nextTemperature;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return temperature;
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=apparent-temperature.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"apparent-temperature.js","sourceRoot":"","sources":["../../src/library/apparent-temperature.ts"],"names":[],"mappings":"AAAA,MAAM,2BAA2B,GAAG,CAAC,CAAC;AACtC,MAAM,qBAAqB,GAAG,OAAO,CAAC;AACtC,MAAM,0BAA0B,GAAG,KAAK,CAAC;AACzC,MAAM,6BAA6B,GAAG,KAAK,CAAC;AAE5C;;;;;;;GAOG;AACH,MAAM,UAAU,sDAAsD,CACpE,WAAmB,EACnB,gBAAwB;IAExB,OAAO,CACL,WAAW;QACX,2BAA2B;QAC3B,qBAAqB;YACnB,gBAAgB;YAChB,IAAI,CAAC,GAAG,CACN,CAAC,0BAA0B,GAAG,WAAW,CAAC;gBACxC,CAAC,WAAW,GAAG,6BAA6B,CAAC,CAChD,CACJ,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,sDAAsD,CACpE,mBAA2B,EAC3B,gBAAwB;IAExB,IAAI,gBAAgB,IAAI,KAAK,EAAE,CAAC;QAC9B,OAAO,mBAAmB,GAAG,2BAA2B,CAAC;IAC3D,CAAC;IAED,IAAI,WAAmB,CAAC;IAExB,IAAI,mBAAmB,GAAG,EAAE,EAAE,CAAC;QAC7B,WAAW,GAAG,mBAAmB,GAAG,GAAG,CAAC;IAC1C,CAAC;SAAM,IAAI,mBAAmB,GAAG,EAAE,EAAE,CAAC;QACpC,WAAW,GAAG,mBAAmB,GAAG,2BAA2B,CAAC;IAClE,CAAC;SAAM,CAAC;QACN,WAAW,GAAG,IAAI,CAAC,GAAG,CAAC,mBAAmB,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC;IACrD,CAAC;IAED,MAAM,gBAAgB,GACpB,0BAA0B,GAAG,6BAA6B,CAAC;IAE7D,KAAK,IAAI,SAAS,GAAG,CAAC,EAAE,SAAS,GAAG,GAAG,EAAE,SAAS,EAAE,EAAE,CAAC;QACrD,MAAM,QAAQ,GACZ,CAAC,0BAA0B,GAAG,WAAW,CAAC;YAC1C,CAAC,WAAW,GAAG,6BAA6B,CAAC,CAAC;QAChD,MAAM,aAAa,GAAG,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QACzC,MAAM,UAAU,GACd,WAAW;YACX,2BAA2B;YAC3B,mBAAmB;YACnB,qBAAqB,GAAG,gBAAgB,GAAG,aAAa,CAAC;QAE3D,IAAI,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,GAAG,IAAI,EAAE,CAAC;YAChC,OAAO,WAAW,CAAC;QACrB,CAAC;QAED,MAAM,WAAW,GAAG,WAAW,GAAG,6BAA6B,CAAC;QAChE,MAAM,UAAU,GACd,CAAC;YACD,CAAC,qBAAqB;gBACpB,gBAAgB;gBAChB,aAAa;gBACb,gBAAgB,CAAC;gBACjB,CAAC,WAAW,GAAG,WAAW,CAAC,CAAC;QAEhC,IAAI,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,GAAG,KAAK,EAAE,CAAC;YACjC,MAAM;QACR,CAAC;QAED,MAAM,eAAe,GAAG,WAAW,GAAG,UAAU,GAAG,UAAU,CAAC;QAE9D,IAAI,eAAe,GAAG,CAAC,MAAM,EAAE,CAAC;YAC9B,WAAW,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC;QAC/B,CAAC;aAAM,CAAC;YACN,WAAW,GAAG,eAAe,CAAC;QAChC,CAAC;IACH,CAAC;IAED,OAAO,WAAW,CAAC;AACrB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/library/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC;AAC1C,cAAc,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export type StateMatcherDefinition<TState, TInput> = {
|
|
2
|
+
readonly state: TState;
|
|
3
|
+
readonly enter: (input: TInput) => boolean;
|
|
4
|
+
readonly leave: (input: TInput) => boolean;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Matches inputs to states while preserving the current state until its leave
|
|
8
|
+
* predicate is satisfied.
|
|
9
|
+
*
|
|
10
|
+
* Definitions are checked in order when selecting the next state. Enter
|
|
11
|
+
* predicates should cover the complete input space, while leave predicates can
|
|
12
|
+
* define a wider boundary to provide hysteresis. After a definition is left,
|
|
13
|
+
* that same definition is skipped while selecting the next state.
|
|
14
|
+
*/
|
|
15
|
+
export declare class StateMatcher<TState, TInput> {
|
|
16
|
+
private readonly definitions;
|
|
17
|
+
private currentDefinition;
|
|
18
|
+
constructor(definitions: readonly StateMatcherDefinition<TState, TInput>[]);
|
|
19
|
+
get state(): TState | undefined;
|
|
20
|
+
update(input: TInput): {
|
|
21
|
+
state: TState;
|
|
22
|
+
changed: boolean;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Matches inputs to states while preserving the current state until its leave
|
|
3
|
+
* predicate is satisfied.
|
|
4
|
+
*
|
|
5
|
+
* Definitions are checked in order when selecting the next state. Enter
|
|
6
|
+
* predicates should cover the complete input space, while leave predicates can
|
|
7
|
+
* define a wider boundary to provide hysteresis. After a definition is left,
|
|
8
|
+
* that same definition is skipped while selecting the next state.
|
|
9
|
+
*/
|
|
10
|
+
export class StateMatcher {
|
|
11
|
+
constructor(definitions) {
|
|
12
|
+
Object.defineProperty(this, "definitions", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
configurable: true,
|
|
15
|
+
writable: true,
|
|
16
|
+
value: definitions
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(this, "currentDefinition", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
configurable: true,
|
|
21
|
+
writable: true,
|
|
22
|
+
value: void 0
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
get state() {
|
|
26
|
+
var _a;
|
|
27
|
+
return (_a = this.currentDefinition) === null || _a === void 0 ? void 0 : _a.state;
|
|
28
|
+
}
|
|
29
|
+
update(input) {
|
|
30
|
+
const { currentDefinition } = this;
|
|
31
|
+
if (currentDefinition && !currentDefinition.leave(input)) {
|
|
32
|
+
return { state: currentDefinition.state, changed: false };
|
|
33
|
+
}
|
|
34
|
+
this.currentDefinition = undefined;
|
|
35
|
+
for (const definition of this.definitions) {
|
|
36
|
+
if (definition === currentDefinition) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (definition.enter(input)) {
|
|
40
|
+
this.currentDefinition = definition;
|
|
41
|
+
return { state: definition.state, changed: true };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
throw new Error(`No matching state for input ${JSON.stringify(input)}; previous state: ${String(currentDefinition === null || currentDefinition === void 0 ? void 0 : currentDefinition.state)}.`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=state-matcher.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state-matcher.js","sourceRoot":"","sources":["../../src/library/state-matcher.ts"],"names":[],"mappings":"AAMA;;;;;;;;GAQG;AACH,MAAM,OAAO,YAAY;IAGvB,YACmB,WAGd;QAHH;;;;mBAAiB,WAAW;WAGzB;QANG;;;;;WAAsE;IAO3E,CAAC;IAEJ,IAAI,KAAK;;QACP,OAAO,MAAA,IAAI,CAAC,iBAAiB,0CAAE,KAAK,CAAC;IACvC,CAAC;IAED,MAAM,CAAC,KAAa;QAClB,MAAM,EAAC,iBAAiB,EAAC,GAAG,IAAI,CAAC;QAEjC,IAAI,iBAAiB,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;YACzD,OAAO,EAAC,KAAK,EAAE,iBAAiB,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,EAAC,CAAC;QAC1D,CAAC;QAED,IAAI,CAAC,iBAAiB,GAAG,SAAS,CAAC;QAEnC,KAAK,MAAM,UAAU,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC;YAC1C,IAAI,UAAU,KAAK,iBAAiB,EAAE,CAAC;gBACrC,SAAS;YACX,CAAC;YAED,IAAI,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;gBAC5B,IAAI,CAAC,iBAAiB,GAAG,UAAU,CAAC;gBACpC,OAAO,EAAC,KAAK,EAAE,UAAU,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,EAAC,CAAC;YAClD,CAAC;QACH,CAAC;QAED,MAAM,IAAI,KAAK,CACb,+BAA+B,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,qBAAqB,MAAM,CAAC,iBAAiB,aAAjB,iBAAiB,uBAAjB,iBAAiB,CAAE,KAAK,CAAC,GAAG,CAC7G,CAAC;IACJ,CAAC;CACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@homelib/utils",
|
|
3
|
+
"version": "0.0.0-wip.33a9dc48299c",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public"
|
|
6
|
+
},
|
|
7
|
+
"repository": "https://github.com/homelib/homelib.git",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"author": "vilicvane",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"exports": {
|
|
12
|
+
"types": "./bld/library/index.d.ts",
|
|
13
|
+
"default": "./bld/library/index.js"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@homelib/core": "0.0.0-wip.33a9dc48299c",
|
|
17
|
+
"tslib": "^2.8.1"
|
|
18
|
+
},
|
|
19
|
+
"gitHead": "33a9dc48299c7f032eba8b37e8c535d2b1988d89"
|
|
20
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
const APPARENT_TEMPERATURE_OFFSET = 4;
|
|
2
|
+
const VAPOR_PRESSURE_FACTOR = 2.01696;
|
|
3
|
+
const SATURATION_EXPONENT_FACTOR = 17.67;
|
|
4
|
+
const SATURATION_TEMPERATURE_OFFSET = 243.5;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Calculates apparent temperature in degrees Celsius from air temperature in
|
|
8
|
+
* degrees Celsius and relative humidity as a normalized ratio from 0 to 1.
|
|
9
|
+
* Inputs are expected to be finite numbers within the formula's physical
|
|
10
|
+
* domain.
|
|
11
|
+
*
|
|
12
|
+
* Uses `AT = T - 4 + 2.01696 × RH × exp(17.67 × T / (T + 243.5))`.
|
|
13
|
+
*/
|
|
14
|
+
export function getApparentTemperatureByTemperatureAndRelativeHumidity(
|
|
15
|
+
temperature: number,
|
|
16
|
+
relativeHumidity: number,
|
|
17
|
+
): number {
|
|
18
|
+
return (
|
|
19
|
+
temperature -
|
|
20
|
+
APPARENT_TEMPERATURE_OFFSET +
|
|
21
|
+
VAPOR_PRESSURE_FACTOR *
|
|
22
|
+
relativeHumidity *
|
|
23
|
+
Math.exp(
|
|
24
|
+
(SATURATION_EXPONENT_FACTOR * temperature) /
|
|
25
|
+
(temperature + SATURATION_TEMPERATURE_OFFSET),
|
|
26
|
+
)
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Calculates air temperature in degrees Celsius from apparent temperature in
|
|
32
|
+
* degrees Celsius and relative humidity as a normalized ratio from 0 to 1.
|
|
33
|
+
* Inputs are expected to be finite numbers within the formula's physical
|
|
34
|
+
* domain.
|
|
35
|
+
*
|
|
36
|
+
* Solves the apparent-temperature formula using Newton's method.
|
|
37
|
+
*/
|
|
38
|
+
export function getTemperatureByApparentTemperatureAndRelativeHumidity(
|
|
39
|
+
apparentTemperature: number,
|
|
40
|
+
relativeHumidity: number,
|
|
41
|
+
): number {
|
|
42
|
+
if (relativeHumidity <= 1e-12) {
|
|
43
|
+
return apparentTemperature + APPARENT_TEMPERATURE_OFFSET;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let temperature: number;
|
|
47
|
+
|
|
48
|
+
if (apparentTemperature > 30) {
|
|
49
|
+
temperature = apparentTemperature + 2.5;
|
|
50
|
+
} else if (apparentTemperature > 10) {
|
|
51
|
+
temperature = apparentTemperature + APPARENT_TEMPERATURE_OFFSET;
|
|
52
|
+
} else {
|
|
53
|
+
temperature = Math.max(apparentTemperature + 5, 5);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const derivativeFactor =
|
|
57
|
+
SATURATION_EXPONENT_FACTOR * SATURATION_TEMPERATURE_OFFSET;
|
|
58
|
+
|
|
59
|
+
for (let iteration = 0; iteration < 200; iteration++) {
|
|
60
|
+
const exponent =
|
|
61
|
+
(SATURATION_EXPONENT_FACTOR * temperature) /
|
|
62
|
+
(temperature + SATURATION_TEMPERATURE_OFFSET);
|
|
63
|
+
const exponentValue = Math.exp(exponent);
|
|
64
|
+
const difference =
|
|
65
|
+
temperature -
|
|
66
|
+
APPARENT_TEMPERATURE_OFFSET -
|
|
67
|
+
apparentTemperature +
|
|
68
|
+
VAPOR_PRESSURE_FACTOR * relativeHumidity * exponentValue;
|
|
69
|
+
|
|
70
|
+
if (Math.abs(difference) < 1e-9) {
|
|
71
|
+
return temperature;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const denominator = temperature + SATURATION_TEMPERATURE_OFFSET;
|
|
75
|
+
const derivative =
|
|
76
|
+
1 +
|
|
77
|
+
(VAPOR_PRESSURE_FACTOR *
|
|
78
|
+
relativeHumidity *
|
|
79
|
+
exponentValue *
|
|
80
|
+
derivativeFactor) /
|
|
81
|
+
(denominator * denominator);
|
|
82
|
+
|
|
83
|
+
if (Math.abs(derivative) < 1e-15) {
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const nextTemperature = temperature - difference / derivative;
|
|
88
|
+
|
|
89
|
+
if (nextTemperature < -273.15) {
|
|
90
|
+
temperature = -273.15 + 1e-6;
|
|
91
|
+
} else {
|
|
92
|
+
temperature = nextTemperature;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return temperature;
|
|
97
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export type StateMatcherDefinition<TState, TInput> = {
|
|
2
|
+
readonly state: TState;
|
|
3
|
+
readonly enter: (input: TInput) => boolean;
|
|
4
|
+
readonly leave: (input: TInput) => boolean;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Matches inputs to states while preserving the current state until its leave
|
|
9
|
+
* predicate is satisfied.
|
|
10
|
+
*
|
|
11
|
+
* Definitions are checked in order when selecting the next state. Enter
|
|
12
|
+
* predicates should cover the complete input space, while leave predicates can
|
|
13
|
+
* define a wider boundary to provide hysteresis. After a definition is left,
|
|
14
|
+
* that same definition is skipped while selecting the next state.
|
|
15
|
+
*/
|
|
16
|
+
export class StateMatcher<TState, TInput> {
|
|
17
|
+
private currentDefinition: StateMatcherDefinition<TState, TInput> | undefined;
|
|
18
|
+
|
|
19
|
+
constructor(
|
|
20
|
+
private readonly definitions: readonly StateMatcherDefinition<
|
|
21
|
+
TState,
|
|
22
|
+
TInput
|
|
23
|
+
>[],
|
|
24
|
+
) {}
|
|
25
|
+
|
|
26
|
+
get state(): TState | undefined {
|
|
27
|
+
return this.currentDefinition?.state;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
update(input: TInput): {state: TState; changed: boolean} {
|
|
31
|
+
const {currentDefinition} = this;
|
|
32
|
+
|
|
33
|
+
if (currentDefinition && !currentDefinition.leave(input)) {
|
|
34
|
+
return {state: currentDefinition.state, changed: false};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
this.currentDefinition = undefined;
|
|
38
|
+
|
|
39
|
+
for (const definition of this.definitions) {
|
|
40
|
+
if (definition === currentDefinition) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (definition.enter(input)) {
|
|
45
|
+
this.currentDefinition = definition;
|
|
46
|
+
return {state: definition.state, changed: true};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
throw new Error(
|
|
51
|
+
`No matching state for input ${JSON.stringify(input)}; previous state: ${String(currentDefinition?.state)}.`,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
}
|