@tmlmobilidade/types
Version:
91 lines (90 loc) • 3.58 kB
JavaScript
/* * */
/**
* Validates and transforms a value into a GTFS Binary type.
* It accepts numeric or string representations of boolean values.
* If the value is not provided, it defaults to 0 (NO).
* @param value The value to validate and transform.
* @returns A GTFS_Binary value (0 or 1).
* @throws Error if the value is not a valid GTFS boolean representation.
*/
export function validateGtfsBinary(value) {
// Return NO if the value is not provided or is null/undefined
if (value === undefined || value === null || value === '') {
return 0;
}
// Handle numeric and string representations of GTFS boolean values
if (typeof value === 'number') {
if (value === 0)
return 0;
if (value === 1)
return 1;
}
// Handle string representations of GTFS boolean values
if (typeof value === 'string') {
if (value === '0')
return 0;
if (value === '1')
return 1;
}
// If the value does not match any known GTFS boolean representation, throw an error
throw new Error(`Invalid GTFS Binary value: "${value}". It must be 0 or 1.`);
}
/**
* Validates and transforms a value into a GTFS Ternary type.
* It accepts numeric or string representations of ternary values.
* If the value is not provided, it defaults to 0 (NOT_SPECIFIED).
* @param value The value to validate and transform.
* @returns A GTFS_Ternary value (0, 1, or 2).
* @throws Error if the value is not a valid GTFS ternary representation.
*/
export function validateGtfsTernary(value) {
// Return NOT_SPECIFIED if the value is not provided or is null/undefined
if (value === undefined || value === null || value === '') {
return 0;
}
// Handle numeric and string representations of GTFS boolean values
if (typeof value === 'number') {
if (value === 0)
return 0;
if (value === 1)
return 1;
if (value === 2)
return 2;
}
// Handle string representations of GTFS boolean values
if (typeof value === 'string') {
if (value === '0')
return 0;
if (value === '1')
return 1;
if (value === '2')
return 2;
}
// If the value does not match any known GTFS boolean representation, throw an error
throw new Error(`Invalid GTFS Ternary value: "${value}". It must be 0, 1, or 2.`);
}
/**
* Validates and transforms a value into a PickupDropoffType.
* It accepts numeric or string representations of pickup/drop-off types.
* If the value is not provided, it defaults to 1 (Regular pickup or drop-off).
* @param value The value to validate and transform.
* @returns A PickupDropoffType value (0, 1, 2, or 3).
* @throws Error if the value is not a valid PickupDropoffType representation.
*/
export function validateGtfsPickupDropoffType(value) {
// Default to regular pickup or drop-off
if (value === undefined || value === null || value === '')
return 1;
// Handle numeric and string representations of PickupDropoffType values
if (typeof value === 'number') {
if (value >= 0 && value <= 3)
return value;
}
else if (typeof value === 'string') {
const numValue = parseInt(value, 10);
if (!isNaN(numValue) && numValue >= 0 && numValue <= 3)
return numValue;
}
// If the value does not match any known PickupDropoffType, throw an error
throw new Error(`Invalid PickupDropoffType value: "${value}". It must be a number between 0 and 3.`);
}