Skip to content

Instantly share code, notes, and snippets.

@dfkaye
dfkaye / component-create.js
Last active March 20, 2023 05:21
factory pattern to create worker-like component objects or actors (eventually)
View component-create.js
// 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
View merge-style.js
// 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
View intersection.js
// 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
View is-class.js
// 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
View lift-parts.js
// 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...
View partial-equal.js
// 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 / Σμυρναιικος-μπαλος.txt
Created January 26, 2023 05:02
Σμυρναιικος μπαλος, Marika Papagika
View Σμυρναιικος-μπαλος.txt
έχεις το χρώμα της αυγής,
και τον αγγέλων μοιάζεις,
με τα γλυκά ματάκια σου,
τον ήλιο τον σκεπάζεις
you have the color of dawn,
and the angel you look like,
with your sweet eyes,
you cover the sun
@dfkaye
dfkaye / queueMicrotask-vs-promise-sequence.js
Created January 14, 2023 05:29
what is the order of processing when using queueMicrotask?
View queueMicrotask-vs-promise-sequence.js
// 13 January 2023
// what is the order of processing when using queueMicrotask?
// synchronous sequence
// microtask
// promise resolution
queueMicrotask((_) => {
console.log(_, "task");
});
@dfkaye
dfkaye / mutation-observer-on-detached-elements.js
Created December 31, 2022 18:42
MutationObserver works on detached Elements
View mutation-observer-on-detached-elements.js
// 31 december 2022
// MutationObserver works on detached Elements
// define an element to watch for changes
var targetNode = document.createElement("p");
targetNode.setAttribute("data-test", "initial value");
targetNode.innerHTML = "Some <b>bold</b> ideas.";
// define callback with reference to the targetNode
function callback(mutationList, observer) {
@dfkaye
dfkaye / diff-squares.js
Created December 27, 2022 04:57
math diversion: find difference between square of the sum of numbers {1 ,n} and the sum of the squares {1, n}
View diff-squares.js
// 26 Dec 2022
// A diversion.
// Find the difference between the square of the sum of the numbers
// from 1 to n, and the sum of the squares from 1 to n.
function squareOfSum(n) {
if (+n !== +n) {
return 0
}