Skip to content

Commit

Permalink
Documention of standalone functions
Browse files Browse the repository at this point in the history
  • Loading branch information
richfab committed Nov 23, 2023
1 parent 6f6ddb5 commit 4a372e2
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions gbfs-validator/gbfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@ const validatorVersion = process.env.COMMIT_REF
? process.env.COMMIT_REF.substring(0, 7)
: require('./package.json').version

/**
* @typedef {{
* languages: Object,
* required: boolean,
* exists: boolean,
* file: string,
* hasErrors: boolean
* }} FileValidationResult
*/

/**
* @typedef {{
* vehicle_type_id: string,
* form_factor: string,
* propulsion_type: string
* }} VehicleType
*/

/**
* This function returns true if the file from a GBFS feed has errors or if the file is required and missing.
* @param {Object} data - The body of a file from a GBFS feed.
* @param {boolean} required - True the file is required.
* @returns {boolean}
*/
function hasErrors(data, required) {
let hasError = false

Expand All @@ -22,6 +46,11 @@ function hasErrors(data, required) {
return hasError
}

/**
* This function returns the number of errors in a file of a GBFS feed.
* @param {FileValidationResult} file - The validation result of a file from a GBFS feed.
* @returns {number}
*/
function countErrors(file) {
let count = 0

Expand All @@ -46,6 +75,13 @@ function countErrors(file) {
return count
}

/**
* This function returns a JSON Patch to modify a GBFS JSON schema.
* @param {string} version - The version of the GBFS feed.
* @param {string} partial - The path to the JSON Patch file.
* @param {Object} data - The params for generating the JSON Patch.
* @returns {Object} - A JSON Patch.
*/
function getPartialSchema(version, partial, data = {}) {
let partialSchema

Expand All @@ -60,6 +96,11 @@ function getPartialSchema(version, partial, data = {}) {
return partialSchema
}

/**
* This function returns an array of vehicle types defined in vehicle_types.json.
* @param {Object} param0 - The vehicle_types.json file.
* @returns {VehicleType[]}
*/
function getVehicleTypes({ body }) {
if (Array.isArray(body)) {
return body.reduce((acc, lang) => {
Expand All @@ -84,6 +125,11 @@ function getVehicleTypes({ body }) {
}
}

/**
* This function returns an array of pricing plans defined in system_pricing_plans.json.
* @param {*} param0 - The system_pricing_plans.json file.
* @returns {Object[]} - An array of pricing plans.
*/
function getPricingPlans({ body }) {
if (Array.isArray(body)) {
return body.reduce((acc, lang) => {
Expand All @@ -100,6 +146,11 @@ function getPricingPlans({ body }) {
}
}

/**
* This function returns true if free_bike_status.json contains a vehicle with a vehicle_type_id.
* @param {*} param0 - The free_bike_status.json file.
* @returns {boolean}
*/
function hadVehicleTypeId({ body }) {
if (Array.isArray(body)) {
return body.some((lang) =>
Expand All @@ -110,6 +161,11 @@ function hadVehicleTypeId({ body }) {
}
}

/**
* This function returns true if free_bike_status.json contains a vehicle with a pricing_plan_id.
* @param {*} param0 - The free_bike_status.json file.
* @returns {boolean}
*/
function hasPricingPlanId({ body }) {
if (Array.isArray(body)) {
return body.some((lang) =>
Expand All @@ -120,6 +176,13 @@ function hasPricingPlanId({ body }) {
}
}

/**
* This function returns true if a file from a GBFS feed contains a specific rental_uri (ios or android).
* @param {Object} param0 - A file from a GBFS feed.
* @param {string} key - The type of the objects that may contain a rental_uri (bikes or stations).
* @param {string} store - The type of store (ios or android).
* @returns {boolean}
*/
function hasRentalUris({ body }, key, store) {
if (Array.isArray(body)) {
return body.some((lang) =>
Expand All @@ -130,6 +193,11 @@ function hasRentalUris({ body }, key, store) {
}
}

/**
* This function returns true if a file from a GBFS feed exists in any language.
* @param {Object} file - A file from a GBFS feed.
* @returns {boolean}
*/
function fileExist(file) {
if (!file) {
return false
Expand All @@ -144,6 +212,11 @@ function fileExist(file) {
return false
}

/**
* This function returns true if the gbfs.json file is required.
* @param {string} version - The version of the GBFS feed.
* @returns {boolean}
*/
function isGBFSFileRequire(version) {
if (!version) {
return false
Expand All @@ -152,7 +225,13 @@ function isGBFSFileRequire(version) {
}
}

/** Class representing a GBFS feed. */
class GBFS {
/**
*
* @param {string} url - The URL of the gbfs.json file
* @param {Object} param1 - The parameters of the validation
*/
constructor(
url,
{ docked = false, freefloating = false, version = null, auth = {} } = {}
Expand Down

0 comments on commit 4a372e2

Please sign in to comment.