Skip to content

Instantly share code, notes, and snippets.

export const blank = <T>(
value: T,
): value is Extract<T, null | undefined | '' | [] | Record<string, never>> => {
if (value === undefined || value === null) {
return true
}
if (typeof value === 'boolean') {
return value === false
}
@papalardo
papalardo / either.ts
Created January 14, 2025 16:47
Trabalhar sem usos de try/catch e melhor legibilidade do fluxo
export type Either<A, B> = { result: A, error: undefined? } | { result: undefined, error: B };
export const either = async <R, L>(getter: () => Promise<R>): Promise<Either<R, L>> => {
try {
return { result: await getter() };
} catch (error) {
return { error };
}
};
// new Date(Date.now() - Duration({ minutes: 5 }).toMilliseconds())
// redis.set('key', Duration({ hours: 6 }).toMinutes('round'), 'value')
type Options = { days?: number; minutes?: number; hours?: number; seconds?: number; milliseconds?: number };
const multiplies: Record<keyof Options, number> = {
days: 24 * 60 * 60_000,
hours: 60 * 60_000,
minutes: 60_000,
seconds: 1_000,
const IgApiClient = require('instagram-private-api').IgApiClient;
const ig = new IgApiClient();
// Suas credenciais (para algumas ações, inclusive buscar imagens, é obrigatório)
const userName = 'papalardo';
const userPassword = '';
// Para buscar o id do usuário acesse: https://codeofaninja.com/tools/find-instagram-user-id/
const userId = '40266287952';
// Gera estado - Obrigatório
@papalardo
papalardo / whatsapp_download_audio.js
Created October 6, 2020 22:34
Whatsapp download and concat audios
class Crunker {
constructor({ sampleRate = 44100 } = {}) {
this._sampleRate = sampleRate;
this._context = this._createContext();
}
_createContext() {
window.AudioContext =
window.AudioContext ||
window.webkitAudioContext ||
@papalardo
papalardo / birthdays_between_dates_laravel.php
Last active December 22, 2022 17:56
Get birthdays users laravel
<?php
use Carbon\CarbonPeriod;
// On user model
public function scopeBirthdayBetween($query, $dateBegin, $dateEnd)
{
$period = CarbonPeriod::create($dateBegin, $dateEnd);
foreach ($period as $key => $date) {
// https://github.com/sindresorhus/is-regexp
const isRegex = input => Object.prototype.toString.call(input) === '[object RegExp]';
// https://github.com/sindresorhus/clone-regexp
const cloneRegexp = (e,n={}) =>{if(!isRegexp(e))throw new TypeError("Expected a RegExp instance");const t=Object.keys({ global: 'g', ignoreCase: 'i', multiline: 'm', dotAll: 's', sticky: 'y', unicode: 'u' }).map(t=>("boolean"==typeof n[t]?n[t]:e[t])?{ global: 'g', ignoreCase: 'i', multiline: 'm', dotAll: 's', sticky: 'y', unicode: 'u' }[t]:"").join(""),a=new RegExp(n.source||e.source,t);return a.lastIndex="number"==typeof n.lastIndex?n.lastIndex:e.lastIndex,a};
// https://github.com/andrewrk/node-diacritics
let replacementList = [{ base: ' ', chars: "\u00A0", }, { base: '0', chars: "\u07C0", }, { base: 'A', chars: "\u24B6\uFF21\u00C0\u00C1\u00C2\u1EA6\u1EA4\u1EAA\u1EA8\u00C3\u0100\u0102\u1EB0\u1EAE\u1EB4\u1EB2\u0226\u01E0\u00C4\u01DE\u1EA2\u00C5\u01FA\u01CD\u0200\u0202\u1EA0\u1EAC\u1EB6\u1E00\u0104\u023A\u2C6F", }, { base: 'AA', c
const normalize = str => str.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
let text = "Lorem Ipsum íís simply dummy tééxt of the printing and typesetting industry.";
let termSearch = 'lorem iss';
text = normalize(text).toLowerCase();
termSearch = normalize(termSearch).toLowerCase();
multiSearchAnd:
(text, searchWords) =>
@papalardo
papalardo / ClockWidget.vue
Created May 2, 2019 01:56
Realtime clock VueJS
<template>
<div class="callout calendar-day">
<div class="grid-x align-middle align-middle">
<div class="shrink cell">
<h1>{{ this.momentInstance.format('DD') }}</h1>
</div>
<div class="auto cell">
<h6>{{ this.momentInstance.format('[de] MMMM [de] YYYY') }}</h6>
<h6 class="light">{{ this.momentInstance.format('dddd[.] HH:mm:ss') }} </h6>
</div>
@papalardo
papalardo / getStatesWithCitys.php
Last active February 3, 2019 15:39
Listar estado com cidades do Brasil
$path = './api';
$filename = 'estados.json';
$filePath = $path .'/'. $filename;
if(file_exists($filePath)) {
$content = json_decode(file_get_contents($filePath), true);
return $content;
}
$somenteEssesEstados = ['RJ', 'SP'];