Skip to content

Commit

Permalink
Minimal code restyling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Francesco Ugolini committed Apr 4, 2023
1 parent 77b9f8d commit 4e16b92
Show file tree
Hide file tree
Showing 11 changed files with 257 additions and 284 deletions.
47 changes: 22 additions & 25 deletions src/core/data_collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export class DataCollection {
*
* @param {string} dataPoint
*/
_parseDataPoint(dataPoint = '', variableType = 'text') {
// Stingify boolean values
#parseDataPoint(dataPoint = '', variableType = 'text') {
// Stringify boolean values
dataPoint = typeof dataPoint === 'boolean' ? dataPoint.toString() : dataPoint;

if (dataPoint) {
Expand Down Expand Up @@ -94,7 +94,7 @@ export class DataCollection {
*
* @param {object} rawData An object containing the data to be used to populate the data set.
*/
_parseRecords(rawData) {
#parseRecords(rawData) {
let records = [];

if (rawData && rawData.length > 0) {
Expand All @@ -104,7 +104,7 @@ export class DataCollection {
this.variables.maps.uids.forEach((variable, variableUID) => {
let variableType = this.variables.maps.types.get(variableUID);

dataField.push(this._parseDataPoint(dataPoint[variable], variableType));
dataField.push(this.#parseDataPoint(dataPoint[variable], variableType));
});

records.push(dataField);
Expand All @@ -119,8 +119,8 @@ export class DataCollection {
*
* @param {Object} rawData An object containing the data to be used to populate the data set.
*/
_createDataSet(rawData) {
const dataPoints = rawData && rawData.length > 0 ? this._parseRecords(rawData) : [];
#createDataSet(rawData) {
const dataPoints = rawData && rawData.length > 0 ? this.#parseRecords(rawData) : [];

this.dataSet = dataPoints;

Expand All @@ -131,7 +131,7 @@ export class DataCollection {
/**
* Create the indexes necessary to process, manipulate and represent the data set and it's records.
*/
_createVariablesMaps() {
#createVariablesMaps() {
// Generate a Map with the variables according to the order provided in the list of variables.

for (let i = 0; i < this.variables.size; i++) {
Expand All @@ -140,7 +140,7 @@ export class DataCollection {

const variableUID = variable.uid ? variable.uid : 'var_uid_' + index;

if (this.variables.list && this.variables.list.includes(variableUID)) {
if (this.variables.list.includes(variableUID)) {
throw 'Data Collection Error: ' + variableUID + ' already exists. Please, provide another unique UID.';
}

Expand Down Expand Up @@ -196,10 +196,10 @@ export class DataCollection {
*/
buildDataCollection(rawData) {
// 1. Create the variable index (necessary to get the records). .
this._createVariablesMaps();
this.#createVariablesMaps();

// 2. Store the records (arrays) into a class property.
this._createDataSet(rawData);
this.#createDataSet(rawData);
}

/**
Expand Down Expand Up @@ -273,7 +273,7 @@ export class DataCollection {
*/
addRecords(rawData) {
if (rawData && rawData.length > 0) {
const data = this._parseRecords(rawData);
const data = this.#parseRecords(rawData);

// 2. Include the new parsed records in the data set.
Array.prototype.push.apply(this.dataSet, data);
Expand All @@ -290,7 +290,7 @@ export class DataCollection {
addVariable(variableProperties, records) {
this.variables.push(variableProperties);

this._createVariablesMaps();
this.#createVariablesMaps();

const variableUID = this.variables.maps.reverseUids.get(variableProperties.sourceField);

Expand Down Expand Up @@ -319,9 +319,9 @@ export class DataCollection {
if (variableUID) {
const type = this.variables.maps.types.get(variableUID);

this.dataSet[index] = this._parseDataPoint(values, type);
this.dataSet[index] = this.#parseDataPoint(values, type);
} else {
this.dataSet[index] = this._parseRecords(values);
this.dataSet[index] = this.#parseRecords(values);
}
} else if (variableUID) {
const index = this.variables.maps.reverseUids.get(variableUID);
Expand All @@ -330,9 +330,9 @@ export class DataCollection {
if (typeof values == 'string') {
const splitData = values.split('');

this.dataSet[i][index] = this._parseDataPoint(splitData[i]);
this.dataSet[i][index] = this.#parseDataPoint(splitData[i]);
} else {
this.dataSet[i][index] = this._parseDataPoint(values[i]);
this.dataSet[i][index] = this.#parseDataPoint(values[i]);
}
}
}
Expand Down Expand Up @@ -467,19 +467,16 @@ export class DataCollection {
return order * (dataPointA - dataPointB);
}

if (dataPointA !== undefined && dataPointB !== undefined) {
if (dataPointA < dataPointB) {
return order * -1;
}

if (dataPointA > dataPointB) {
return order * 1;
}
if (dataPointA < dataPointB) {
return order * -1;
}

return 0;
if (dataPointA > dataPointB) {
return order * 1;
}

return 0;

});
} else {
throw new Error('Error: variable NOT found.');
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
import {
createDateTextField,
getDateFromString,
getTodaysDate,
getTodayDate,
convertIsoDateInEuDate,
convertEuDateInIsoDate,
getISOWeekNumber,
Expand Down Expand Up @@ -112,7 +112,7 @@ export {
export {
createDateTextField,
getDateFromString,
getTodaysDate,
getTodayDate,
convertIsoDateInEuDate,
convertEuDateInIsoDate,
getISOWeekNumber,
Expand Down
16 changes: 6 additions & 10 deletions src/utilities/date_utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
export {
createDateTextField,
getDateFromString,
getTodaysDate,
getTodayDate,
convertIsoDateInEuDate,
convertEuDateInIsoDate,
getISOWeekNumber,
Expand Down Expand Up @@ -152,7 +152,7 @@ function getDateFromString(dateString) {
/**
* Generate today's day according to the European standard
*/
function getTodaysDate() {
function getTodayDate() {
let newDate = new Date();

let month = newDate.getMonth() + 1;
Expand Down Expand Up @@ -316,13 +316,9 @@ function getEndOfMonth(year, month) {
* @param {boolean} showDate Display the date.
* @param {boolean} showTime Display the time.
*/
function getDateTimeInEuFormat(year, month, day, hours, minutes, seconds, spaced, lineBreak, showDate, showTime) {
function getDateTimeInEuFormat(year, month, day, hours, minutes, seconds, spaced, lineBreak, showDate = true, showTime = false) {
// TODO: replace the booleans check with true and refactor the associated instances.

// By default show the date, but not the time
showDate = showDate ? showDate : true;
showTime = showTime ? showTime : false;

let date = '';
let time = '';

Expand Down Expand Up @@ -368,13 +364,13 @@ function getDaysInMonth(year, month) {
* @param {number} month The month where the week belongs. An integer number, between 0 and 11.
* @param {number} day The day whose day of the week is to be calculated. An integer number, between 1 and 31.
* @param {[number]} businessDays Default: [1, 2, 3, 4, 5]. The days of the week considered to be business days.
* @param {[number]} holydays The date of the month that, although a business days, is not considered as such.
* @param {[number]} holidays The date of the month that, although a business days, is not considered as such.
* @returns True if it's a business day, otherwise false.
*/
function isBusinessDay(year, month, day, businessDays = [1, 2, 3, 4, 5], holydays = []) {
function isBusinessDay(year, month, day, businessDays = [1, 2, 3, 4, 5], holidays = []) {
const date = new Date(year, month, day);

if (holydays.length > 0 && holydays.includes(date.getDate())) {
if (holidays.length > 0 && holidays.includes(date.getDate())) {
return false;
} else if (businessDays.length > 0 && businessDays.includes(date.getDay())) {
return true;
Expand Down
18 changes: 9 additions & 9 deletions src/utilities/form_utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ function getFormData(domDataAttributeName, domDataAttributeValue) {
element.getAttribute('data-ft-set-today-date') == 'true' ||
element.getAttribute('data-ft-set-today-date') == true
) {
domFieldValue = date_utilities.getTodaysDate();
domFieldValue = date_utilities.getTodayDate();
}

jsonFieldValue = domFieldValue ? date_utilities.convertEuDateInIsoDate(domFieldValue) : undefined;

break;

case 'checkbox':
jsonFieldValue = element ? element.checked : false;
jsonFieldValue = element.checked || false;

break;

Expand Down Expand Up @@ -495,12 +495,12 @@ function validateForm(formID, clusterName, alertModule, clusterDataAttribute = '
input.getAttribute('data-ft-start-date') &&
input.value
) {
_startEndDateCheck(input);
startEndDateCheck(input);
}

// Custom telephone number check
if (input.getAttribute('data-ft-field-type') === 'tel' && input.value) {
_diallingCodeCheck(input);
diallingCodeCheck(input);
}

// VALIDATION
Expand Down Expand Up @@ -539,7 +539,7 @@ function validateForm(formID, clusterName, alertModule, clusterDataAttribute = '
});

if (errorList.length > 0) {
_showErrors(errorList, alertModule);
showErrors(errorList, alertModule);

return false;
} else {
Expand All @@ -552,7 +552,7 @@ function validateForm(formID, clusterName, alertModule, clusterDataAttribute = '
* @param {array} errorList An array with the elements that didn't pass the validation.
* @param {string} alertModule The data-ft-alerts=* value used to identify the alert module.
*/
function _showErrors(errorList, alertModule) {
function showErrors(errorList, alertModule) {
if (errorList.length > 0) {
let missingFields = '';

Expand Down Expand Up @@ -731,10 +731,10 @@ function redirectToNewPage(redirectParameters) {
*
* NOTE: works with input fields tagged with: "data-ft-start-date="{uid}" data-ft-end-date="{uid}" DOM attribute.
*/
function _startEndDateCheck(input) {
function startEndDateCheck(input) {
let startEndDateAttribute = input.getAttribute('data-ft-start-date');

let startDateElements = input && input.value ? input.value.split('/', 3) : '';
let startDateElements = input.value ? input.value.split('/', 3) : '';

const endDate = startEndDateAttribute ? form.querySelector('[data-ft-end-date="' + startEndDateAttribute + '"]') : '';

Expand Down Expand Up @@ -768,7 +768,7 @@ function _startEndDateCheck(input) {
*
* NOTE: required the (type="text") input field to have the "data-ft-field-type="tel" in order to work.
*/
function _diallingCodeCheck(input) {
function diallingCodeCheck(input) {
const telRegExp = /^\+[\d\s]{2,}/g;

let hasCountryCode = input.value ? telRegExp.test(input.value) : false;
Expand Down
10 changes: 4 additions & 6 deletions src/utilities/generic_utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export {
};

/**
* Allows to toggle a button and untoggle the other ones.
* Allows to toggle a button and release the other ones.
*
* @param {string} toggleButtonsClass The DOM class of the buttons that will act like mutually exclusive toggle buttons.
*/
Expand Down Expand Up @@ -171,7 +171,7 @@ function highlightUnmatchedField(
['keydown', 'input', 'change'].forEach((value, index) => {
// If, instead of selecting, the name is typed, mimic the same behaviour to populate the user id
primaryField.addEventListener(value, (event) => {
let fieldVal = primaryField && primaryField.value ? primaryField.value.trim() : '';
let fieldVal = primaryField.value ? primaryField.value.trim() : '';

if (fieldVal && fieldVal.length !== 0) {
let isValid = false;
Expand Down Expand Up @@ -209,7 +209,7 @@ function highlightUnmatchedField(
}

// Else - e.g. if it's just a 1-level path - check the inputted string this way
} else if (fieldVal) {
} else {
if (fieldVal.toLowerCase() == targetData[i][primaryFieldName].toLowerCase()) {
primaryField.value = targetData[i][primaryFieldName];

Expand Down Expand Up @@ -266,7 +266,7 @@ function getData(dataContainerID, dataAttribute, callback) {
* Get values from a given data- element and decode them
*
* @param {string} dataContainerID The container from which to extract the data.
* @param {string} dataAttribute The data- attribute name in which data are stored withing the data container.
* @param {string} dataAttribute The data- attribute name in which data are stored within the data container.
* @param {string} keyword The keyword used to read the encoded data.
* @param {string} callback The function to be run once data is extracted.
*/
Expand Down Expand Up @@ -369,8 +369,6 @@ function showLoadingOverlay(show) {

// Add the overlay in the dom DOM
targetContainer.insertAdjacentHTML('beforeend', LOADING_OVERLAY_HTML);

loadingOverlay = document.querySelector('.loading-overlay');
}
} else {
if (loadingOverlay) {
Expand Down
Loading

0 comments on commit 4e16b92

Please sign in to comment.