Objective: I need to refactor a method in my component that is used across multiple components, and move it to a service class for better organization.
Context: The method in question takes a user's date of birth input from a profile form and converts it from DD/MM/YYYY format to MM/DD/YYYY format.
Inquiry: When relocating the method to the service class, should it remain public or should it be changed to private?
I have tried researching this topic, but the information is ambiguous. It seems like most suggest keeping it public unless it is exclusively used within the same class.
Current Implementation
Component:
let dobForDb = this.formatDateForDB("31/12/2019");
formatDateForDB(data: any): string {
// Separate by '/'
var array = data.split("/");
let day = array[0];
let month = array[1];
let year = array[2];
let dateString = month + "/" + day + "/" + year;
return dateString;
}
Refactored as
Component:
let dobForDb = this.serviceclass.formatDateForDB("31/12/2019");
Service Class:
public formatDateForDB(data: any): string {
// Separate by '/'
var array = data.split("/");
let day = array[0];
let month = array[1];
let year = array[2];
let dateString = month + "/" + day + "/" + year;
return dateString;
}