Skip to content

Instantly share code, notes, and snippets.

@cef62
cef62 / event-emitter.js
Created November 21, 2016 13:34 — forked from domenic/event-emitter.js
Revealing constructor pattern event-emitter
// This event emitter emits events, but reserves the right to publish events to
// for its creator. It uses a WeakMap for true encapsulation.
const eesToEventMaps = new WeakMap();
export default class EventEmitter {
constructor(publisher) {
const eventMap = Object.create(null);
eesToEventMaps.set(this, eventMap);
@cef62
cef62 / git-helpers.js
Created November 15, 2016 15:15
Run npm install after post-merge hook only when package.json changes. Javascript only.
const { exec } = require('./helpers')
const changedFile = (filename, partial) => exec(
`git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD`,
`Something went wrong reading last git-pulled files list.`,
true
).then((stdout) => stdout.split('\n')
.some((name) => {
let match
if (partial) {
@cef62
cef62 / dynamic-mouse-events.js
Created October 11, 2016 13:45
Which is the preferred way to manage on-demand event like onMouseMove?
class Comp extends React.Component {
constructor(props) {
super(props)
this.onMouseDown = this.onMouseDown.bind(this)
this.onMouseMove = this.onMouseMove.bind(this)
this.onMouseUp = this.onMouseUp.bind(this)
this.state = { onMouseMove: null, onMouseUp: null }
}
import React from 'react'
const provideContext = (contextKey, contextType) => (
React.createClass({
childContextTypes: {
[contextKey]: contextType
},
getChildContext() {
const { children, ...props } = this.props
@cef62
cef62 / post-merge
Created July 8, 2016 08:08 — forked from GianlucaGuarini/post-merge
Git hook that gets triggered after any 'git pull' whenever one of the files specified has changed. Useful to update any web application dependency using bower npm or composer
#/usr/bin/env bash
# MIT © Sindre Sorhus - sindresorhus.com
# forked by Gianluca Guarini
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
check_run() {
echo "$changed_files" | grep -E --quiet "$1" && eval "$2"
}
@cef62
cef62 / redux.in.react.js
Created July 6, 2016 07:09
Redux without Redux in React
// User.js
const UPDATE_USER = 'UPDATE_USER'
const defaultUser = {
displayName: null
}
function userReducer(state = defaultUser, action) {
switch (action.type) {
@cef62
cef62 / Component-composition.js
Last active May 13, 2016 07:24
Voxxed Days Ticino 2016 - Code Snippets
function compose (...fns) {
return (...args) =>
fns.reduceRight((child, fn) =>
fn.apply(null, child ? [child] : args), null
)
}
const b = i => <b>{i}</b>
const i = i => <i>{i}</i>
const u = i => <u>{i}</u>
@cef62
cef62 / README.md
Created March 19, 2016 17:51 — forked from johnlindquist/README.md
Angular 2 Automated Autocomplete

Angular2 Starter Gist Run

@cef62
cef62 / gist:5ca90fe87f38f2f5f801
Created February 10, 2016 23:14 — forked from danieleds/gist:326903084a196055a7c3
CodeMirror: indent with tab or spaces
editor.addKeyMap({
"Tab": function (cm) {
if (cm.somethingSelected()) {
var sel = editor.getSelection("\n");
// Indent only if there are multiple lines selected, or if the selection spans a full line
if (sel.length > 0 && (sel.indexOf("\n") > -1 || sel.length === cm.getLine(cm.getCursor().line).length)) {
cm.indentSelection("add");
return;
}
}
@cef62
cef62 / redux-like-approach.js
Created February 5, 2016 09:42
Example of Redux without Redux
// see: https://github.com/rackt/redux/issues/1367#issuecomment-180073370
import reducer, { increment, decrement } from 'redux-counter'
class App extends Component {
constructor() {
super()
this.state = reducer(undefined, {})
}