Skip to content

Instantly share code, notes, and snippets.

View imaginamundo's full-sized avatar

Diogo Fernandes imaginamundo

View GitHub Profile
@imaginamundo
imaginamundo / html.js
Created February 20, 2024 05:15
HTML string test
// Faster
function html() {
const base = arguments[0];
if (!arguments[1]) return base[0];
let html = base[0];
for (let i = 1; i < arguments.length; i++) {
html += arguments[i];
if (base[i]) html += base[i];
}
return html;
@imaginamundo
imaginamundo / style.css
Created November 9, 2023 23:12
A grid for content
.content-grid {
--padding: 1rem;
--content-max-width: 700px;
--breakout-max-width: 900px;
justify-items: center;
display: grid;
grid-auto-flow: row;
}
.content-grid > * {
box-sizing: border-box;
@imaginamundo
imaginamundo / random.ts
Created September 2, 2021 19:03
Function to generate a random number with start and end as arguments
function random(from = 0, to = 1) {
return Math.random() * (to - from) + from;
}
export default random;
@imaginamundo
imaginamundo / .zshrc
Last active August 29, 2023 20:29
zshrc ❤️
# Load version control information
autoload -Uz vcs_info
precmd() { vcs_info }
precmd_functions+=( precmd_vcs_info )
# Format the vcs_info_msg_0_ variable
zstyle ':vcs_info:git:*' formats ' · %b'
# Set up the prompt (with git branch name)
setopt PROMPT_SUBST
@imaginamundo
imaginamundo / api-upload.js
Created May 24, 2021 20:48
Next.js get CSV and parse
import fs from 'fs';
import { IncomingForm } from 'formidable';
import csvParser from 'csv-parser';
export default async (req, res) => {
const data = await new Promise((resolve, reject) => {
const form = new IncomingForm()
form.parse(req, (err, fields, files) => {
if (err) return reject(err);
import React, { createContext, useContext, useState } from "react";
const Context = createContext({});
export function AuthContextProvider({ children }) {
const [ auth, setAuth ] = useState(null);
return (
<Context.Provider value={ { auth, setAuth } }>
{ children }
@imaginamundo
imaginamundo / timeoutFetch.js
Last active November 15, 2022 16:14
A wrapper for fetch with timeout using abort controller and a specific timeout error with url and status.
const DEFAULT_TIMEOUT = 3000;
function request(resource, options = {}) {
const { timeout = DEFAULT_TIMEOUT } = options;
return fetch(resource, { ...options, signal: AbortSignal.timeout(timeout) });
}
export default request;
@imaginamundo
imaginamundo / read_file.ts
Created February 1, 2020 17:45
Deno read import map
import { parse } from 'https://deno.land/std/flags/mod.ts';
import readFileHelp from './read_file_help.ts';
import readFileList from './read_file_list.ts';
import readFileOutdated from './read_file_outdated.ts';
import readFileUpdate from './read_file_update.ts';
const args = parse(Deno.args);
let arg;
@imaginamundo
imaginamundo / url.js
Created December 30, 2019 16:37
Function do get and update parameter from url
export const updateParameter = (uri = '/', key, value) => {
const hasQueries = uri.includes('?');
const hasHash = uri.includes('#');
let hash = '';
if (hasHash) {
hash = uri.substr(uri.indexOf('#'));
uri = uri.substr(0, uri.indexOf('#'));
}
let queries = '';
@imaginamundo
imaginamundo / Regex telefone Brasil.md
Last active April 6, 2024 22:46
Regex para padrões de telefones brasileiros com explicação e como usar

Regex para telefones do Brasil

(?:(^\+\d{2})?)(?:([1-9]{2})|([0-9]{3})?)(\d{4,5})(\d{4})

Pequena explicação

  • (?:(^\+\d{2})?)

    • Busca por um + seguido por dois números, opcional.
  • (?:([1-9]{2})|([0-9]{3})?)

  • Busca por dois números de 1 à 9, ou três números de 0 à 9, isso faz a diferença entre o DDD com zero e DDD sem zero, lembrando que nenhum código de cidade tem o digito 0. Também opcional.