Skip to content

Instantly share code, notes, and snippets.

View GuilhermeRossato's full-sized avatar
💼
“What I cannot build, I do not understand” - Richard Feynman

Guilherme Rossato GuilhermeRossato

💼
“What I cannot build, I do not understand” - Richard Feynman
View GitHub Profile
@GuilhermeRossato
GuilhermeRossato / README.md
Last active April 30, 2024 00:11
Function to display a Snackbar alert message. CSS-independent and project agnostic (does not use any library or framework).

Standalone Snackbar alert

Creates and displays an informative message popup on a web page that fades out over time.

The function is part of my standalone web-utilities project: It uses inline styles to be CSS-independent and native browser functions to be Framework-independent.

More information: https://github.com/GuilhermeRossato/web-utilities.git

@GuilhermeRossato
GuilhermeRossato / gmail-send.js
Created January 15, 2024 11:48
Node script to send an email through gmail using oauth2 (requires `googleapis` and `mimetext` npm modules)
// https://console.cloud.google.com/apis/credentials/oauthclient
const CLIENT_ID = "2######################################################################m";
const CLIENT_SECRET = "G#################################K";
const REDIRECT_URL = "http://localhost:8973/google-oauth/";
const targetEmail = process.argv[2];
const subject = process.argv[3] || "This is a test from a node script";
const htmlContent = process.argv[4] || "Hello world<br/>What's <b>Up?</b>";
const senderName = "Your Name";
const senderEmail = "your-email@gmail.com";
@GuilhermeRossato
GuilhermeRossato / README.md
Created May 23, 2023 09:16
Simple nodejs project with typescript in the frontend and backend with good stack traces and as little dependencies as necessary

Minimal Typescript Web App Project

This is a minimal project with a frontend and a backend in typescript that has automatic rebuilding of typescript files and preserves the call stack when an error is thrown.

To start your app just run npm run start and it will serve the project at port 8080.

The compilation of the backend and the frontend happens automatically when a file changes with the use of tsc in watch mode.

The backend server process is restarted automatically with the use of nodemon.

@GuilhermeRossato
GuilhermeRossato / singleton-web-app.js
Last active April 29, 2024 20:36
Single File Framework - An web app combined in a single file for fast prototyping of projects
/**
* This is the single file framework.
* As a backend script executed by Node.js it creates a http server that send itself to clients.
* As a frontend script it executes a startup function on the browser.
*
* You execute it on the terminal by calling `node index.js`, it will attempt to start your browser at the target url.
* The entry point is an empty HTML file that loads this script as the frontend script, and reloads the page when it updates.
*
* No credit is necessary when using this.
@GuilhermeRossato
GuilhermeRossato / getFuzzySearchMatches.js
Created August 16, 2022 22:15
Simple javascript function to perform fuzzy search based on sequential characters and return the matching parts
/**
* Returns a list of index pairs with the sequencial pieces of the query that show up on the parameter
* The indexes are between 0 and the text length.
* @example query = "hello", text = "ahellow", return = [{startIndex: 1, endIndex: 6}]
* @example query = "abc_def", text = "abcdef", return = [{startIndex: 0, endIndex: 3}]
* @example query = "abcdef", text = "abc_def", return = [{startIndex: 0, endIndex: 3}, {startIndex: 4, endIndex: 7}]
* @example query = "abcd", text = "a b c", return = [{start:0, end:1},{start:2, end:3},{start:4, end:5}]
* @params {string} query The search query to transverse the text
* @params {string} text The text to find the parts that match the search query
*/
@GuilhermeRossato
GuilhermeRossato / arrayToCsvString.js
Created April 18, 2022 20:44
Transform JS Array of CSV string
/**
* Transform an array of objects in javascript into a single csv string with the header as the first line
*
* Works on any javascript version above ES6 (2015)
*
* @param array {any[]}
* @param columnDelimiter {string}
* @param quoteChar {string}
* @param newline {string}
* @returns {string}
@GuilhermeRossato
GuilhermeRossato / nodejs-find-where-logged.js
Created April 14, 2022 04:40
Script to trace every console.log / warn / error and process.stdout.write after it executes (process.stdout part only for nodejs context)
['log', 'warn', 'error'].forEach((methodName) => {
// @ts-ignore
const originalMethod = console[methodName];
// @ts-ignore
console[methodName] = (...args) => {
let initiator = 'unknown place';
try {
throw new Error();
} catch (e) {
// @ts-ignore
@GuilhermeRossato
GuilhermeRossato / http-server.js
Last active June 26, 2023 22:35
Single file static http server with nodejs (without any dependency)
#!/usr/bin/node
const config = {host: '127.0.0.1', port: 8080, path: process.argv[2] || '.'};
const path = require('path');
const http = require('http');
const fs = require('fs');
const url = require('url');
const mimeLookup = {'aac':'audio/aac','abw':'application/x-abiword','arc':'application/x-freearc','avi':'video/x-msvideo','azw':'application/vnd.amazon.ebook','bin':'application/octet-stream','bmp':'image/bmp','bz':'application/x-bzip','bz2':'application/x-bzip2','csh':'application/x-csh','css':'text/css','csv':'text/csv','doc':'application/msword','docx':'application/vnd.openxmlformats-officedocument.wordprocessingml.document','eot':'application/vnd.ms-fontobject','epub':'application/epub+zip','gz':'application/gzip','gif':'image/gif','htm':'text/html','html':'text/html','ico':'image/vnd.microsoft.icon','ics':'text/calendar','jar':'application/java-archive','jpeg':'image/jpeg','jpg':'image/jpeg','js':'text/javascript','json':'application/json','jsonld':'application/ld+json','mid':'audio/midi','midi':'au
@GuilhermeRossato
GuilhermeRossato / index.js
Last active November 2, 2021 20:57
NodeJS Script to Serve Folder (either host local web server or run npm scripts) from the Context Menu (right click on folder)
// @ts-check
// Script to either host a local web server or run npm scripts
const http = require("http");
const fs = require("fs");
const path = require("path");
const cp = require("child_process");
const folder = process.argv.length <= 2 ? process.cwd() : path.resolve(process.argv[2]);
@GuilhermeRossato
GuilhermeRossato / getMimeTypeFromExtension.js
Created July 30, 2021 03:04
Get MimeType From File Extension - A Javascript function to return the value of a "content-type" header given a file extension.
function getMimeTypeFromExtension(extension = "txt") {
if (extension[0] === ".") {
extension = extension.substr(1);
}
return {
"aac": "audio/aac",
"abw": "application/x-abiword",
"arc": "application/x-freearc",
"avi": "video/x-msvideo",
"azw": "application/vnd.amazon.ebook",