Skip to content

Instantly share code, notes, and snippets.

@jrc03c
jrc03c / check-if-directly-invoked.js
Created December 30, 2024 19:21
Check if JS script is being invoked directly in Node
// In CommonJS format:
if (require.main === module) {
console.log("Invoked directly!")
}
// In ES module format:
if (import.meta.url.includes(process.argv[1])) {
console.log("Invoked directly!")
}
@jrc03c
jrc03c / jsobject.py
Last active August 18, 2024 17:17
create a js-like object in python
# A JS-like object in Python is an object whose properties can be set on-the-fly as in
# JS (i.e., without the need to define a class first).
def JSObject(data={}):
class JSObject:
def __getattr__(self, name):
return data[name]
def __setattr__(self, name, value):
data[name] = value
@jrc03c
jrc03c / tfidf.py
Last active April 16, 2024 19:23
A quick-and-dirty tf-idf implementation in Python
# pyds: https://github.com/jrc03c/pyds
from numpy import log
from pyds import sort
alpha = "abcdefghijklmnopqrstuvwxyz "
quotes = "'\"‘’“”"
def clean(x):
@jrc03c
jrc03c / extended-build.js
Last active October 7, 2023 18:39
an updated default build script with more bells & whistles
const { execSync } = require("node:child_process")
const { fg, fx } = require("@jrc03c/bash-colors")
const { indent, unindent, wrap } = require("@jrc03c/js-text-tools")
const express = require("express")
const fs = require("node:fs")
const path = require("node:path")
const process = require("node:process")
const watch = require("@jrc03c/watch")
const OUT_DIR = "_site"
@jrc03c
jrc03c / temporarily-disable-page-scrolling.js
Created September 8, 2023 16:25
Temporarily disable page scrolling when modal or menu is open
let scrollY = 0
// to disable scrolling:
scrollY = window.scrollY
document.body.style.position = "fixed"
document.body.style.top = `-${scrollY}px`
// to enable scrolling again:
document.body.style.position = ""
document.body.style.top = ""
@jrc03c
jrc03c / html-elements.txt
Created August 31, 2023 13:27
A list of all HTML elements (excluding deprecated elements)
# https://developer.mozilla.org/en-US/docs/Web/HTML/Element
<a>
<abbr>
<address>
<area>
<article>
<aside>
<audio>
<b>
<base>
@jrc03c
jrc03c / file-stream-write.js
Last active August 23, 2023 16:30
Stream (write) a file to disk in Node
const fs = require("fs")
!(async () => {
const stream = fs.createWriteStream("path/to/file")
let canWrite = true
let counter = 0
// optionally listen for errors
stream.on("error", error => {
throw error
@jrc03c
jrc03c / file-stream-read.js
Last active August 23, 2023 16:31
Stream (read) a file from disk in Node
const fs = require("fs")
const readline = require("readline")
!(async () => {
const stream = fs.createReadStream("path/to/file")
const rl = readline.createInterface({
input: stream,
crlfDelay: Infinity,
})
@jrc03c
jrc03c / set_matplotlib_plot_size.py
Created June 28, 2023 22:06
set matplotlib plot size
# 8" x 4.5"
plot.rcParams["figure.figsize"] = 8, 4.5
@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)
}