Skip to content

Instantly share code, notes, and snippets.

View typedModel.ts
type TypedDispatcher<R> = R extends (state: any, payload?: infer P, meta?: infer M) => any
? (payload?: P, meta?: M) => Action<P, M>
: R extends (state: any, payload: infer P, meta?: infer M) => any
? (payload: P, meta?: M) => Action<P, M>
: R extends (state: any, payload: infer P, meta: infer M) => any
? (payload: P, meta: M) => Action<P, M>
: null;
type StripUnknown<T> = T extends (payload?: unknown, meta?: unknown) => Action<unknown, unknown>
? () => Action<never, never>
@Draccoz
Draccoz / extension.js
Last active Oct 4, 2017
Generates a CUID and copies it to clipboard.
View extension.js
const St = imports.gi.St;
const Main = imports.ui.main;
const Shell = imports.gi.Shell;
const Clipboard = St.Clipboard.get_default();
const CLIPBOARD_TYPE = St.ClipboardType.CLIPBOARD;
const namespace = 'cuid';
const blockSize = 4;
const base = 36;
@Draccoz
Draccoz / destroyable.js
Created Dec 20, 2016
Destroyable web components mixin
View destroyable.js
const Destroyable = (superclass, destroyTimeout) => class extends superclass {
detachedCallback() {this.__destroying = setTimeout(() => { if (!this.isConnected && this.destroyedCallback) this.destroyedCallback() }, destroyTimeout)},
attachedCallback() {clearTimeout(this.__destroying)}
};
@Draccoz
Draccoz / transform.js
Created Dec 8, 2016
CSS Transform getter and setter from single template
View transform.js
function buildPatterns(order) {
let orderMap = {};
let orderArray = order.split("|");
orderArray.forEach((t, i) => orderMap[ t ] = i);
return (consts, ...vars) => {
const escapePars = [ /(\(|\))/g, "\\$1" ];
const map = {
number: { match: `([\\de.-]+)`, cast: Number },
string: { match: `([\\w.-]+)`, cast: String },
@Draccoz
Draccoz / oarray.js
Last active Dec 2, 2016
Observable Array (POC, very slow)
View oarray.js
const mediator = new WeakMap();
const notify = (instance, ev, index, item) => mediator.get(instance)[ev].forEach(listener => listener({index, item}));
class OArray extends Array {
constructor() {
super();
mediator.set(this, {changed: [], added: [], removed: []});
}
push(...items) {
for (let i = 0, tl = this.length, l = items.length; i < l; i++) {
let item = items[i];
@Draccoz
Draccoz / config
Created Oct 14, 2016
Nginx config for wrapping js imports as html5 imports
View config
location ~/html-imports/(.+)$ {
set $path $1;
default_type 'text/html';
content_by_lua 'ngx.say("<script src=\'", ngx.var.path, ".js\'></script>")';
}
View helpers.ts
Object.prototype.getPath = function(path) {
return path
.split(".")
.reduce((obj, p) => obj ? obj[p] : undefined, this);
}
Object.prototype.setPath = function(path) {
return path
.split(".")
.reduce((obj, p) => typeof obj[p] === 'object' ? obj[p] : (obj[p] = {}), this);
@Draccoz
Draccoz / polymer-expression-binding-behavior.js
Last active May 17, 2016
Google Polymer behavior allowing to use simple javascript expressions inside of the templates
View polymer-expression-binding-behavior.js
(function (global) {
var dynamicFunctionsNames = {};
var cached = {};
function expressBinding(match, p) {
var fun;
var attrs = p
.replace(/&amp;/g, "&") // decode &
.replace(/&lt;/g, "<") // decode <
.replace(/&gt;/g, ">") // decode >