Skip to content

Instantly share code, notes, and snippets.

View shesek's full-sized avatar

Nadav Ivgi shesek

View GitHub Profile
@shesek
shesek / autocomplete-format.coffee
Created February 26, 2012 10:43
Custom URL format in jQuery-UI autocomplete source
autocompleteFormat = (url) -> (request, response) -> $.get (url.replace '%s', encodeURIComponent request.term), response, 'json'
# Usage
$('#search').autocomplete
source: autocompleteFormat '/search/%s.json'
@shesek
shesek / object-create.coffee
Created February 26, 2012 13:45
ES5 Object.create helpers
defaults = writable: true, enumerable: true, configurable: true
initializer = (key) -> (val) ->
prop = Object.create defaults
prop[key] = val
prop
value = initializer 'value'
getter = initializer 'get'
setter = initializer 'set'
@shesek
shesek / iter.coffee
Created February 27, 2012 09:16
CoffeeScript iteration helper
## Version 1 ##
# To continue to the next iteration, `return` instead of `continue`
# To stop iteration, `throw StopIteration` instead of `break`
StopIteration = {}
iter = (iterator, func) ->
try loop func do iterator
catch e then throw e unless e is StopIteration
return
# Usage
@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
@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 / 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 / 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 / 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 / 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 / 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