Skip to content

Instantly share code, notes, and snippets.

const moment = require('moment');
const PERIOD = moment.duration(3, 'days');
module.exports = {
PERIOD,
isWithinPeriod(test) {
return moment().add(PERIOD).isAfter(test);
},
};
#!/usr/bin/env node
// skip the first two args as they are node and this script
const [,,filepath] = process.argv;
doSomethingFSLike(filepath);
const {
topLevel: {
intermediate, // declaration of intermediate
intermediate: {
nested // declaration of nested
}
}
} = {
topLevel: {
intermediate: {
function defaults({ a = 1, b = 2 } = {}) {
console.log('a', a);
console.log('b', b);
}
defaults();
// a 1
// b 2
defaults({ a: 42, b: 32 });
function defaults({ a, b } = { a: 1, b: 2 }) {
console.log('a', a);
console.log('b', b);
}
defaults();
// a 1
// b 2
defaults({ a: 42, b: 32 });
// simple renaming to avoid collisions or just as a preference.
const a = 5;
const { a: b } = { a: 3 };
console.log(b); // prints 3
// rename for another API and use enhanced object literal notation.
function verifyUser({ userId: id }) {
return User.update({ id, verified: true });
}
// can destructure arrays
const [a, b] = [1,2]; // a = 1, b = 2
// can destructure object keys
const { a } = { a: 3 }; // a = 3
// even function parameters
function foo({ a }) {
console.log(a);
}
@emilong
emilong / component.jsx
Created May 27, 2016 20:35
Creating semantic links for react-router using HOCs
import React, { Component } from 'react'
import { SubWidgetLink } from './routes'
export default class MyComponentWithALink extends Component {
render() {
const { widget } = this.props
return (
<div>
...
import { combineReducers } from 'redux'
import { createAction, handleActions } from 'redux-actions'
const authorFetched = createAction('AUTHOR_FETCHED')
const bookFetched = createAction('BOOK_FETCHED')
const authorReducer = handleActions({
AUTHOR_FETCHED: (state, action) => {
const author = action.payload
import { combineReducers } from 'redux'
import { createAction, handleActions } from 'redux-actions'
const authorFetched = createAction('AUTHOR_FETCHED')
const bookFetched = createAction('BOOK_FETCHED')
const literaryReducer = handleActions({
// maybe an author has a bunch of books as sub-resources
AUTHOR_FETCHED: (state, action) => {
const author = action.payload