Array.from({ length: 100 }, (v, i) => [[15, 'fizzbuzz'], [5, 'buzz'], [3, 'fizz']].find(x => (i+1) % x[0] === 0) || (i + 1))
.reduce((a, o) => a += (o[1] || o) + "\n",'')
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Promise = require('bluebird'); | |
const fetch = require('node-fetch'); | |
const cheerio = require('cheerio'); | |
const getAllUrls = () => { | |
return fetch('http://regioni.usyouthsoccer.org') | |
.then(res => res.text()) | |
.then((html) => { | |
const $ = cheerio.load(html); | |
return $('#associationscarousel').find('> li > a').toArray().map(link => $(link).attr('href')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cartReducer = (state = { | |
cartItems: 0, | |
items: products, | |
cartContents: [], | |
}, action) => { | |
switch (action.type) { | |
case "ADD_TO_CART": { | |
const existingItem = state.cartContents.find(item => { | |
return item.id === Number(action.payload); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import BonusContext from './bonus-context'; | |
class App extends Component { | |
static contextType = BonusContext; | |
render() { | |
return( | |
<li className={ this.context ? 'current' : '' }> | |
<NavLink | |
exact |
This pattern takes advantage of lexical scope in javascript, CommonJS modules, and object destructuring. It's a good way to share stateful resources, such as a db connection object, to middleware and detached routers in Express.
/app.js:
const express = require('express')
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// some helper fns.. maybe in a different file or not | |
function validateEmail(email){ | |
return false; | |
} | |
export default (db) => { | |
// define some route handlers | |
function onPostCheckMail (req, res) { | |
if(validateEmail(req.body.email) === true){ //validateEmail is undefined |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
routeConfig.post('/Dashboard/Index', repos.DashboardRepository.getRangeData(repos), (req, res) => { | |
let queries = [], | |
sess = req.userSession, | |
filter = req.rangeFilter; | |
queries.push(repos.PromotionRepository.getAgencyPromotions(sess.agencyId, 'all')); | |
queries.push(repos.DashboardRepository.getIndex(repos, filter, sess)); | |
queries.push(repos.PolicyRepository.getPolicies(filter, true)); | |
Promise.all(queries) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// app.js | |
// ... | |
const models = require('./models/'); | |
const state = { | |
models, | |
logger, | |
errorReporter, | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
startTimer() { | |
return process.hrtime() | |
}, | |
stopTimer(timer) { | |
const [s, ns] = process.hrtime(timer) | |
return ((s * 1e9) + ns) / 1e6 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** when wrapping a function with many other functions, the identity of the | |
original function quickly gets lost, and it's hard to make sense of it | |
at a glance */ | |
const Timer = inject("someStore")(observer(class Timer extends React.Component { | |
/* ... */ | |
})) | |
/** you can make it less hard to read by separating your component's definition | |
and it's functional wrappers **/ |
NewerOlder