Skip to content

Instantly share code, notes, and snippets.

@jotredev
Last active July 7, 2023 02:06
Show Gist options
  • Save jotredev/d3896b2fe65196757f4147b1c74babd2 to your computer and use it in GitHub Desktop.
Save jotredev/d3896b2fe65196757f4147b1c74babd2 to your computer and use it in GitHub Desktop.
Funciones de JavaScript
export const dateFormat = (date) => {
const newDate = new Date(date.split("T")[0].split("-"));
const options = {
year: "numeric",
month: "long",
day: "numeric",
};
return newDate.toLocaleDateString("es-ES", options);
};
export const shortDateAndHour = (date) => {
const newDate = new Date(date);
const options = {
year: "numeric",
month: "short",
day: "numeric",
hour: "numeric",
minute: "numeric",
hour12: true,
};
return newDate.toLocaleDateString("es-ES", options);
};
export const number = (numero) => {
if (numero >= 1000) {
const abreviacion = ["", "K", "M", "B", "T"]; // abreviaciones posibles
const index = Math.floor(Math.log10(numero) / 3); // calcular el índice de abreviación
const numeroAbreviado = (numero / Math.pow(1000, index)).toFixed(1); // redondear el número
return numeroAbreviado + abreviacion[index]; // agregar la abreviación correspondiente
}
return numero;
};
export const timeAgo = (date) => {
const currentDate = new Date();
currentDate.setHours(currentDate.getHours() - 6);
const seconds = Math.floor((currentDate - new Date(date)) / 1000);
let interval = Math.floor(seconds / 31536000);
if (interval > 1) {
return `Hace ${interval} años`;
}
interval = Math.floor(seconds / 2592000);
if (interval > 1) {
return `Hace ${interval} meses`;
}
interval = Math.floor(seconds / 86400);
if (interval > 1) {
return `Hace ${interval} días`;
}
interval = Math.floor(seconds / 3600);
if (interval > 1) {
return `Hace ${interval} horas`;
}
interval = Math.floor(seconds / 60);
if (interval > 1) {
return `Hace ${interval} minutos`;
}
return `Hace ${Math.floor(seconds)}s`;
};
export const validateEmail = (email) => {
const re =
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
};
export const validatePassword = (password) => {
// Expresión regular para validar al menos una letra
const letter = /[a-zA-Z]/;
// Expresión regular para validar al menos un número
const number = /[0-9]/;
// Expresión regular para validar al menos una mayúscula
const upperCase = /[A-Z]/;
// Expresión regular para validar al menos un caracter especial
const special = /[!@#$%^&*]/;
// Comprobar si la contraseña cumple con todos los requisitos
if(password.length >= 8 && letter.test(password) && number.test(password) && upperCase.test(password) && special.test(password)) {
return true;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment