Skip to content

Instantly share code, notes, and snippets.

@dfkaye
dfkaye / value-atom.js
Last active May 12, 2024 05:24
chapter 8 Data oriented programming non-blocking Atom
// 11 May 2024
// chapter 8 Data oriented programming
// atomic value swap alg
function Atom (state) {
return {
state,
atomicCompareAndSet(current, previous, next) {
return current === previous ? (state = next, true) : false;
@dfkaye
dfkaye / regex-email-address.js
Created May 10, 2024 20:51
email valid regex quick and dirty from Kyle Simpson
// 10 May 2024
// Not mine! The regexp is from a tweet by Kyle Simpson
// https://twitter.com/getifyX/status/1788895673821118886
var reEmailAddress = /^[^@]+@[^@.]+(?:\.[^@.]+)+$/;
["a@b.c", { toString: () => "a@b.c" }, ["a@b.c"]].map(email => reEmailAddress.test(email));
// Array(3) [ true, true, true ]
@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