Skip to content

Instantly share code, notes, and snippets.

@twmulloy
twmulloy / gist:eb3ed4629217ebbfedf6
Last active December 28, 2015 06:39
verbose js example
var k = (function(){
"use strict"
var what, is;
what = {
type: {
is: function (obj) {
var type = Object.prototype.toString.call(obj);
@twmulloy
twmulloy / main.go
Created April 7, 2014 19:03
spa catch-all Go app
package main
import (
"html/template"
"net/http"
)
var layout = template.Must(template.ParseGlob("layouts/base.html"))
func init() {
@twmulloy
twmulloy / gist:23804260e0676f1d59b2
Last active August 29, 2015 14:06
group consecutive models
# elements = [{name: "story"}, {name: "social"}, {name: "article"}, {name: "article"}, {name: "story"}, {name: "story"}, {name: "article"}]
# group_consecutive_elements("article", elements)
def group_consecutive_elements(name, elements)
grouped_elements = []
elements.chunk{ |e| e.name == name }.each{ |name, ary| grouped_elements.push((name and ary.length > 1) ? [ary] : ary) }
grouped_elements.flatten!(1)
grouped_elements
end
@twmulloy
twmulloy / gist:e35ebcfbdfd2488f571b
Created April 9, 2015 00:49
JSON flatten to nested form fields
## JSON to nested form data, reset `form` before using
## Formats to `fields` requirement from `ng-file-upload`
## https://github.com/danialfarid/ng-file-upload/wiki/Rails-Example#rails-4-and-angular-file-upload-3-
form = {}
## {"test": { "sup": "bro" }} => { "test[sup]": "bro" }
json2form = (obj, root) ->
angular.forEach obj, (v, k) ->
key = if root then "#{root}[#{k}]" else k
if angular.isObject(v)
[{
"name": "Alabama",
"code": "AL"
}, {
"name": "Alaska",
"code": "AK"
}, {
"name": "American Samoa",
"code": "AS"
}, {
[{
"name": "Afghanistan",
"code": "AF"
}, {
"name": "Åland Islands",
"code": "AX"
}, {
"name": "Albania",
"code": "AL"
}, {
def set_item_options
date = Time.now.utc
date_credential = date.strftime('%Y%m%d')
## S3
@item_options = {
key: "#{Rails.env}/uploads/#{@item.id}",
acl: 'public-read',
algorithm: 'AWS4-HMAC-SHA256',
credential: [
// http://wiki.c2.com/?FizzBuzzTest
// Write a program that prints the numbers from 1 to 100.
// But for multiples of three print “Fizz” instead of the
// number and for the multiples of five print “Buzz”. For
// numbers which are multiples of both three and five
// print “FizzBuzz”.
var fizzBuzz = (function() {
var FIZZ = 'Fizz';
function flatten() {
var result = [];
var currArg;
for(var i = 0; i < arguments.length; i++) {
currArg = arguments[i];
if (currArg.constructor === Array) {
result.push.apply(result, flatten.apply(this, currArg))
} else {
result.push(currArg);
}
// O(n^2)
function unique(arr) {
var result = [];
for(var i = 0; i < arr.length; i++) {
if (result.indexOf(arr[i]) === -1) {
result.push(arr[i]);
}
}
return result;
}