Skip to content

Instantly share code, notes, and snippets.

View tbranyen's full-sized avatar

Tim Branyen tbranyen

View GitHub Profile
@tbranyen
tbranyen / cjs.js
Last active August 30, 2018 18:47
CJS to ESM
const something = require('something');
const { somethingElse } = require('something-else');
const { exports } = module;
exports.somethingElse = somethingElse;
module.exports = something;
Object.assign(module.exports, exports);
@tbranyen
tbranyen / webapp-node-api.js
Last active August 20, 2018 04:50
WebApp Node API Example (Experimental JS Bundler) w/ Virtual Modules
const { webapp, register } = require('webapp');
async function main() {
const input = register('./input.js')`
const msg = 'Hello world!';
console.log(msg);
`;
const { source } = await webapp({
input,

Functions rescue us in this case because functions are lazy. But now, we can’t chain these things with .then() anymore.

function lazyPromise() {
  return new Promise(resolve => setTimeout(resolve, 1000))
    .then(() => console.log('Chain off all'));
}
// The only location you can't chain off is the function

// Chains just fine this way
@tbranyen
tbranyen / concept.js
Last active August 28, 2017 11:40
DOM Diffing Specification Concept
// Create virtual document and documentElement.
const vdoc = document.createVirtualDocument();
const root = vdoc.createDocumentElement('html');
// <template/> tags should be able to be converted as well.
const someTemplate = document.querySelector('template#some-template');
// Add some SVG.
root.appendChild(vdoc.createElement({
nodeName: 'svg',
namespaceURI: 'http://www.w3.org/2000/svg',
@tbranyen
tbranyen / acceleration.sh
Created July 28, 2017 22:39
ThinkPad X1 Carbon 5th Generation libinput acceleration
touchpad_id=$(xinput --list | grep "TouchPad" | xargs -n 1 | grep "id=" | sed 's/id=//g')
accel_speed_code=$(xinput --list-props $touchpad_id | awk '/Accel Speed \(/ {print $4}' | grep -o '[0-9]\+')
# Default acceleration is too slow (non-existent)
xinput --set-prop $touchpad_id $accel_speed_code .75
const { render } = require('react-dom');
const mount = document.querySelector('main');
class Root {
render() {
return (
<div>Hello world</div>
);
}
}
@tbranyen
tbranyen / example.js
Last active July 6, 2017 16:49
Getting to React Component w/ diffHTML
import { innerHTML } from 'diffhtml'
// Start simple...
innerHTML(document.body, `
<marquee>Go for it!</marquee>
`)
// Move to template engine if you want
innerHTML(document.body, mustache.render(`
<div>
class AbortablePromise extends Promise {
constructor(fn) {
let _reject = null;
super((resolve, reject) => {
_reject = reject;
return fn(resolve, reject);
});
this.abort = () => {
@tbranyen
tbranyen / _setup.sh
Created May 29, 2017 04:03
Web Components <3 JSDOM
npm i jsdom-wc@11.0.0-alpha-1
// Allow the user to invoke the Custom Element chain.
function makeElement(tagName, props) {
const Constructor = customElements.get(tagName);
if (Constructor) {
return new Constructor(props);
}
else {
document.createElement(tagName);
}