This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function port(fn) { | |
return Function.prototype.bind.call(Function.call, fn); | |
} | |
// Usage: | |
// port the `forEach` method of the Array prototype | |
const each = port([].forEach); | |
// Use standalone by passing the contextual object as the first argument | |
each(document.querySelectorAll('div'), (el) => console.log(el)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isPlainObject(value) { | |
if (!value || typeof value !== 'object') { | |
return false; | |
} | |
const prototype = Object.getPrototypeOf(value); | |
return prototype === null || prototype === Object.getPrototypeOf({}); | |
} | |
// Usage: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function scope(obj, callback) { | |
const source = callback.toString(); | |
const vars = Object.getOwnPropertyNames(obj).map((key) => `${key} = this['${key}']`); | |
const body = source.substring(source.indexOf('{') + 1, source.lastIndexOf('}')); | |
const fn = new Function(`var ${vars.join(',')}; ${body}`); | |
fn.call(obj); | |
} | |
// Usage: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function updateAttribute(element, name, newVal, oldVal = null) { | |
if (name[0] === 'o' && name[1] === 'n') { | |
name = name.slice(2).toLowerCase(); | |
if (newVal) { | |
element.addEventListener(name, newVal); | |
} | |
if (oldVal) { | |
element.removeEventListener(name, oldVal); | |
} | |
} else if (newVal == null || newVal === false) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function callbackMiddlewareProxy(callback) { | |
const middleware = []; | |
const push = middleware.push.bind(middleware); | |
return new Proxy(callback, { | |
get: (target, prop) => { | |
if (prop === 'use') { | |
return push; | |
} | |
return target[prop]; | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function templit(tpl, tag) { | |
let exec; | |
return (data) => { | |
if (!exec) { | |
const vars = Object.getOwnPropertyNames(data).map((key) => `${key} = this['${key}']`); | |
exec = tag == null | |
? new Function(`var ${vars.join(',')}; return \`${tpl}\``) | |
: new Function('__tag', `var ${vars.join(',')}; return __tag\`${tpl}\``); | |
} | |
return tag == null ? exec.call(data) : exec.call(data, tag); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const has = {}.hasOwnProperty; | |
function getPath(root, tester, path = []) { | |
for (const key in root) { | |
if (has.call(root, key)) { | |
const value = root[key]; | |
if (tester(key, value) === true) { | |
path.push(key); | |
return path; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://stackoverflow.com/a/46759625 | |
function isConstructable(fn) { | |
try { | |
Reflect.construct(function () {}, [], fn); | |
return true; | |
} catch (e) { | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon#sending_analytics_at_the_end_of_a_session | |
const callbacks = []; | |
function unload(callback) { | |
callbacks.push(callback); | |
}; | |
document.addEventListener('visibilitychange', () => { | |
if (document.visibilityState === 'hidden') { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function *stateGenerator(state, reducer) { | |
let action; | |
while (true) { | |
if (action) { | |
state = reducer(state, action); | |
} | |
action = yield state; | |
} | |
} |