Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am gr0uch on github.
  • I am daliwali (https://keybase.io/daliwali) on keybase.
  • I have a public key ASAcWbv-AUUj-bYbmmpNRSTEwcDE41wHx_ufhbhE-4enWAo

To claim this, I am signing this object:

// Don't do functional programming in JS. It's wasteful.
var shoppingCart = [ { price: 12.99 }, { price: 5.99 }, { price: 3.10 } ]
// Loops twice and does two function calls per iteration.
var fpSum = shoppingCart
.map(function (product) { return product.price })
.reduce(function (accumulator, price) { return accumulator + price }, 0)
// One loop, no unnecessary function calls.
var i, j, impSum = 0

Reactive programming utility function that defines a property on a target object to depend on the values of other properties. Uses object getters & setters.

Strawman:

// Returns result of first run.
dependentKeys(
  [ target, 'targetKey' ],
  [ object1, 'key1' ],
 [ object2, 'key2' ],
/**
* Given an object and an array of keys, walk over the keys on the object.
*
* For example:
* ```js
* walk({ a: { b: [ 0, { c: 2 } ] } }, [ 'a', 'b', 1, 'c' ]) // returns 2
* ```
*
* @param {Object} obj
* @param {String|Number[]} path
@gr0uch
gr0uch / subclass.js
Last active September 16, 2015 10:30
Subclassing without using `class` keyword.
const mixins = {
mixinMethod () {
console.log(`This is a mixin method.`)
return this
}
}
// Constructor definitions.
function Class () { console.log(`This is the constructor.`) }
function Subclass () { Class.apply(this, arguments) }
/**
* Take an iterable of Promises and invoke them serially, otherwise identical
* to the `Promise.all` static method.
*
* @param {Promise[]} promises
* @return {Promise}
*/
function serial (promises) {
const results = []
/**
* Generate an ASCII password with a given length.
*
* @param {Number} [length] - Length of password, defaults to 18.
* @return {String}
*/
export default function generatePassword (length = 18) {
return String.fromCharCode(...Array.apply(null, Array(length))
// Allow all ASCII characters that can be input via a standard US keyboard,
// except for the space character.
@gr0uch
gr0uch / example.js
Last active August 29, 2015 14:23
Use callback middleware functions as promises.
import promiseMiddleware from './promise_middleware'
import http from 'http'
import cors from 'cors'
http.createServer(async function (request, response) {
const middleware = promiseMiddleware.bind(null, request, response)
try {
await middleware((request, response, next) => next())
import fortune from 'fortune'
import express from 'express'
const store = fortune.create()
const server = express()
store.defineType('user', {})
server.use('/users', (request, response, next) => {
// Do something...
@gr0uch
gr0uch / union.js
Last active August 29, 2015 14:20
Get the union of arrays by means of the Set type.
/**
* Get the union of arrays with unique values by means of the Set type.
*
* @param {Array[]}
* @return {Set}
*/
function union () {
return new Set(arguments[0].concat(
...Array.prototype.slice.call(arguments, 1)))
}