Skip to content

Instantly share code, notes, and snippets.

Handlebars.registerHelper('short_string', function(context, options){
//console.log(options);
var maxLength = options.hash.length || 100;
var trailingString = options.hash.trailing || '';
if(context.length > maxLength){
return context.substring(0, maxLength) + trailingString;
}
return context;
});
Handlebars.registerHelper('addition', function(context, options){
return context + parseFloat(options.hash.to);
});
/**
* usages (handlebars)
* {{addition 10 to=10}}
* {{addition this to=20}}
* {{addition this to="-5"}}
@thehungrycoder
thehungrycoder / gist:4680521
Created January 31, 2013 05:31
Tastypie collection output
[
{
title: 'Blog Title1',
content: 'Body of Blog Post',
author: 'User 1'
},
{
title: 'Blog Title 2',
content: 'Body of Blog Post 2',
author: 'User 2'
@thehungrycoder
thehungrycoder / gist:4680462
Last active December 11, 2015 23:58
Overriding Resource class to use custom data source in tastypie
from tastypie.resources import Resource
from tastypie import fields
class dict2obj(object):
"""
Convert dictionary to object
@source http://stackoverflow.com/a/1305561/383912
"""
def __init__(self, d):
self.__dict__['d'] = d
myCustomConfirmBox = function(message, callback) {
var options;
options = [
{
'label': 'Yes, Definitely',
'class': 'btn-danger',
'callback': function() {
if (typeof callback === 'function') {
return callback();
@thehungrycoder
thehungrycoder / flatten-spec.rb
Last active August 29, 2015 14:26
Ruby's flatten implementation for Array
require 'rspec' #gem install rspec
require './flatten'
describe 'myflatten' do
it 'makes array single dim regardless of nesting' do
expect([1,2,3].myflatten).to eq([1,2,3])
expect([[1,2,3]].myflatten).to eq([1,2,3])
expect([[[1,2,3]]].myflatten).to eq([1,2,3])
expect([[1,2, [3]], [[[[[[[[[4]]]]]]]]], [[5, [6]], 7]].myflatten).to eq([1,2,3,4,5,6,7])
#!/usr/bin/ruby
require 'prime'
# This is a quick & simple script to find 333rd of all prime numbers begins & ends with 3.
numOfPrime = 0
i = 0
while true do
i += 1
if !Prime.prime?(i)