Skip to content

Instantly share code, notes, and snippets.

@dfkaye
dfkaye / hex2rgb.js
Created May 7, 2024 07:25
hex2rgb, a hex to rgb JavaScript string conversion function that's faster than binary operators and bit shifting...
// 6 May 2024
// hex to rgb
// made you look
// originary defamatory statement
// https://twitter.com/the_moisrex/status/1787444601571221892
// uncomplicated, performs well enough (suite runs under 1ms).
// supports 3 or 6 character strings with or without leading octothorpe.
@dfkaye
dfkaye / create-microtask.js
Last active May 6, 2024 21:26
use queueMicrotask and friends to get away from IIFEs and semicolons
// 5 may 2024
// use queueMicrotask and friends to get away from IIFEs and semicolons
'use strict';
(function () {
console.group("collision")
try {
@dfkaye
dfkaye / promise-handler.js
Created May 2, 2024 05:02
promise handler that returns promises as { value } or { error: { message } } structures
// 26 April 2024
// promise handler that returns promises as {value} or {error: {message}}
// structures - that is, a resolved promise with a value field, or a rejected
// promise with an error field that in turn contains a message field.
// goaded by Austin Gil's tweet 25 April 2024:
// https://twitter.com/heyAustinGil/status/1783622633000710321
// his solution, slightly modified...
/*
function IP(p) {
@dfkaye
dfkaye / array-queue.js
Last active May 1, 2024 07:36
array queue, two implementations
// 12-13 April 2024
// array queue
// first implementation uses a single array and deletes slots on "pull", while
// using "push" to append new items.
// second implementation uses a single array but uses "splice" for both "push"
// and "pull" commands, the benefit being we don't have to manage empty slots;
// however, splice results in the array indexes being shuffled internally...
@dfkaye
dfkaye / custom-set-attribute.js
Created March 22, 2024 06:37
How to create a custom setAttribute method on a non-DOM object and reflect the attribute as a property.
// 3 December 2023
// How to create a custom setAttribute method on a non-DOM object
// and reflect the attribute as a property.
// 3 December
// Expanded by adding get and remove attribute methods.
// 21 March 2024
// Added hasAttribute method.
// Added logic to reflect a property as an attribute.
@dfkaye
dfkaye / fake-fetch-iife.js
Last active March 21, 2024 07:07
fake fetch iife closure test
// 20 March 2024
// fake fetch iife closure test
// Twitter posed a question at
// https://twitter.com/jcubic/status/1770519375944040682
// So I answered. I've since been blocked. *shrug*
// Here's what I get with a fake fetch, F:
// 8 March 2024
// check that given value contains no values or is an empty array, string, object...
// from simplegraph repo 2014
// https://github.com/dfkaye/simplegraph/
// simpler than the is-empty.js gist 5 Jan 2018
// https://gist.github.com/dfkaye/f87272098c4e19e4d55a6a18515aefce
function empty(o) {
@dfkaye
dfkaye / element-to-xpath.js
Created March 4, 2024 08:52
generate an XPath expression for an element in an HTML document
// 3 March 2024
// generate an XPath expression for an element in an HTML document
// ONLY WORKS FOR ELEMENTS SO FAR, NO FANCY NODE TYPE SELECTION YET
// for an element tree structure such as
// <a><b><c><d id="test"> this is <e> embedded </e> text </d></c></b></a>
// the XPath expression for window.test (id="test") should be
// //BODY[1]/A[1]/B[1]/C[1]/D[1]
// and passing that to document.evaluate() should return
@dfkaye
dfkaye / xslt-document-fn.js
Last active March 2, 2024 23:08
import XML source documents into XSLT using the document() function.
// 2 March 2024
// import XML source documents into XSLT using the document() function.
// example markup borrowed from
// https://www.abbeyworkshop.com/howto/xslt/document/
// parsing function defined in gist 19 June 2023,
// "Using XML, XSLT, XHR, to parse and serialize HTML in the browser",
// https://gist.github.com/dfkaye/94bc03241c4c3bf458ab0dc5d56b1958,
async function parse({ text = "", type = "" }) {
@dfkaye
dfkaye / forms-elements-access-patterns.js
Created February 16, 2024 06:28
many ways to access forms and elements in the DOM
// 15 February 2024
// many ways to access forms and elements in the DOM
var input = document.createElement("input");
input.name = "E";
var form = document.createElement("form");
form.name = "F";
form.appendChild(input);
document.body.appendChild(form);