Skip to content

Instantly share code, notes, and snippets.

@twmulloy
twmulloy / example.js
Last active September 26, 2017 23:09
var numbers = [1, 5, 17, 4, 9, 3, 1, 17, 32, 5, 3, 27, 9, 18, 3, 12, 67, 18, 32, 1, 19, 21, 1, 17];
var hashMap = {};
var groupArr = [];
var currentNum;
for (var i = 0; i < numbers.length; i++) {
currentNum = numbers[i];
if (typeof hashMap[currentNum] === 'undefined') {
hashMap[currentNum] = 0;
}
@twmulloy
twmulloy / page.component.ts
Last active June 23, 2017 04:36
Angular 4 + Alchemy CMS recursive <nav /> from `/api/pages/nested` response
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core'
import { ActivatedRoute } from '@angular/router'
import { Site } from './site'
import { PageService } from './page.service'
import { Page } from './page'
@Component({
selector: 'page',
// 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;
}
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);
}
// 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';
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: [
[{
"name": "Afghanistan",
"code": "AF"
}, {
"name": "Åland Islands",
"code": "AX"
}, {
"name": "Albania",
"code": "AL"
}, {
[{
"name": "Alabama",
"code": "AL"
}, {
"name": "Alaska",
"code": "AK"
}, {
"name": "American Samoa",
"code": "AS"
}, {
@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)
@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