UNPKG

flight-planner

Version:
56 lines (55 loc) 2.19 kB
/** * Checks if the given string is a valid aircraft registration. * * @param registration - The string to check * @returns True if the string is a valid aircraft registration, false otherwise */ export const isAircraftRegistration = (registration) => { return /^(N[1-9][0-9A-HJ-NP-Z]{0,4}|[A-Z]{1,2}-?[A-Z0-9]{1,4})$/.test(registration.toUpperCase()); }; /** * Normalizes the given aircraft registration to uppercase. * * @param registration - The aircraft registration to normalize * @returns The normalized aircraft registration */ export const aircraftNormalizeRegistration = (registration) => { return registration.toUpperCase().replace(/-/g, ''); }; /** * Calculates the maximum payload of the aircraft based on its maximum takeoff weight and empty weight. * * @param aircraft - The aircraft object * @returns The maximum payload in kilograms, or undefined if the weights are not provided */ export const aircraftMaxPayload = (aircraft) => { if (aircraft.maxTakeoffWeight !== undefined && aircraft.emptyWeight !== undefined) { return aircraft.maxTakeoffWeight - aircraft.emptyWeight; } return undefined; }; /** * Calculates the range of the aircraft based on its fuel capacity, fuel consumption, and cruise speed. * * @param aircraft - The aircraft object * @returns The range in nautical miles, or undefined if the required properties are not provided */ export const aircraftRange = (aircraft) => { const endurance = aircraftEndurance(aircraft); if (endurance !== undefined && aircraft.cruiseSpeed !== undefined) { return endurance * aircraft.cruiseSpeed; } return undefined; }; /** * Calculates the endurance of the aircraft based on its fuel capacity and fuel consumption. * * @param aircraft - The aircraft object * @returns The endurance in hours, or undefined if the required properties are not provided or fuelConsumption is zero */ export const aircraftEndurance = (aircraft) => { if (aircraft.fuelCapacity !== undefined && aircraft.fuelConsumption !== undefined && aircraft.fuelConsumption > 0) { return aircraft.fuelCapacity / aircraft.fuelConsumption; } return undefined; };