Skip to content

Instantly share code, notes, and snippets.

View WebReflection's full-sized avatar
🎯
Focusing

Andrea Giammarchi WebReflection

🎯
Focusing
View GitHub Profile
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 / module-structure.md
Last active December 5, 2017 14:36
Covering all possible Browser oriented modules cases.

structure

esm/index.js  <= the real stuff
cjs/index.js  <= the CommonJSified stuff
index.js      <= the unminified browser bundle
min.js        <= the minified browser bundle

esm/index.js

@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 / date-i18n.js
Created October 22, 2017 16:27
simple swapped entries demonstration
i18n.set('en')`Date: ${'month'}/${'day'}/${'year'}`
.for('it')`Data: ${'year'}/${'month'}/${'day'}`;
// default
const d = new Date;
i18n.locale = 'en';
i18n`Date: ${d.getMonth() + 1}/${d.getDate()}/${d.getFullYear()}`
// "Date: 10/22/2017"
i18n.locale = 'it';
@WebReflection
WebReflection / testing-i18n.js
Created October 22, 2017 16:17
Basic test for i18n
// default
i18n`Hello ${'World'}`
// "Hello World"
// we switch to German
i18n.locale = 'de';
// but we still write in English
i18n`Hello ${'World'}`
// "Hallo World"
@WebReflection
WebReflection / i18n.hello-world.js
Created October 22, 2017 16:08
Setting an "Hello World" for i18n
i18n.set('en')`Hello ${'name'}`
.for('de')`Hallo ${'name'}`
.for('it')`Ciao ${'name'}`
.for('sp')`Hola ${'name'}`;
@WebReflection
WebReflection / i18n.set.js
Created October 22, 2017 16:06
A simpel way to define the i18n.db
i18n.set = locale => (tCurrent, ...rCurrent) => {
const key = tCurrent.join('\x01');
let db = i18n.db[locale] || (i18n.db[locale] = {});
db[key] = {
t: tCurrent.slice(),
v: rCurrent.map((value, i) => i)
};
const config = {
for: other => (tOther, ...rOther) => {
db = i18n.db[other] || (i18n.db[other] = {});
@WebReflection
WebReflection / i18n.js
Created October 22, 2017 15:58
i18n in 10 lines of code
function i18n(template) {
for (var
info = i18n.db[i18n.locale][template.join('\x01')],
out = [info.t[0]],
i = 1, length = info.t.length; i < length; i++
) out[i] = arguments[1 + info.v[i - 1]] + info.t[i];
return out.join('');
}
i18n.locale = 'en';
i18n.db = {};