Skip to content

Instantly share code, notes, and snippets.

@realFranco
Created August 25, 2021 20:51
Show Gist options
  • Select an option

  • Save realFranco/b57aaa48aeae9eaed1770cd6ea826d00 to your computer and use it in GitHub Desktop.

Select an option

Save realFranco/b57aaa48aeae9eaed1770cd6ea826d00 to your computer and use it in GitHub Desktop.
Map structures between Backend & Frontend services.
INPUT_DATA_BACK = [
{
"weekDayName": 3,
"scheduleBlocks": [
{
"startWorkingTime": 480,
"endWorkingTime": 720,
"tag": null,
"description": null
},
{
"startWorkingTime": 840,
"endWorkingTime": 1080,
"tag": null,
"description": null
}
]
},
{
"weekDayName": 6,
"scheduleBlocks": [
{
"startWorkingTime": 480,
"endWorkingTime": 720,
"tag": null,
"description": null
},
{
"startWorkingTime": 840,
"endWorkingTime": 1080,
"tag": null,
"description": null
}
]
}
]
INPUT_DATA_FRONT = [
{
"weekdayID": 1,
"weekdayName": "lunes",
"scheduleBlocks": [
{
"startWorkTime": 480,
"endWorkTime": 720,
"labelStartTime": "08:00",
"labelEndTime": "12:00"
},
],
}
]
/*
Example of use:
> idToDay(0, "esp") -> "todos-los-días"
> idToDay(5, "esp") -> "jueves"
*/
function idToDay(id, language=""){
// TODO: if "id" is not in "mapDay" return an empty String object
if (id == undefined)
return "";
let mapDay = {
0: { "eng": "every-day", "esp": "todos-los-días" },
1: { "eng": "monday-friday", "esp": "lunes-viernes" },
2: { "eng": "monday", "esp": "lunes" },
3: { "eng": "tuesday", "esp": "martes" },
4: { "eng": "wednesday", "esp": "miércoles" },
5: { "eng": "thursday", "esp": "jueves" },
6: { "eng": "friday", "esp": "viernes" },
7: { "eng": "saturday", "esp": "sábado" },
8: { "eng": "sunday", "esp": "domingo" }
}
out = mapDay[id];
if (language == "esp")
out = out["esp"];
else if (language == "eng")
out = out["eng"];
return out
}
function translateSchedules(schedule){
function minutesToStrTime(minutes){
// 480 -> '08:00'
// 720 -> '12:00'
let strHour = "", strMinutes = "";
let hours = minutes / 60;
// Improve apply ternary operator
strHour = parseInt(hours);
if (hours < 10)
strHour = "0" + parseInt(hours);
// console.log("hours: ", hours);
let minutesInternal = hours - parseInt(hours); // Fetch the decimal portion
// console.log("minutes float portion: ", minutesInternal);
strMinutes = Math.ceil(minutesInternal * 0.6 * 100);
// console.log("minutes: ", strMinutes);
if (strMinutes < 10)
strMinutes = "0" + strMinutes;
return strHour + ":" + strMinutes;
}
let out = {};
// TODO: improve if-else block
if (schedule.hasOwnProperty("labelStartTime") == true) {
// console.log("Schedule is comming from the Frontend");
out = {
"startWorkTime": schedule.startWorkTime,
"endWorkTime": schedule.endWorkTime
};
}
else {
// console.log("Schedule is comming from the Backend");
out = {
"startWorkTime": schedule.startWorkingTime,
"endWorkTime": schedule.endWorkingTime,
"labelStartTime": minutesToStrTime(schedule.startWorkingTime),
"labelEndTime": minutesToStrTime(schedule.endWorkingTime)
};
}
return out; // Schedule translated
}
function translate(schedule){
/*
Data structures examples
{
"weekDayName": 3,
"scheduleBlocks": [
{
"startWorkingTime": 480,
"endWorkingTime": 720,
"tag": null,
"description": null
},
]
}
{
"weekdayID": 1,
"weekdayName": "lunes",
"scheduleBlocks": [
{
"startWorkTime": 480,
"endWorkTime": 720,
"labelStartTime": "08:00",
"labelEndTime": "12:00"
},
],
}
*/
// translate schedules
let scheduleBlocks = schedule.scheduleBlocks.map(translateSchedules);
let weekdayID = "";
// si la info viene del front
if ( schedule.hasOwnProperty( "weekdayID" ) == true )
weekdayID = schedule.weekdayID;
// si la info viene del front
else
weekdayID = schedule.weekDayName;
return {
"weekdayID": weekdayID,
"weekdayName": idToDay(id=weekdayID, language="esp"),
"scheduleBlocks": scheduleBlocks
}
}
console.log("Input data coming from the Backend\n")
out = INPUT_DATA_BACK.map(translate);
console.log( JSON.stringify( out ), "\n\n" );
console.log("Input data coming from the Frontend\n")
out = INPUT_DATA_FRONT.map(translate);
console.log( JSON.stringify( out ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment