Skip to content

Instantly share code, notes, and snippets.

@dfkaye
dfkaye / innerHTML-vs.-DOMParser.js
Last active July 23, 2023 06:12
Element.innerHTML vs. DOMParser.parseFromString(html)
// 6 May 2023
// Element.innerHTML vs. DOMParser.parseFromString(html)
// 7 May 2023
// added unbalanced tags, escaped HTML, event handler attribute cases.
// 22 July 2023
// Thanks to sharp eyes (Justin Fignani) who found several assertions were flat
// out wrong, probably due to running these tests in the console, frequently the
// incognito windows which have different CSP settings depending what "blank" page
@dfkaye
dfkaye / event-emitter.js
Created April 8, 2023 05:11
ECMA2023 Event Emitter example modified from Wikipedia Event-Driven Architecture page
// 7 April 2023
// JavaScript event emitter from Wikipedia's Event-Driven Architecture page
// https://en.wikipedia.org/wiki/Event-driven_architecture
// Modified from the original.
// Interesting for the ECMA2023 class features and Map, Set, et al
(() => {
'use strict';
@dfkaye
dfkaye / attribute-vs-property.js
Last active April 1, 2023 03:49
effects of adding or removing a property on elements with or without their corresponding attribute
// 26 March 2023
// effects of adding or removing a property on elements with or
// without their corresponding attribute.
// continued from merge-style gist where it was discovered that adding a style property
// creates a style attribute on elements without one initially.
// [ in progress: lots of cases to think through... ]
// I use `var` because I re-run these console tests repeatedly in the browser.
@dfkaye
dfkaye / component-create.js
Last active March 20, 2023 05:21
factory pattern to create worker-like component objects or actors (eventually)
// 8-9 March 2023
// component.create
// factory pattern to create worker-like component objects or actors (eventually)
// no big plans for expansion, just an illustration; however,
// a couple methods trying to wriggle into the implementation include:
// 1. extend()...? maybe.
// 2. terminate() + onterminate()...? maybe.
// suppose we could turn this into a closure and track each created object
@dfkaye
dfkaye / merge-style.js
Last active March 7, 2023 05:39
style replace vs. style merge - differences between replacing vs. merging style attribute and style properties
// 3-6 March 2023
// style attributes vs. style properties
// differences between replace vs. merge
// replacing props vs. replacing attributes
// no surprises
// merging props vs. merging attributes
// at least one surprise, namely, that adding a style property
// creates a style attribute on elements without one initially.
@dfkaye
dfkaye / intersection.js
Last active March 2, 2023 06:58
intersections from more than two arrays
// 2 March 2023
// intersections from more than two arrays
// goaded by https://frontendroom.com/intersection-of-multiple-arrays-in-js/
// works on arrays of values and arrays of objects with a single key;
// not sure I agree with the multiple keys matching output.
// values
var a = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6", "Item 7"]
@dfkaye
dfkaye / is-class.js
Created February 25, 2023 21:09
detect whether something is a class in JavaScript, not just a function; warning: this is not tamper-proof
// 25 February 2023
// detect whether something is a class in JavaScript, not just a function.
// warning: this is not tamper-proof.
// @webreflection figured this out
// https://stackoverflow.com/questions/30758961/how-to-check-if-a-variable-is-an-es6-class-declaration/75567955#75567955
// The test?
// The variable is typeof function and its prototype field is not writable.
@dfkaye
dfkaye / lift-parts.js
Created February 17, 2023 22:25
lift parts out of a value using the Object constructor guard
// 17 February 2023
// lift parts out of a value
// using the Object constructor guard
function lift(v, key) {
var o = Object(v);
return Object.hasOwn(o, key)
? o[key]
: undefined;
@dfkaye
dfkaye / partial-equal.js
Last active February 7, 2023 22:57
partial-equal in JavaScript: compare an object's values to a minimal "schema", ignoring extra fields on the compared object, and more...
// 5 February 2023
// partial-equal in JavaScript
// inspired by https://brandur.org/fragments/partial-equal
// compare an object's values to a minimal "schema" which can be a primitive,
// allows extra fields on the compared object but expected fields must match
// expected values, including symbols as field names and field values;
// first failure terminates, logs a warning.
@dfkaye
dfkaye / template-render.js
Created February 1, 2023 22:04
modified template literal data rendering example from MDN site
// 1 February 2023
// modified template literal data rendering example from
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
// from https://gist.github.com/dfkaye/0597d0bfbfefbd31b72731c2f543b85e
function parseHTML(str) {
return new DOMParser().parseFromString(String(str).trim(), 'text/html').body;
};