Skip to content

Instantly share code, notes, and snippets.

View nathansmith's full-sized avatar
😎
Ask me about free high fives!

Nathan Smith nathansmith

😎
Ask me about free high fives!
View GitHub Profile
@nathansmith
nathansmith / waitForDeps.js
Created July 25, 2024 19:57
JS function that waits for dependencies, then calls them.
// Define function.
function waitForDeps(obj, list) {
// Expose promise.
return new Promise((resolve) => {
// Start polling.
const interval = setInterval(() => {
// Get missing.
const listMissing = list.filter(key => !obj[key]);
// All clear?
@nathansmith
nathansmith / getListStats.js
Last active July 2, 2024 17:16
Parses a numeric list into: mean, median, mode, etc.
/**
* Gets the median for a list of numbers.
*
* @param {number[]} [listSorted=[]]
* @returns {number}
*/
const getMedian = (listSorted = []) => {
// Get count.
const listCount = listSorted.length;
@nathansmith
nathansmith / web-design-development-learning-resources.md
Last active May 29, 2024 12:25
Resources for learning web design & front-end development
@nathansmith
nathansmith / [1] README.md
Last active May 28, 2024 19:17
Node script to extract unique domains from a `*.har` file.

How to use

To use this Node script:

  1. Save the contents of the script in a file named har-domain-parser.cjs.

  2. Place your trace.har file in the same directory as the script file.

  3. Then run the script using the command line.

@nathansmith
nathansmith / .htaccess
Created October 1, 2011 03:33
Route extension-less files to HTML.
# Route extension-less URLs to the equivalent *.html file.
# For instance: example.com/about = example.com/about.html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
</IfModule>
@nathansmith
nathansmith / [1] json-verbose.d.ts
Last active May 2, 2024 18:19
Example of JSON in TypeScript.
export type IJson = IJsonArray | IJsonObject | boolean | number | null | string;
export type IJsonArray = IJson[];
/*
=====
NOTE:
=====
This is an `interface` because the reference
@nathansmith
nathansmith / deepFreeze.ts
Last active April 15, 2024 15:56
Type safe, recursive `Object.freeze`.
// ======
// Types.
// ======
type DeepReadonly<T> = {
readonly [K in keyof T]: T[K] extends Record<PropertyKey, unknown> ? DeepReadonly<T[K]> : T[K];
};
// ===========================
// Helper: deep freeze object.
@nathansmith
nathansmith / linkedin-follow.js
Last active February 24, 2024 01:54
Console script to "follow back" everyone on LinkedIn.
/*
=====
NOTE:
=====
This works with LinkedIn's current markup, as of 2024-02-23.
Run it via the dev tools console, while on your "following" page.
https://linkedin.com/mynetwork/network-manager/people-follow/followers
*/
@nathansmith
nathansmith / js-class-footgun.md
Last active February 20, 2024 19:52
Example of how NOT to build a JavaScript class.

JS class footgun

I came across some code awhile ago that, though I can't share exactly, I wanted to document as a "footgun."

You should never have a class that can be used both as a singleton with helper methods and can be instantiated.

Even if you did, there should never be static and public methods — by the same name — that do different things.

/*
@nathansmith
nathansmith / center_select.html
Last active February 14, 2024 13:28
Example of how to "center" text in a `<select>` dropdown.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1"
/>
<title>