Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/customer_satellites.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const fs = require("fs");

const formatDate = require("./date_convertor")
let launchers = require("../data/customer_satellites.json");

module.exports = async (req, res) => {
try {
res.send(launchers);
res.send({"customer_satellites":formatDate(launchers.customer_satellites)})
} catch (error) {
res.status(500);
const response = error.response || {};
Expand Down
43 changes: 43 additions & 0 deletions api/date_convertor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Function to extract year, month, and day from a date string in the format "DD-MM-YYYY".
* @param {string} currentDate - The date string in the format "DD-MM-YYYY".
* @returns {string} A string representing the date in the format "YYYY-MM-DD".
* @throws {Error} Throws an error if the date format is invalid.
*/
const extractMYD = (currentDate) => {
try {
// Check if the date string is provided and is of type string
if (!currentDate || typeof currentDate !== 'string') {
throw new Error('Invalid date format');
}

// Split the date string into day, month, and year components
const [day, month, year] = currentDate.split('-');

// Check if all components are present
if (!day || !month || !year) {
throw new Error('Invalid date format');
}

// Return the formatted date string in the format "YYYY-MM-DD"
return `${year}-${month}-${day}`;
} catch (e) {
// If any error occurs, throw an "Invalid date format" error
throw new Error('Invalid date format');
}
}

/**
* Function to format the launch_date property of each record in an array of records.
* @param {Array} records - An array of records, each containing a launch_date property.
* @returns {Array} An array of records with the launch_date property formatted as "YYYY-MM-DD".
*/
const formatDate = (records) => {
return records.map(record => {
// Format the launch_date property of each record using the extractMYD function
record.launch_date = extractMYD(record.launch_date);
return record;
});
}

module.exports = formatDate;