Skip to content

Instantly share code, notes, and snippets.

View trusktr's full-sized avatar
📜
writing code

Joe Pea trusktr

📜
writing code
View GitHub Profile
@nuxodin
nuxodin / style-once.js
Last active October 16, 2023 03:39
Polyfill "style once" hopefully standardised soon :)
const selector = 'link[rel="stylesheet"], style';
onElement(selector, function(el){ // NOT IMPLEMENTED IN THIS SCRIPT
// checks all styleSheets and importRules, todo store urls global and check all if used url found
// console.log(el.sheet)
checkAllSheets()
});
function checkAllSheets(){
const urls = {};
@calebdwilliams
calebdwilliams / mixin-annotated.js
Created July 15, 2020 14:17
Mixin annotation with JSDoc and TypeScript
import { LitElement } from 'https://cdn.pika.dev/lit-element@^2.3.1';
/** @typedef {new (...args: any[]) => any} Constructor */
/**
* @template {!Constructor} T
* @param {T} superclass - The class to extend
*/
const FormControlMixin = (superclass) =>
class FormControl extends superclass {
@peerreynders
peerreynders / reactIsAFramework.md
Last active March 23, 2024 02:34
React is a (view component) framework

"Art prior" to React:

Martin Fowler: InversionOfControl (2005-Jun-26)

Inversion of Control is a key part of what makes a framework different to a library. A library is essentially a set of functions that you can call, these days usually organized into classes. Each call does some work and returns control to the client.

A framework embodies some abstract design, with more behavior built in. In order to use it you need to insert your behavior into various places in the framework either by subclassing or by plugging in your own classes. The framework's code then calls your code at these points.

The litmus test:

  • If your code calls it, it's a library.
@Jonarod
Jonarod / blob_conversions_util.js
Created December 7, 2019 04:56
Javascript utility to convert Blob to Base64, ImageData or ObjectUrl back and forth. Tree shakeable and promise based.
const BlobToBase64 = function(blob){
let blobUrl = URL.createObjectURL(blob);
return new Promise((resolve, reject) => {
let img = new Image();
img.onload = () => resolve(img);
img.onerror = err => reject(err);
img.src = blobUrl;
}).then(img => {
URL.revokeObjectURL(blobUrl);
import * as THREE from 'three'
export function isRenderItem(obj: THREE.Object3D): obj is THREE.RenderItem & THREE.Object3D {
return 'geometry' in obj && 'material' in obj
}
export function disposeMaterial(obj: THREE.Object3D): void {
if (!isRenderItem(obj)) return
// because obj.material can be a material or an array of materials
@trusktr
trusktr / obliterate.js
Created February 20, 2019 00:15
Obliterate an object
// recursively deletes all properties within an `object` or `function`
// TODO option to also handle non-enumerable but configurable descriptors
function obliterate(obj: object) {
const visited = new WeakSet
_obliterate(obj)
async function _obliterate(obj) {
if (!obj || !(typeof obj === 'object' || typeof obj === 'function')) return
@justinfagnani
justinfagnani / Scoped-Custom-Element-Registries.md
Last active June 26, 2023 02:08
Scoped Custom Element Registries

Outdated - current proposal is at https://github.com/justinfagnani/scoped-custom-elements

Scoped Custom Element Definitions

Overview

Scoped Custom Element definitions is an oft-requested feature of Web Components. The global registry is a possible source of name collisions that may arise from coincidence, or from an app trying to define multiple versions of the same element, or from more advanced scenarios like registering mocks during tests, or a component explicitly replacing an element definition for its scope.

Since the key DOM creation APIs are global, scoping definitions is tricky because we'd need a machanis to determind which scope to use. But if we offer scoped versions of these APIs the problem is tractable. This requires that DOM creation code is upgraded to use the new scoped APIs, something that hopefully could be done in template libraries and frameworks.

@trusktr
trusktr / image-grid.md
Last active May 1, 2024 19:59
Image grid in Markdown
screen shot 2017-08-07 at 12 18 15 pm blah screen shot 2017-08-07 at 12 18 15 pm screen shot 2017-08-07 at 12 18 15 pm
@trusktr
trusktr / perf-tests.js
Last active August 26, 2018 23:40
Perf tests
function performanceTests(...testCases) {
const NUM_ITERATIONS = 5000000
let results = []
console.log(' -- Running tests... ')
for (let i=0, l=testCases.length; i<l; i+=1) {
results.push(testCases[i](NUM_ITERATIONS))
}
@jamesramsay
jamesramsay / README.md
Last active April 26, 2024 13:15
Gmail: delete old emails automatically

Gmail: delete old emails automatically

Automatically deletes old emails that match the specified label.

Get started

  • Create a new Google Apps Script at https://script.google.com
  • Overwrite the placeholder with the javascript below
  • Update the following constants:
  • LABEL_TO_DELETE: the label that should be have old messages deleted