Skip to content

Instantly share code, notes, and snippets.

View HoraceShmorace's full-sized avatar

Horace Nelson HoraceShmorace

View GitHub Profile
@HoraceShmorace
HoraceShmorace / useIntersectionObserver.js
Created May 17, 2024 19:17
useIntersectionObserver hook
/**
*
* Custom hook for setting up an intersection observer on a DOM element to determine its visibility relative to a specified viewport or the default viewport. Implements the native `IntersectionObserver` JavaScript class.
*
* @param {React.RefObject} ref - Required. The React ref object pointing
* @param {Object} options - The options for setting up the observer.
to the DOM element to observe.
* @param {Element|null} [options.root=null] - The element that is used as the viewport for checking visibility of the target. Defaults to the browser viewport if not specified.
* @param {string} [options.rootMargin="0px"] - The margin around the viewport or "root". Can have values similar to the CSS margin property, e.g. "10px 20px 30px 40px" (top, right, bottom, left).
* @param {number} [options.threshold=0.5] - A single number or array of numbers indicating at what percentage of the target's visibility the observer's callback should execute. Ranges from 0 to 1.
@HoraceShmorace
HoraceShmorace / threejs-hierarchy-disposal.js
Created January 1, 2022 23:20
Disposes of any Three.js object and any disposal objects in the hierarchy below it by doing a depth-first search
/**
* Disposes of any Three.js object (mesh, material, etc.) and any disposable objects in the hierarchy below it by doing a depth-first search.
* @param {Boolean} showLogging Output logs useful for troubleshooting.
* @example
* const trash = HierarchyDisposal(true)
* trash([some three.js object])
*/
function HierarchyDisposal (showLogging = false) {
const bag = []
@HoraceShmorace
HoraceShmorace / gist:0801578559b1d9fa104d1baa2656a498
Last active March 19, 2021 13:13
A simple function to list all S3 objects in a given bucket
const AWS = require('aws-sdk')
const s3 = new AWS.S3({ apiVersion: '2006-03-01', region: 'us-east-1' })
const params = {
Bucket: 'YOUR_BUCKET_NAME'
}
/**
* `s3.listObjects` has a max result set size of 1000. This function iterates to list all objects in the bucket.
*
* @example
@HoraceShmorace
HoraceShmorace / auto-order-amazon-fresh.js
Last active April 14, 2020 19:52 — forked from scarrillo/amazon-delivery-slots.js
Amazon.com: Check for delivery slots for Fresh and Whole Foods
const ALLOW_AUTO_PURCHASE = false
const CHECKOUT_URL = 'https://www.amazon.com/gp/buy/shipoptionselect/handlers/display.html?hasWorkingJavascript=1'
const { pathname } = location
const isShipOptionPage = /buy\/shipoptionselect/.test(pathname)
const isPaySelectPage = /buy\/payselect/.test(pathname)
const isPurchasePage = /buy\/spc/.test(pathname)
const isThankYouPage = /buy\/thankyou/.test(pathname)
let attemptNum = 1
@HoraceShmorace
HoraceShmorace / calcCompoundedInterest.js
Last active March 28, 2019 22:07
Calculate simple earnings with compounded interest (margins)
const MAX_PERIODS = 52
const MARGIN_PER_PERIOD = 0.05
function calc(v, t) {
console.log(v)
if (t === 0) return v;
const n = (v * MARGIN_PER_PERIOD) + v
return calc(n, --t)
}
@HoraceShmorace
HoraceShmorace / palindrome_via_array.php
Last active July 11, 2018 13:40
Palindrome checker class
<?php // https://www.testdome.com/questions/php/palindrome/7320?questionIds=7320,11840&generatorId=30&type=fromtest&testDifficulty=Easy
class Palindrome {
public static function isPalindrome($word) {
$word = str_split(strtolower($word));
for($i = 0; $i < ceil(count($word)/2); $i++) {
$leftChar = $word[$i];
$rightChar = $word[count($word)-$i-1];
if($leftChar !== $rightChar) {
return FALSE;
}
@HoraceShmorace
HoraceShmorace / ethereum-zombie-game.js
Last active January 3, 2018 20:22
Ethereum Zombie Game
import web3 from 'web3'
var abi = '' /* TODO: abi generated by the compiler */
var ZombieFeedingContract = web3.eth.contract(abi)
var contractAddress = '' /* TODO: our contract address on Ethereum after deploying */
var ZombieFeeding = ZombieFeedingContract.at(contractAddress)
// Assuming we have our zombie's ID and the kitty ID we want to attack
let zombieId = 1
let kittyId = 1
/**
* Calculates whether an HTML element has been viewed, according to the criteria in the options argument.
* @param {Object} options An object with the following properties:
* minArean {Number} The minimum % of visible area to qualify as viewed.
* minWidth {Number} The minimum % of visible width to qualify as viewed.
* minHeight {Number} The minimum % of visible height to qualify as viewed.
* @param {Function} next An optional callback function.
* @returns {Boolean} Whether the element has been viewed.
*/