Skip to content

Instantly share code, notes, and snippets.

View WebReflection's full-sized avatar
🎯
Focusing

Andrea Giammarchi WebReflection

🎯
Focusing
View GitHub Profile
@WebReflection
WebReflection / irc.js
Created February 15, 2018 08:27
Transform IRC logs into Markdown
#!/usr/bin/env node
require('fs').readFile(process.argv[2], (err, data) => {
if (err) return;
const content = data.toString().trim();
const re = /^\[(.+?)\]\s+<(.+?)>\s(.*)$/gm;
const chat = [];
let current = {};
while (match = re.exec(content)) {
if (match[2] !== current.name) {
@WebReflection
WebReflection / paint.js
Last active February 11, 2018 17:15
Grants UI setup and future state through rAF
var paint = function(rAF){ 'use strict';
return function paint(before) {
before();
return rAF(before), {
then: function (after) {
return rAF(rAF.bind(null, after)), this;
}
};
};
}(
@WebReflection
WebReflection / jsc
Last active October 2, 2024 23:49
JavaScriptCore for macOS and Linux
#!/usr/bin/env bash
if [ -f /usr/lib/jsc ]; then
/usr/lib/jsc "$@"
elif [ -f /System/Library/Frameworks/JavaScriptCore.framework/Resources/jsc ]; then
/System/Library/Frameworks/JavaScriptCore.framework/Resources/jsc "$@"
fi
const clone = (() => {
/*! (c) Andrea Giammarchi - WTFPL */
const toString = {}.toString;
const flags = ['global', 'ignoreCase', 'multiline', 'sticky', 'unicode'];
const prime = obj => typeof obj === 'object' ? new obj.constructor(obj.valueOf()) : obj;
const through = obj => {
const descriptors = Object.getOwnPropertyDescriptors(obj);
Reflect.ownKeys(descriptors).forEach(key => {
const descriptor = descriptors[key];
if ('value' in descriptor) {
@WebReflection
WebReflection / fixEslintAllman.js
Created January 16, 2018 11:07
fix allman style with brackets losing initial padding
function fixEslintAllman(code) {
return code.replace(/^{/mg, function ($0, $1) {
var newLine = code.lastIndexOf('\n', $1 - 2);
var spaces = code.slice(newLine + 1, $1 - 1).match(/^(\s*)/)[1];
return spaces + '{';
});
}
function removeColumn($0) {
let tr = $0.closest('tr');
const index = [].indexOf.call(tr.querySelectorAll($0.nodeName), $0);
$0.remove();
tr = tr.closest('thead').nextElementSibling.querySelector('tr');
do {
let td = tr.querySelectorAll('td');
if (td.length) td[index - 1].remove();
else tr.querySelectorAll('th')[index].remove();
} while (tr = tr.nextElementSibling);
@WebReflection
WebReflection / prevaluation.js
Created November 22, 2017 14:13
Try to execute a Gist pre content and it copies it to clipboard.
document.documentElement.appendChild(
document.createElement('style')
).textContent = `
.executable {
transition: all .3s;
}
.executable:hover {
cursor: pointer;
background: #fff !important;
}`;
@WebReflection
WebReflection / hyper-nitty-gritty.js
Last active May 30, 2023 06:09
hyperHTML, the nitty gritty
// used to retrieve template content
const templates = new WeakMap;
// used to retrieve node updates
const updates = new WeakMap;
// hyperHTML, the nitty gritty
function hyperHTML(chunks, ...interpolations) {
// if the static chunks are unknown
@WebReflection
WebReflection / json-channel.js
Last active July 29, 2022 12:01
A GJS WebKit JSON Communication Channel Example
const JSONChannel = (Private => class JSONChannel {
// (c) Andrea Giammarchi - @WebReflection (ISC)
constructor(secret=Array.from(
crypto.getRandomValues(new Uint8Array(8))
).map(i => i.toString(36)).join('')) {
const listener = e => this.emit('message', null, e.detail);
document.addEventListener(`${secret}:gjs`, listener);
Private.set(this, {
secret, listener,
fn: Object.create(null)
@WebReflection
WebReflection / template-content.js
Created August 30, 2017 08:30
micro shim for template content
(function (document, TEMPLATE) {
var container = document.createElement(TEMPLATE);
if ('content' in container) return;
var createElement = document.createElement;
var descriptor = {
get: function () { return this.content ? this.content.innerHTML : ''; },
set: function (html) {
var el = createElement.call(document, TEMPLATE);
if (/^[^\S]*?<(t(?:head|body|foot|r|d|h))/i.test(html)) {
var selector = RegExp.$1;