Skip to content

Instantly share code, notes, and snippets.

@jrc03c
jrc03c / __dirname.js
Last active September 2, 2022 15:34
Create `__dirname` variable when using `import` statements in Node
const __dirname = path.resolve(
import.meta.url.replace("file://", "").split("/").slice(0, -1).join("/")
)
@jrc03c
jrc03c / vite.config.js
Created September 2, 2022 15:39
Create a module resolution alias in (e.g.) Vite
// Note that this uses the __dirname definition from here:
// https://gist.github.com/jrc03c/6fbccd5af3a4d1b61f12e1e620e1c01b
import { defineConfig } from "vite"
import path from "path"
const __dirname = path.resolve(
import.meta.url.replace("file://", "").split("/").slice(0, -1).join("/")
)
@jrc03c
jrc03c / server.js
Created January 7, 2023 16:43
Basic HTTP / HTTPS server in Node w/ Express
const express = require("express")
const fs = require("fs")
const http = require("http")
const https = require("https")
// create a basic HTTP app that just redirects requests from HTTP to HTTPS
const httpApp = express()
httpApp.use((request, response) => {
return response.redirect("https://" + request.hostname + request.url)
@jrc03c
jrc03c / point-is-in-triangle.js
Last active January 23, 2023 16:36
check to see if a point lies inside a triangle
const ABOVE = "above"
const BELOW = "below"
const EXACTLY_ON = "exactly_on"
function getPointRelationToLine(point, pair) {
const [p1, p2] = pair
// if the pair have the same x-values, then they either lie on a
// vertical line or are identical
if (p2[0] - p1[0] === 0) {
@jrc03c
jrc03c / copy-text.js
Last active April 19, 2023 20:00
Copy arbitrary text to the clipboard in the browser
// 🚨 🚨 🚨
// NOTE: According to MDN, `document.execCommand()` is deprecated. See:
// https://developer.mozilla.org/en-US/docs/Web/API/document/execCommand
// Instead, use the Clipboard API:
// https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API
// 🚨 🚨 🚨
function copy(text) {
const input = document.createElement("input")
input.type = "text"
@jrc03c
jrc03c / git.txt
Last active April 19, 2023 20:05
Git Cheat Sheet
##################
Basic Git Commands
##################
git init = turns the current working directory into a repository
git clone = downloads a remote repository to your machine
Syntax: git clone <source> <destination>
Example: git clone https://github.com/willfind/uplift-website /home/josh/projects/uplift.app
@jrc03c
jrc03c / download-text.js
Last active May 9, 2023 00:30
Download text as a file
function downloadText(filename, text){
const a = document.createElement("a")
a.href = "data:text/plain;charset=utf-8," + encodeURIComponent(text)
a.download = filename
a.dispatchEvent(new MouseEvent("click"))
}
@jrc03c
jrc03c / deep-sort.js
Last active May 29, 2023 01:07
Recursively / deeply sort an object or array in JS
function deepSort(x, fn) {
fn = fn || ((a, b) => (a < b ? -1 : 1))
if (typeof x === "object") {
if (x === null) return x
if (x instanceof Array) {
return x.map(v => deepSort(v, fn)).sort(fn)
}
@jrc03c
jrc03c / download-canvas-as-image.js
Created February 13, 2020 15:10
Download the contents of an HTML5 canvas as an image
function downloadCanvasAsImage(canvas, filename){
let a = document.createElement("a")
a.href = canvas.toDataURL()
a.download = filename
a.dispatchEvent(new MouseEvent("click"))
}
@jrc03c
jrc03c / build.js
Last active June 13, 2023 17:21
generic build script
const process = require("process")
const watch = require("@jrc03c/watch")
function rebuild() {
console.log("-----")
console.log(`Rebuilding... (${new Date().toLocaleString()})`)
try {
// do stuff...
console.log("Done! 🎉")