Skip to content

Instantly share code, notes, and snippets.

View jf990's full-sized avatar

John Foster jf990

View GitHub Profile
@jf990
jf990 / fileUploadSafe.php
Created June 23, 2025 16:06
PHP ideas and helpers
<?php
// User uploads a profile picture (jpg or png), try to verify it is a safe file.
// Helpful info about uploading files in PHP: https://www.php.net/manual/en/features.file-upload.php
// use finfo to learn the file MIME type: https://www.php.net/manual/en/function.finfo-open.php
$finfo = new finfo(FILEINFO_MIME_TYPE);
for each($_FILES[‘pictures’][‘error’] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmpName = $_FILES[‘pictures’][‘tmp_name’][$key];
@jf990
jf990 / tagParse.js
Last active May 16, 2025 19:12
parseTags() function to convert a string list of tags into an array of individual tags
/**
* Parse a string of tags into an array of individual tags. Tags are either delimited
* with a ; or a /, but only one or the other. It also weeds out things we don't want
* such as empty or invalid tags.
* @param {string} A string of tags, each tag separated by a / or a ;.
* @returns {Array} An array of the individual tag strings, one tag per array element.
*/
function parseTags(tags) {
const tagsNotAllowed = ["shit", "fuck", "dick"];
const tagsNormalized = tags.trim().toLowerCase();
@jf990
jf990 / createButton.js
Last active May 16, 2025 18:59
Dynamically create a buttom element
// Create button usage:
const button = createButton(document.body, "Act now!", action, "warning");
/**
* Create a button element.
* @param {DOMElement} A parent element to append the new button element to. If not provided the new button element is not added to the DOM but it is retured so the caller can decide what to do with it.
* @param {string} A label for the button.
* @param {function} An event handler function to call when the button is clicked.
* @param {string} A button class variant, the default is primary. See https://getbootstrap.com/docs/5.3/components/buttons/#variants.
* @param {string} Additional classes to assign to the button element class list. This should be null or an empty string or a list of class names.
@jf990
jf990 / favicon.md
Created February 6, 2025 22:13
Web page favicon images
@jf990
jf990 / utilities.js
Last active January 28, 2025 01:49
Various utility and helper functions
/**
* Count the number of times each unique word appears in the string of text. The text is
* normalized to lowercase and
* @param {string} text A string of text to process.
* @returns {array} An array of key/value pairs where the key is the word encountered in
* the text string and the value is the count of occurrences of that word in the text.
*/
function determineWordWeights(text) {
const stopWords = [
@jf990
jf990 / macos-cheatsheet.md
Last active August 22, 2025 18:12
List of helpful macOS command line utilities

macOS Cheatsheet

Many hidden and OS files are not shown in Finder. In a Finder window, ⌘ + ⇧ + . to show all files in Finder.

Start up

From this window you can start up from a different disk, start up in safe mode, use macOS Recovery, and more.

  1. Power down the computer.
  2. Press and hold the power button during start up.
@jf990
jf990 / windows-cheatsheet.md
Last active May 22, 2025 19:11
Windows cheatsheet

Windows cheatsheet

Screenshot shortcuts

  • Print Screen: Copies an image of your entire screen to the clipboard. You can find the Print Screen button in the upper-right corner of most keyboards, or next to the Space Bar on some. To paste the screenshot, press Ctrl + V.
  • Windows Logo Key + PrtScn: A shortcut for print screen, depending on your hardware.
  • Alt + PrtScr: A shortcut for capturing a single active window.
  • Fn + Windows Logo Key + Space Bar: A shortcut for taking a screenshot if your device doesn't have the PrtScn button.
  • Windows Logo Key + Shift + S: A shortcut for a static image snip in the Snipping Tool. Image is saved to ~/Pictures/Screenshots.
  • Windows Logo Key + Shift + R: A shortcut for a video snip in the Snipping Tool.
@jf990
jf990 / setClassList.js
Last active February 5, 2025 01:06
Update an elements class definition by removing and adding classes
/**
* Remove and add classes to any element. Removes are performed before adds.
* @param {string} Selector to target. Must resolve to a single element.
* @param {string|array} Classes to add. If using single string, can only be a single class. Otherwise use array of strings for each class to add.
* @param {string|array} Classes to remove. If using single string, can only be a single class. Otherwise use array of strings for each class to remove.
*/
function setClassList(selector, classesToAdd, classesToRemove) {
let element = document.querySelector(selector);
if (Array.isArray(classesToRemove) && classesToRemove.length > 0) {
@jf990
jf990 / screen-resolution.js
Last active November 6, 2024 00:51
Responsive design screen breakpoints
const screenSizes = ["sm", "md", "lg", "xl", "2xl"];
const screenWidthPx = {
sm: "640px",
md: "768px",
lg: "1024px",
xl: "1280px",
"2xl": "1536px"
};
const screenWidth = {
sm: 640,
@jf990
jf990 / scrolltext.html
Created September 23, 2024 20:07
A text typing animation like you see in video games
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no" />
<title>HTML console type text animation</title>
<style>
.introContainer {
width: 90%;
max-width: none;