Skip to content

Instantly share code, notes, and snippets.

Avatar

crazy4groovy

View GitHub Profile
@crazy4groovy
crazy4groovy / dynamic-script-text.js
Last active Nov 25, 2020
How to dynamically load script via Promse in JavaScript https://tinyurl.com/yxtf55bn
View dynamic-script-text.js
function loadScript(id, innerHTML) {
var script = document.createElement("script");
script.type = "text/javascript";
script.id = id || Math.random().toString().substr(2);
script.innerHTML = innerHTML;
document.body.appendChild(script);
}
// load script text
loadScript(null, "\
@crazy4groovy
crazy4groovy / README.md
Last active Nov 3, 2020
Having an < iframe > that behaves just like a YouTube embed with great for performance.
@crazy4groovy
crazy4groovy / readSheet.js
Created Oct 14, 2020
Read public-readable Google Speadsheet as a CSV or JSON
View readSheet.js
// see: https://andregarzia.com/2018/11/reading-google-sheets-data-from-livecode.html
async function readSheet(key, sheetName = 'Sheet1', format = 'csv' /* 'json' */) {
const url = `https://docs.google.com/spreadsheets/d/${key}/gviz/tq?tqx=out:${format}&sheet=${sheetName}`
return fetch(url).then(r => r.text())
}
@crazy4groovy
crazy4groovy / package.json
Last active Oct 13, 2020 — forked from kentcdodds/package.json
setup script for my workshops
View package.json
{
"name": "workshop-setup",
"version": "1.0.0",
"description": "This is the common setup script for most of my workshops",
"bin": "./setup.js"
}
@crazy4groovy
crazy4groovy / ObjPropertyAccessChecker.js
Last active Sep 2, 2020
Metaprogramming with Proxies
View ObjPropertyAccessChecker.js
// https://exploringjs.com/deep-js/ch_proxies.html#proxy-use-cases
const propertyCheckerHandler = {
get(target, propKey, receiver) {
// Only check string property keys
if (typeof propKey === 'string' && !(propKey in target)) {
throw new ReferenceError('Unknown property: ' + propKey);
}
return Reflect.get(target, propKey, receiver);
}
@crazy4groovy
crazy4groovy / findLogestFileName1.js
Last active Sep 2, 2020
Given an array of Directories/Filenames, find the longest filename
View findLogestFileName1.js
function processDir(d, par = '') {
let dirnm = par
return d.map(item => {
if (Array.isArray(item)) { // this is a folder
return processDir(item, dirnm)
}
dirnm = par + '/' + item
if (item.indexOf('.') > -1) { // this is a filename
return dirnm
}
@crazy4groovy
crazy4groovy / decksFromCards1.js
Last active Aug 24, 2020
Given an array of cards, how many full decks (52 cards) can be created?
View decksFromCards1.js
const getNewDeck = () => [
"2S", "2C", "2D", "2H", "3S", "3C", "3D", "3H", "4S", "4C", "4D", "4H", "5S", "5C", "5D", "5H", "6S", "6C", "6D", "6H", "7S", "7C", "7D", "7H", "8S", "8C", "8D", "8H", "9S", "9C", "9D", "9H", "TS", "TC", "TD", "TH", "JS", "JC", "JD", "JH", "QS", "QC", "QD", "QH", "KS", "KC", "KD", "KH", "AS", "AC", "AD", "AH"
]
const removeItem = (arr, i) => {
arr.splice(i, 1)
}
function decksFromCards(cards) {
let decks = [getNewDeck()]
@crazy4groovy
crazy4groovy / getCssSelector.js
Last active Jul 16, 2020
css-selector-generator inline
View getCssSelector.js
function __css__handler (cb) {
return function (event) {
// event.preventDefault();
var selector = CssSelectorGenerator.default(event.target);
cb(selector);
}
}
// track every DOM node click
document.body.addEventListener('click', __css__handler(alert));
@crazy4groovy
crazy4groovy / Dockerfile
Created Jun 10, 2020
Build a "distroless" NodeJS docker image
View Dockerfile
FROM node:12.18.0 AS build-env
ADD . /app
WORKDIR /app
RUN npm ci --only=production
# https://github.com/GoogleContainerTools/distroless
# - considered experimental and not recommended for production usage!
# https://github.com/GoogleContainerTools/distroless/blob/master/examples/nodejs/Dockerfile
FROM gcr.io/distroless/nodejs
COPY --from=build-env /app /app
View little-vdom-decompiled.js
/* eslint-disable no-unused-vars */
/* eslint-disable no-else-return */
// JSX constructor, similar to createElement()
export const h = (type, props, ...children) => {
return {
type,
// Props will be an object for components and DOM nodes, but a string for
// text nodes
props,
You can’t perform that action at this time.