Skip to content

Instantly share code, notes, and snippets.

View bendc's full-sized avatar

Benjamin De Cock bendc

View GitHub Profile
@bendc
bendc / functional-inheritance.js
Last active July 9, 2016 16:03
Functional inheritance
function car() {
return {
start: function() {
console.log("Engine on.")
},
accelerate: function() {
console.log("Let's go!")
}
}
}
@bendc
bendc / define.js
Created July 8, 2016 09:24
Friendlier object definition
const define = (object, ...pairs) =>
(Array.isArray(pairs[0]) ? pairs : [pairs]).reduce((accumulator, pair) => {
const [ key, value ] = pair;
return Object.defineProperty(accumulator, key, { value, enumerable: true });
}, object);
@bendc
bendc / toRGB.js
Created May 11, 2015 20:16
Hexadecimal to RGB converter
const toRGB = (() => {
const expand = hex =>
hex.length < 7 ? hex.split("").reduce((a, b) => a + b + b) : hex;
const convert = hex =>
hex.match(/[\d\w]{2}/g).map(val => parseInt(val, 16));
return hex => {
const [r, g, b] = convert(expand(hex));
return `rgb(${r}, ${g}, ${b})`;
};
})();
@bendc
bendc / immutable.js
Created January 6, 2016 16:56
Deep freeze objects
const immutable = (() => {
const getValue = obj => key => obj[key];
const isObject = obj => Object(obj) === obj;
const isMutable = obj => isObject(obj) && !Object.isFrozen(obj);
return obj => {
Object.keys(obj).map(getValue(obj)).filter(isMutable).forEach(immutable);
return Object.freeze(obj);
};
})();
@bendc
bendc / cloneMap.js
Created October 25, 2016 08:07
Deep clone a Map containing JSON-compatible data
const clone = map =>
new Map(JSON.parse(JSON.stringify([...map])));
@bendc
bendc / objectmap.js
Created October 26, 2016 08:00
Object mapping
const odd = { a: 1, b: 3 };
const even = Object.entries(odd).reduce((object, pair) => {
const [key, value] = pair;
object[key] = value + 1;
return object;
}, {});
console.log(even); // => { a: 2, b: 4 }
@bendc
bendc / hello-message-render.js
Created January 4, 2017 16:48
Web Components: hello-message render
document.body.innerHTML = "<hello-message>Jane</hello-message>";
@bendc
bendc / hello-message-slot.js
Created January 5, 2017 13:07
Web Components: hello-message-slot
customElements.define("hello-message", class extends HTMLElement {
constructor() {
super();
const root = this.attachShadow({mode: "open"});
root.innerHTML = "Hello <slot></slot>";
}
});
@bendc
bendc / hello-message-attribute.js
Created January 5, 2017 13:11
Web Components: hello-message-attribute
customElements.define("hello-message", class extends HTMLElement {
constructor() {
super();
const root = this.attachShadow({mode: "open"});
root.innerHTML = "Hello <slot></slot>";
}
static get observedAttributes() {
return ["name"];
}
@bendc
bendc / todo-list-basic.js
Created January 6, 2017 10:57
Web Components: todo-list-basic
const state = ["Buy milk", "Call Sarah", "Pay bills"];
customElements.define("todo-item", class extends HTMLElement {
constructor() {
super();
const root = this.attachShadow({mode: "open"});
root.innerHTML = `
<label>
<input type=checkbox>
<slot></slot>