Skip to content

Instantly share code, notes, and snippets.

View shesek's full-sized avatar

Nadav Ivgi shesek

View GitHub Profile
@shesek
shesek / express-multi-view-directories.coffee
Last active December 14, 2015 01:58
Monkey patch express to support multiple view directories
View = require 'express/lib/view'
View::lookup = do ({ lookup } = View) -> (path) ->
return lookup.call this, path unless Array.isArray @root
return abs for root in @root when (abs = lookup.call { root }, path)?
###
Usage:
require 'express-multi-view-directories'
app.set 'views', ['...', '...', '...']
@shesek
shesek / sync.coffee
Created February 17, 2013 20:25
Decorate an synchronous function that returns a value to be asynchronous-like and pass the return value to a callback argument
sync = (fn) -> (a..., cb) ->
try cb null, fn a...
catch e then cb e
@shesek
shesek / mock-require.coffee
Last active December 13, 2015 16:39
Require a file, and mock the modules returned to it from require()
Module = require 'module'
# native_module isn't public, mock the needed functionality
NativeModule = do (natives = Object.keys process.binding 'natives') ->
exists: (name) -> name in natives
require: require
mock = (request, parent, modules) ->
return modules[request] if request of modules
return NativeModule.require request if NativeModule.exists request
@shesek
shesek / package.json
Last active March 24, 2016 10:49
setTimeout and setInterval with nicer syntax for CoffeeScript, purely for syntactic sugar
{ "name": "timer-sugar", "version": "0.0.1", "main": "timers.coffee", "license": "BSD" }
module.exports = (req, res, next) ->
return do next unless req.is 'text/plain'
req.body = ''
req.on 'data', (data) -> req.body += data
req.on 'end', next
@shesek
shesek / etter.coffee
Last active December 11, 2015 19:48
getter, setter and lazy
multi = (fn) -> (obj, name, a) ->
if a? then fn obj, name, a
else fn obj, name_, a_ for name_, a_ of name
def = (obj, name, prop) ->
prop.configurable ?= true
prop.enumerable ?= true
Object.defineProperty obj, name, prop
module.exports = exports =
@shesek
shesek / browserify-add-code.coffee
Last active December 11, 2015 19:29
Add inline client-side JavaScript with browserify
# Adds a function as an inline entry to browserify
# Useful for short "glue" code that just needs to require a
# bounch of things and initilize them.
get_proto = Object.getPrototypeOf or (o) -> o.__proto__
count = 0
addCode = (b, fn) -> b.addEntry 'main' + (if ++count > 1 then count else '') + '.js', body: addCode.body fn
addCode.body = (fn) -> '(' + fn + ')()'
@shesek
shesek / callable.coffee
Last active January 14, 2019 15:37
Decorate constructor functions, and have them return a callable object that delegates to a `callable()` method when invoked.
##########
### Moved to https://github.com/shesek/callable-klass
##########
"use strict"
# without strict mode, `this` defaults to window, so `(this?obj)`
# would always return window.
callable = (ctor) ->
@shesek
shesek / client.coffee
Last active December 11, 2015 14:48
Inline client-side JavaScript with browserify and express
browserify = require 'browserify'
client = (mount, fn) -> (browserify { mount }).addEntry 'main.js', body: '(' + fn + ')()'
# Usage:
app = express()
app.use client '/client.js', ->
foo = require './foo'
bar = require 'bar'
foo 123, bar, 456
@shesek
shesek / partial-impl.coffee
Last active December 10, 2015 23:30
possible implementations for partial application with pre-evaluated bound arguments and function target in CoffeeScript. See https://github.com/jashkenas/coffee-script/pull/2597
# With an helper function:
partial = (fn, a...) -> (b...) -> fn a..., b...
add_3 = partial add, 3
# With an IIFE:
add_3 = do (fn=add, a=3) -> (b...) -> fn a, b...
# note: because we know the number of bound arguments at compile-time, the do() IIFE could be
# compiled with the exact number of arguments, rather than with a splat.
# the do thingy is the same as `add_3 = ( (fn, a) -> (b...) -> fn a, b... ) add, 3`