Skip to content

Instantly share code, notes, and snippets.

View Papacidero's full-sized avatar

Carlos Eduardo Papacidero Papacidero

View GitHub Profile
const dateHandler = ({date = new Date(), days = 0, months = 0} = {}) => {
let copy = new Date(Number(date));
copy.setMonth(months > 0 ? copy.getMonth() + months : copy.getMonth() - Math.abs(months) )
copy = days > 0 ? copy.setDate(date.getDate() + days) : copy.setDate(date.getDate() - Math.abs(days));
return new Date(copy)
}
const daysInAMonth = (date = new Date()) => {
return new Date(date.getFullYear(), date.getMonth()+1, 0).getDate();
}
const objCreator = (key, value) => {
return { [key] : value};
}
let person = objCreator('name', 'Carlos Papacidero');
// resultado
{
name: 'Carlos Papacidero'
}
const handleLoading = (area) => {
return { [area]: true }
}
handleLoading('header');
const handleLoading = (area) => {
return { area: true };
}
const loading = handleLoading('header'); // Pense um pouco e adivinhe o que irá acontecer com o objeto?
const loading = {
header: false,
content: false,
footer: false,
};
// Sem Named Parameters
const formatarData = (dia, mes, ano, formato) => {
return `${
formato
.replace('dia', dia)
.replace('mes', mes)
.replace('ano', ano)
}`
}
// Utilizando Named Parameters com Default values
const formatarData = ({dia = 1, mes = 1, ano = 1970, formato = 'dia/mes/ano'} = {}) => {
return `${
formato
.replace('dia', dia)
.replace('mes', mes)
.replace('ano', ano)
}`
}
// Utilizando Named Parameters
const formatarData = ({dia, mes, ano, formato} = {}) => {
return `${
formato
.replace('dia', dia)
.replace('mes', mes)
.replace('ano', ano)
}`
}
const randomNumber = ({min = 1, max = 100} = {})=> {
return Math.floor(Math.random() * (max - min + 1) + min);
}
const gerarJogos = ({jogos = 1, dezenas = 6} = {}) => {
return Array.from({length: jogos}, () => {
const gerarNumeros = () => new Set(Array.from({length: dezenas}, () => randomNumber({min: 1, max: 60})));
let game = gerarNumeros()
while(game.size < dezenas) game = gerarNumeros();
return Array.from(game).sort((a,b)=> a -b);
@Papacidero
Papacidero / randomNumber.js
Last active December 1, 2020 10:47
randomNumber
const randomNumber = ({min = 1, max = 100} = {})=> {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
randomNumber({min: 1, max: 60});