Combine default parameters and destructuring for a compact version of the defaults / overrides pattern.
function foo ({
bar = 'no',
baz = 'works!'
} = {}) {
/** Returns a simple relative time string | |
* @example | |
* nicetime(new Date()-1e4) == 'Just now' // within the last minute | |
* nicetime(Date.now()-1e6) == '16m' // within the last hour | |
* nicetime(Date.now()-1e7) == '2h' // within the last hour | |
* nicetime('Dec 31, 2014') == '4d' // within the last 7 days | |
* nicetime('Dec 25, 2014') == 'Dec 25' // over 7 days ago gives a simple date | |
* nicetime('July 4, 2014') == 'Jul 4, 2014' // over half a year ago adds the year | |
*/ | |
function nicetime(text) { |
var webpack = require('webpack'); | |
var HtmlWebpackPlugin = require('html-webpack-plugin'); | |
var path = require('path'); | |
var folders = { | |
APP: path.resolve(__dirname, '../app'), | |
BUILD: path.resolve(__dirname, '../build'), | |
BOWER: path.resolve(__dirname, '../bower_components'), | |
NPM: path.resolve(__dirname, '../node_modules') | |
}; |
atom-text-editor::shadow, | |
.tree-view-resizer { | |
::-webkit-scrollbar { | |
width: 2px; | |
height: 2px; | |
} | |
::-webkit-scrollbar-track { | |
background: #1a2127; | |
} | |
::-webkit-scrollbar-thumb { |
import mongooseResource from '../lib/mongoose-resource'; | |
import Foo from '../models/foo'; // a mongoose Model | |
export default mongooseResource('foo', Foo); |
// ES6 w/ Promises | |
// Note: From a React starter template - see https://t.co/wkStq8y3I5 | |
function fetchData(routes, params) { | |
let data = {}; | |
return Promise.all(routes | |
.filter(route => route.handler.fetchData) | |
.map(route => { | |
return route.handler.fetchData(params).then(resp => { | |
data[route.name] = resp; |
The 0.13.0
improvements to React Components are often framed as "es6 classes" but being able to use the new class syntax isn't really the big change. The main thing of note in 0.13
is that React Components are no longer special objects that need to be created using a specific method (createClass()
). One of the benefits of this change is that you can use the es6 class syntax, but also tons of other patterns work as well!
Below are a few examples creating React components that all work as expected using a bunch of JS object creation patterns (https://github.com/getify/You-Dont-Know-JS/blob/master/this%20&%20object%20prototypes/ch4.md#mixins). All of the examples are of stateful components, and so need to delegate to React.Component
for setState()
, but if you have stateless components each patterns tends to get even simpler. The one major caveat with react components is that you need to assign props
and context
to the component instance otherwise the component will be static. The reason is
/** | |
* Basic proof of concept. | |
* - Hot reloadable | |
* - Stateless stores | |
* - Stores and action creators interoperable with Redux. | |
*/ | |
import React, { Component } from 'react'; | |
export default function dispatch(store, atom, action) { |
var setImmediate = (function(f, q, p, reg) { | |
q = []; | |
f = document.createElement('iframe'); | |
f.style.cssText = 'position:absolute;top:-999em;left:0;width:1px;height:1px;'; | |
document.body.appendChild(f); | |
function done() { | |
for (var i=0; i<q.length; i++) q[i](); | |
q.length = 0; | |
} | |
p = [ |
/** | |
* var jpr = new JSONPRequest(); | |
* jpr.onload = (...data) => { | |
* console.log(data); | |
* }; | |
* jpr.open('GET', 'some-url?callback={{callback}}'); | |
* jpr.send(); | |
*/ |