Skip to content

Instantly share code, notes, and snippets.

View alex-cory's full-sized avatar
🔥
🤔

Alex Cory alex-cory

🔥
🤔
View GitHub Profile
@alex-cory
alex-cory / isObject.js
Created September 15, 2021 23:29
isObject to test if a variable is actually only an Object
const isObject = v => Object.prototype.toString.call(v) === '[object Object]'
Object.entries({
'{}': {},
Set: new Set([1,2,3,4]),
Date: new Date(),
null: null,
undefined: undefined,
string: 'string',
NaN: NaN,
/* Syntax */
import useRouter, { routes } from 'use-next-router'
// see this to implement: https://stackoverflow.com/questions/67764930/proxy-route-strings
// routes.api.posts => '/api/posts'
// routes.api.posts({ postId: 'ID' }) => '/api/posts/ID'
// routes.api.posts({ postId: 'ID' }).query({ name: 'alex' }) => '/api/posts/ID?name=alex'
// routes.api.posts.query({ name: 'alex' }) => '/api/posts?name=alex'
// routes.api.profiles({ profileId: 'ID' }).posts({ postId: 'ID' }).query({ name: 'alex' }) => '/api/profiles/ID/posts/ID?name=alex'
import { useEffect, useRef } from 'react'
import { isMobile } from 'react-device-detect'
export const getElement = (id = '') => {
if (typeof window === 'undefined') return console.warn('getElement - no window object available')
// if we are passing an element as the ID
if (typeof id !== 'string') return id
return document[id.startsWith('.') ? 'querySelector' : 'getElementById'](id)
}
@alex-cory
alex-cory / clear-all-indexedDB.js
Created December 16, 2021 02:33
Clears out all the indexedDB databases
indexedDB.databases().then(dbs => {
var promises = dbs.map(db => {
return new Promise((resolve, reject) => {
var req = indexedDB.deleteDatabase(db.Name);
req.onsuccess = resolve;
req.onerror = reject;
req.onblocked = reject;
});
});
Promise.all(promises).then(console.log).catch(console.error);
@alex-cory
alex-cory / next-video.md
Last active January 7, 2022 22:53
Spec for react video hook

Next Video

Goals

  • make video event listeners behave the same across all browsers
  • simplify the syntax
  • reduce bundle size compared to libraries like video.js, and others with sizes of ~15MB
  • improve performance
    • reduce renders
    • in PWAs using videos - ex: storing local videos
/**
* Outputs
* 🌐 - API request -- potential others 🗺️ 🌎
* 💥❌ - useEffect unmount -- potential others 🚫
* 💥 - useEffect --
* ??? - in render function
* ??? - callback function
*/
const toEmoji = (num) => {
if (num === null || num === undefined || Number.isNaN(num)) return '⛔';
if (num === 10) return '🔟';
if (!isFinite(num)) return '🔁';
return String(num)
.replace('-', '−')
.replace('.', '⏺️')
.replace(/\d/g, digit => `${digit}️⃣`)
.replace(/⏺️(\d{0,2})\d*/, (match, p1) => `⏺️${p1}`);