Skip to content

Instantly share code, notes, and snippets.

// ==UserScript==
// @name Word Filter
// @description Replace words with other words.
// @namespace http://daliwa.li
// @include *
// @version 1.0.0
// @grant none
// ==/UserScript==
@gr0uch
gr0uch / js_documentation.md
Last active August 29, 2015 14:13
Idea: JS documentation tool

There are not enough tools in the JS ecosystem for generating docs from source code. There is dox which seems to be popular, but it is not very configurable and a bit limited in functionality in that has no lexical understanding of code. The gold standard is jsdoc but it still has its own shortcomings. It is also rigid in its expectations of input, and outputs JSON in a predefined way. I want to write a new tool that instruments code so that it has an understanding of what is being documented, and modular serializers for inputs and outputs.

Example input, JSDoc style:

App.UsersController = Framework.ArrayController.extend({
  /**
   * Get a list of inactive users.
   *
 * @public
@gr0uch
gr0uch / errors.js
Last active August 29, 2015 14:14
Custom typed errors in ES6
// Hello. There is now a module for this.
// https://github.com/0x8890/error-class
// $ npm install error-class
const hasCaptureStackTrace = 'captureStackTrace' in Error
// Internal function to set up an error.
function setup (message) {
const { constructor, constructor: { name } } = this
@gr0uch
gr0uch / enumerate_methods.js
Last active August 29, 2015 14:14
Enumerate class methods
/**
* Get a hash of a class's methods. This is not so straightforward
* because class methods are not enumerable.
*
* @param {Class} cls
* @return {Object}'
*/
export default function enumerateMethods (cls = class {}) {
return Object.getOwnPropertyNames(cls.prototype)
.reduce((methods, method) => {
@gr0uch
gr0uch / subclass.js
Created February 27, 2015 20:47
One-line dependency injection in ES6
/**
* Dependency injection in ES6. Parentheses can be omitted for arrow functions with
* only one argument, and by omitting the curly braces it automatically returns
* the proceeding expression, in this case, the Subclass that extends from the
* Superclass that is injected. So cool.
*/
export default Superclass => class Subclass extends Superclass { ... }
@gr0uch
gr0uch / serial.js
Last active August 29, 2015 14:16
Serial execution of functions for flow based programming.
/**
* Take an array of functions that accept one argument and return a value or Promise,
* and invoke each function serially, passing the resolved value to the next function.
*
* @param {Array} fnArray
* @param {Mixed} initialValue
* @return {Promise}
*/
export default (fnArray, initialValue) =>
fnArray.reduce((chain, fn, index) =>
@gr0uch
gr0uch / object_map.js
Last active August 29, 2015 14:17
Immutable mapping on an object.
/**
* Immutable mapping on an object.
*
* @param {Object} object
* @param {Function} mapFunction
* @return {Object}
*/
export default (object, mapFunction) =>
Object.keys(object).reduce((clone, key) => {
clone[key] = mapFunction(object[key], key);
@gr0uch
gr0uch / CLASS_ANTIPATTERN.md
Last active May 29, 2018 09:44
Reconciling ES6 `class` and constructor anti-pattern.

The use of the new keyword in JavaScript is highly divisive, some say that you should avoid it entirely. ES6 classes may seem at odds with this line of thinking, but I will show how you can have your cake and eat it too. Consider a module that exports a class:

export default class {
  constructor () { /* Do stuff here. */ }
}

The only way to get an instance out of this module, or rather an object that inherits the prototype of the class object, is to use the new keyword, which some consider harmful. Personally, I am not against new, it provides implicit understanding that the returned value is an object with inherited properties.

@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)))
}
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...