Skip to content

Instantly share code, notes, and snippets.

View jonathantneal's full-sized avatar

Jonathan Neal jonathantneal

View GitHub Profile
@jonathantneal
jonathantneal / filteredObject.ts
Created November 5, 2020 13:20
filteredObject: Returns a new object with all entries that pass the test implemented by the provided function.
/** Returns a new object with all entries that pass the test implemented by the provided function. */
function filteredObject<T>(object: { [s: string]: T } | ArrayLike<T>, predicate: (value: T, name: string, array: [string, T][]) => unknown): { [s: string]: T } {
const newObject = Object.create(Object(object).prototype)
const entries = Object.entries(object)
for (const [name, value] of entries) {
if (predicate(value, name, entries)) {
Reflect.set(newObject, name, Reflect.get(object, name))
}
}
return newObject
@jonathantneal
jonathantneal / README.md
Last active September 5, 2022 09:02
Create Fragment

Create Fragment

The createFragment function returns an HTML fragment from a string.

[
  { name: 'Hedral', coat: 'Tuxedo', legs: 4 },
  { name: 'Pillar', coat: 'Ticked Tabby', legs: 3 },
].reduce(
 (table, { name, coat, legs }) =&gt; {
@jonathantneal
jonathantneal / HTMLSelectorSlotElement.js
Created September 8, 2020 01:33
Selector Slot Element
HTMLSelectorSlotElement = ((internal, filter) => class HTMLSelectorSlotElement extends HTMLElement {
constructor() {
internal.set(super(), [
this.attachShadow({ mode: 'open' }),
document.createElement('slot'),
document.createElement('slot'),
new MutationObserver(HTMLSelectorSlotElement.prototype.connectedCallback.bind(this)),
[]
])
}
@jonathantneal
jonathantneal / README.md
Created September 7, 2020 18:50
Fetching Web Platform Data in NodeJS

Fetching Web Platform Data in NodeJS

Should there be a need to fetch web platform data in NodeJS, here are some dependency-less functions that will return feature data from W3C, CanIUse, and MDN Browser Compatibility Data.

Dependency-Free Fetching in NodeJS

NodeJS includes an http and https library which can perform network requests.

const https = require('https')
This file has been truncated, but you can view the full file.
[
{
"type": "Comment",
"value": "\n augmented-ui\n BSD 2-Clause License\n Copyright (c) James0x57, PropJockey, 2019\n",
"delimiterStart": "/*",
"delimiterEnd": "*/"
},
{
"type": "Space",
"value": "\n"
@jonathantneal
jonathantneal / README.md
Last active April 28, 2020 14:24
PostCSS Tokenizer
@jonathantneal
jonathantneal / events.js
Created April 10, 2020 20:12
Events: Polyfill missing Event constructors in Internet Explorer (864 bytes / 547 bytes gzipped)
!function () {
let WindowEvent = Event
try {
new WindowEvent('')
} catch (error) {
' Animation AudioProcessing BeforeUnload Clipboard Close Composition Custom Error Focus Gamepad HashChange IDBVersionChange Input Keyboard Message Mouse Mutation OfflineAudioCompletion PageTransition PaymentRequestUpdate Pointer PopState Progress Storage Track Transition UI WebGLContext Wheel'
.split(' ').forEach(function (eventName) {
eventName += 'Event'
let NativeEvent = window[eventName] || WindowEvent
let nativeEventName = NativeEvent === WindowEvent || eventName === 'InputEvent' ? 'Event' : eventName
@jonathantneal
jonathantneal / keyboard-key.js
Created April 9, 2020 14:33
KeyboardEvent Key: Polyfill the correct key names in Internet Explorer (577 bytes / 385 bytes gzipped)
!function () {
let KeyboardEventPrototype = KeyboardEvent.prototype
let KeyboardEventPrototypeKeyDescriptor = Object.getOwnPropertyDescriptor(KeyboardEventPrototype, 'key')
let KeyboardEventPrototypeKeyGetter = KeyboardEventPrototypeKeyDescriptor.get
KeyboardEventPrototypeKeyDescriptor.get = function () {
return {
27: 'Escape',
29: 'NonConvert',
32: ' ',
37: 'ArrowLeft',
@jonathantneal
jonathantneal / globalThis.js
Created April 9, 2020 14:30
globalThis: Defines globalThis in all environements (107 bytes, 107 bytes gzipped)
Object.prototype.__defineGetter__('_t', function () {
return this
}), _t.globalThis = _t, delete Object.prototype._t
@jonathantneal
jonathantneal / closest.js
Created April 8, 2020 23:55
Closest, but with an extra argument to specify the ancestor to look up until
closest = (function (Object, error) {
return function (targetElement, selectors, untilElement) {
let document = Object(Object(targetElement).ownerDocument)
let rootElement = Object(document.documentElement)
let Element = Object(document.defaultView).Element
untilElement = untilElement || rootElement
if (typeof Element === 'function' && targetElement instanceof Element && untilElement instanceof Element) {
while (untilElement.contains(targetElement)) {
if (targetElement.matches(selectors)) {
return targetElement