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 / 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 / florian-eberhart-linkedin-spam.md
Last active February 5, 2024 13:11
Florian Eberhart - LinkedIn spam

Florian Eberhart - LinkedIn spam

The following is a message I shared on LinkedIn, posted 2024-02-04. I figured it might be handy to have a public facing page. That way, one does not need to be on LinkedIn to read it. If anyone wants to pick this up as part of a larger content moderation story, you have my permission.

🔗 Original post on LinkedIn


I want to give a shout-out to Florian Eberhart for being especially popular amongst LinkedIn spam bots. I have received several identical messages encouraging me to register for his upcoming event. At the time of this writing, it looks like 14 people are planning to attend.

@nathansmith
nathansmith / 1_README.md
Last active December 28, 2023 23:30
🐶 Example of cached fetch promise

For work, I made an example of how to cache a fetch promise.

This is different than awaiting a promise and saving the result. Subsequent calls will use the same promise.

It outputs alternating logs:

  • Image object.

  • String "bad dog".

// Helper function to determine error message.
const getErrorMessage = (error: Error | ErrorEvent | PromiseRejectionEvent): string => {
// Set later.
let message = '';
// Message string?
if (typeof (error as Error)?.message === 'string') {
message = (error as Error)?.message;
// Reason string?
@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 / try-catch-promise-examples.js
Created December 8, 2022 18:04
Promise snippets I put together, to share with coworkers.
// ============
// Helper: log.
// ============
const log = (error) => {
console.log(error?.message || error);
}
// ===============
// Promise reject.
@nathansmith
nathansmith / [1] test-setup.js
Last active September 27, 2022 13:37
Jest test setup
require('@testing-library/jest-dom');
// Override.
Object.defineProperty(window, 'requestAnimationFrame', {
writable: true,
value: (f) => {
if (typeof f === 'function') {
f();
}
},