dive-deco
Version:
A TypeScript implementation of decompression calculation algorithms for scuba diving, featuring Bühlmann ZH-L16C algorithm with gradient factors, gas management, and oxygen toxicity tracking.
129 lines (128 loc) • 4.08 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BuhlmannConfigBuilder = exports.BuhlmannConfig = void 0;
const types_1 = require("./types");
class BuhlmannConfig {
gfLow;
gfHigh;
surfacePressureValue;
decoAscentRateValue;
ceilingTypeValue;
roundCeilingValue;
ndlTypeValue;
constructor(gradientFactors = [100, 100], surfacePressure = 1013, decoAscentRate = 10, ceilingType = types_1.CeilingType.Actual, roundCeiling = false, ndlType = types_1.NDLType.Actual) {
this.gfLow = gradientFactors[0];
this.gfHigh = gradientFactors[1];
this.surfacePressureValue = surfacePressure;
this.decoAscentRateValue = decoAscentRate;
this.ceilingTypeValue = ceilingType;
this.roundCeilingValue = roundCeiling;
this.ndlTypeValue = ndlType;
}
static default() {
return new BuhlmannConfig();
}
static builder() {
return new BuhlmannConfigBuilder();
}
validate() {
if (this.gfLow < 1 || this.gfLow > 100) {
return {
field: 'gfLow',
reason: 'GF Low must be between 1 and 100',
};
}
if (this.gfHigh < 1 || this.gfHigh > 100) {
return {
field: 'gfHigh',
reason: 'GF High must be between 1 and 100',
};
}
if (this.gfLow > this.gfHigh) {
return {
field: 'gradientFactors',
reason: 'GF Low must be less than or equal to GF High',
};
}
if (this.surfacePressureValue < 500 ||
this.surfacePressureValue > 1200) {
return {
field: 'surfacePressure',
reason: 'Surface pressure must be between 500 and 1200 mbar',
};
}
if (this.decoAscentRateValue <= 0 || this.decoAscentRateValue > 30) {
return {
field: 'decoAscentRate',
reason: 'Deco ascent rate must be between 0 and 30 m/min',
};
}
return null;
}
surfacePressure() {
return this.surfacePressureValue;
}
decoAscentRate() {
return this.decoAscentRateValue;
}
ceilingType() {
return this.ceilingTypeValue;
}
roundCeiling() {
return this.roundCeilingValue;
}
gradientFactors() {
return [this.gfLow, this.gfHigh];
}
ndlType() {
return this.ndlTypeValue;
}
clone() {
return new BuhlmannConfig([this.gfLow, this.gfHigh], this.surfacePressureValue, this.decoAscentRateValue, this.ceilingTypeValue, this.roundCeilingValue, this.ndlTypeValue);
}
}
exports.BuhlmannConfig = BuhlmannConfig;
class BuhlmannConfigBuilder {
gfLow = 100;
gfHigh = 100;
surfacePressureValue = 1013;
decoAscentRateValue = 10;
ceilingTypeValue = types_1.CeilingType.Actual;
roundCeilingValue = false;
ndlTypeValue = types_1.NDLType.Actual;
gradientFactors(gfLow, gfHigh) {
this.gfLow = gfLow;
this.gfHigh = gfHigh;
return this;
}
surfacePressure(pressure) {
this.surfacePressureValue = pressure;
return this;
}
decoAscentRate(rate) {
this.decoAscentRateValue = rate;
return this;
}
ceilingType(type) {
this.ceilingTypeValue = type;
return this;
}
roundCeiling(round) {
this.roundCeilingValue = round;
return this;
}
ndlType(type) {
this.ndlTypeValue = type;
return this;
}
build() {
const config = new BuhlmannConfig([this.gfLow, this.gfHigh], this.surfacePressureValue, this.decoAscentRateValue, this.ceilingTypeValue, this.roundCeilingValue, this.ndlTypeValue);
// Validate the configuration before returning
const error = config.validate();
if (error) {
throw new Error(`Invalid configuration: ${error.reason}`);
}
return config;
}
}
exports.BuhlmannConfigBuilder = BuhlmannConfigBuilder;