Skip to content

Instantly share code, notes, and snippets.

View shesek's full-sized avatar

Nadav Ivgi shesek

View GitHub Profile
@shesek
shesek / switchx.coffee
Created April 20, 2012 03:29
CoffeeScript: tiny DSL for switch-like function based control flow
switchx = (value, fns...) -> return ret for fn in fns when ret=fn value
whenx = (pred, fn) -> (val) -> (fn val) or true if pred val
defaultx = (fn) -> (val) -> fn val
# example
jump = (p) -> p.takeEnergy JUMP_ENERGY
canJump = (p) -> p.getEnergy() > JUMP_ENERGY
# ...
@shesek
shesek / generator.coffee
Created March 5, 2012 08:29
CoffeeScript generator function decorator
# Very simple implementation for generators in CoffeeScript. Obviously not as powerful as real generators,
# and only useful for iterating over values (pulling values via `.next()` is quite impossible to implement
# with a natural `yield` syntax in the generator), but still quite useful and provides an elegant syntax.
# The `generator` function is used as a decorator for functions. The function passed to `generator` gets
# called with the `this` context having a `yield` property that should be used to yield values to consumers.
# The function returned by calling `generator` should than be called with a function to consume the values.
generator = do ->
StopIteration = {}
@shesek
shesek / cunit.coffee
Last active October 1, 2015 09:58
Lightweight CoffeeScript unit testing
# Just a fun experiment I hacked together :-)
## Basic handling
results = current = {}
group = (format) -> (name, func) ->
previous = current
(current.sub ||= []).push current = name: format.replace '%s', name
try
previous.beforeEach?()
do func
@shesek
shesek / class-decor.coffee
Created March 2, 2012 10:24
CoffeeScript class decorators
# Just playing around with some ideas...
interface = (names...) -> (klass) -> throw new Error "Missing #{n}" for n in names when not klass::[n]?; klass
extender = (props) -> (klass) -> klass::[k]=v for k,v of props; klass
watchable = interface 'watch', 'unwatch'
writable = interface 'write'
evented = extender
on: (event, callback) -> do stuff
trigger: (name, args...) -> do stuff
@shesek
shesek / trac-project.js
Created March 1, 2012 21:22
Trac multi project UI enhancement
/*
* Just some small UI enhancement for multi-projects. In my setup, I have an `project` custom field and
* "project/" prefixes for the component, version and milestones fields (e.g. "AwesomeProject/1.2" as a,
* version, "SomeProject/User" as a component, etc). This JavaScript hides the irrelevant components/versions/
* milestones on the ticket creation/edit forms according to the selected project, adds some navigation
* to the roadmap page to only show milestones of a specific project and creates links for the project-specific
* roadmap page (using the hash).
*/
@shesek
shesek / returnx.coffee
Created March 1, 2012 20:48
CoffeeScript: Returning from internal anonymous functions
# Allows return values to be returned from anonymous functions within other functions. The function that
# the value should be returned from should be decorated with the `catchx` function, and the value should
# be returned using the `returnx` function. This is done by throwing the return value using `returnx`,
# than catching it inside the decorator and `return`ing it.
# Was previously part of a specific solution for iterators at https://gist.github.com/1953057
catchx = returnx = null
do ->
ReturnValue = (@val) ->
@shesek
shesek / iter-returnex.coffee
Created March 1, 2012 20:35
CoffeeScript iteration helper - with "return exception" decorator
# With a `returnex` function decorator for returning exception values
# Previous versions at https://gist.github.com/1922656
iter = returnex = null
do ->
StopIteration = {}
NextIteration = {}
ReturnValue = (@val) ->
helpers =
continue: -> throw NextIteration
@shesek
shesek / create.coffee
Created February 29, 2012 03:32
CoffeeScript object creating helper
# A tiny helper for creating objects with dynamic keys. I found myself repeating the following pattern all over my code:
doStuff = ->
obj = {}
obj[k] = foo for k in bar()
obj
# Which is not very elegant, and in many cases forces me to expand one-line functions into multiple lines.
# Instead, its possible to move the repeated pattern into a separate function:
@shesek
shesek / defaultcols.py
Created February 27, 2012 11:27
Default columns for Trac query
# Adds a new `default_cols` option under [query] with a comma-separated list of default columns
# See http://trac-hacks.org/wiki/DefaultColsPlugin
from trac.core import *
from trac.web.main import IRequestFilter
from trac.config import ListOption
class DefaultCols(Component):
implements(IRequestFilter)
@shesek
shesek / privates.coffee
Created February 27, 2012 09:37
JavaScript per-instance private storage area
# A simple implementation to get a private storage area for objects.
# Also allows setting default properties for the storage area.
# Written in CoffeeScript: (with an added `export` function. On the regular version, use `exports.privates = ...`)
export privates = do ->
counter=0
(proto) ->
key = "__id#{ ++counter }"
store = []
(object) -> store[object[key] ||= store.length] ||= Object.create proto or null