Skip to content

Instantly share code, notes, and snippets.

View kenjione's full-sized avatar

Alex Chernyshev kenjione

View GitHub Profile
gsub = (source, pattern, replacement) ->
unless pattern? and replacement?
return source
result = ''
while source.length > 0
if (match = source.match(pattern))
result += source.slice(0, match.index)
result += replacement
source = source.slice(match.index + match[0].length)
@vjt
vjt / camelize.js
Created February 15, 2011 15:50
String.camelize
// I thought I needed it, but I didn't need it anymore,
// but I already implemented it. So, here we go, if you
// ever would need a Javascript camelize implementation
// you can freely use this :-).
// - vjt@openssl.it Tue Feb 15 16:49:52 CET 2011
jQuery.extend (String.prototype, {
camelize: function () {
return this.replace (/(?:^|[-_])(\w)/g, function (_, c) {
@lancejpollard
lancejpollard / awesome_nested_set_optimization_helper.rb
Created July 2, 2010 01:39
Goal: Convert entire hierarchy of models into tree in 1 database call, such that you can loop through them depth-first
def simple_nested_set(clazz) # Post for example
stack = [] # Post for example
result = []
clazz.all(:order => "lft").each do |node|
if stack.empty?
stack.push({:node => node, :children => []})
result << stack.last
next
end
if stack.last[:node].lft < node.lft && node.lft < stack.last[:node].rgt