Skip to content

Instantly share code, notes, and snippets.

View webarthur's full-sized avatar
✌️

Arthur Ronconi webarthur

✌️
View GitHub Profile
@webarthur
webarthur / pure-slidedown-slideup.css
Last active December 18, 2023 07:47
Pure CSS slidedown / slideup animation using transform translateY
.slide-up, .slide-down {
overflow:hidden;
}
.slide-up > div, .slide-down > div {
transform: translateY(-100%);
transition: .4s ease-in-out;
}
.slide-down > div {
transform: translateY(0);
}
@webarthur
webarthur / validaCNPJ.js
Last active December 8, 2023 18:39
Função JavaScript para validar CNPJ
function validaCNPJ (cnpj) {
var b = [ 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 ]
var c = String(cnpj).replace(/[^\d]/g, '')
if(c.length !== 14)
return false
if(/0{14}/.test(c))
return false
@webarthur
webarthur / validaData.js
Last active July 31, 2023 12:21
Função JavaScript para validar datas
// From: https://devarthur.com/blog/funcao-para-validar-data-em-javascript
// Codepen: https://codepen.io/devarthur/pen/vYgBWmv
function validaData (valor) {
// Verifica se a entrada é uma string
if (typeof valor !== 'string') {
return false
}
// Verifica formado da data
@webarthur
webarthur / add-numero-de-linhas-ao-codigo.js
Last active July 30, 2023 20:12
Adiciona números de linha aos blocos de código na página.
/**
* Adiciona números de linha aos blocos de código na página.
*/
function adicionarNumerosLinhasCodigo () {
// Seleciona todos os elementos <pre><code> na página
document.querySelectorAll('pre code')
.forEach(container => {
// Obter as linhas de código
const linhas = container.innerHTML.split('\n')
@webarthur
webarthur / add-line-numbers-to-code.js
Last active July 30, 2023 20:11
Adds line numbers to code blocks on the web page - version 1
/**
* Adds line numbers to code blocks on the page.
*/
function addLineNumbersToCode () {
// Select all <pre><code> elements on the page
document.querySelectorAll('pre code')
.forEach(container => {
// Get code lines
const lines = container.innerHTML.split('\n')
@webarthur
webarthur / add-line-numbers-to-code.js
Created July 30, 2023 20:11
Adds line numbers to code blocks on the web page - version 2
/**
* Adds line numbers to code blocks on the page.
*/
function addLinesToCode () {
// Select all <pre><code> elements on the page
document.querySelectorAll('pre code')
.forEach(container => {
// Get code lines
const lines = (container.textContent.match(/\n/g)||[]).length + 1
@webarthur
webarthur / example.js
Last active July 21, 2023 14:26
Middleware for ExpressJS to validate the request data against a schema using Mongoose.
const express = require('express')
const Article = require('./article.model.js')
const validate = require('./validate.middleware.js')
const router = express.Router()
router.get('/articles',
validate('query', {
title: String,
text: String,
@webarthur
webarthur / template-literals-render.js
Created July 10, 2023 18:12
A simple template literals engine as a function for Node.js
const { readFileSync } = require('fs')
const { resolve } = require('path')
// Resolving the absolute path of the 'views' directory
const viewDir = resolve(`${ __dirname }/views/`)
/**
* Renders a view with the provided data.
* @param {string} viewName - The name of the view to render.
* @param {object} data - The data to pass to the view.
@webarthur
webarthur / sharp-resize.js
Created July 10, 2023 02:39
Sharp: Resize if image is bigger than a specific size (use withoutEnlargement prop)
const sharp = require('sharp')
const maxWidth = 1200
const maxHeight = 2000
await sharp('uploads/image.png')
.resize(maxWidth, maxHeight * 1.5, {
fit: 'inside',
withoutEnlargement: true, // this
})
@webarthur
webarthur / mongodb-query-dates.js
Last active July 8, 2023 15:39
7 methods to query between dates in MongoDB using the greater than ($gte) operator and the less than ($lt) operator
let docs
// METHOD 1 - new Date() +1 day
const startDate = new Date('2021-03-31 00:00')
const endDate = new Date(startDate.getTime() + 86400000)
docs = await collection.find({
date1: {
$gte: startDate,
$lt: endDate
}