Skip to content

Instantly share code, notes, and snippets.

@robmint
robmint / template.js
Created February 21, 2016 22:18
Unique keys in data structures with handlebars templating
// typical input data
// Years and semesters need to be unique so they are stored as keys
var data = {
count: 2,
found: 5,
results: {
'2011': {'Semester 1': {url: 'http://site.nz/?pid=401067'}},
'2012': {
'Semester 1': {url: 'http://site.nz/?pid=493997'},
@robmint
robmint / example.xml
Last active February 17, 2016 02:42
Adding custom fields to a Solr schema to enable sorting and grouping
<!-- typical filesystem location ../config/spring/api/discovery.xml -->
<!-- sort filters for the discovery search-->
<property name="searchSortConfiguration">
<bean class="org.dspace.discovery.configuration.DiscoverySortConfiguration">
<property name="defaultSortOrder" value="desc"/>
<property name="sortFields">
<list>
<ref bean="sortTitle" />
<ref bean="sortDateIssued" />
<ref bean="sortSomeField" />
@robmint
robmint / splitter.js
Last active February 11, 2016 21:17
Split an array into roughly even sized columns - index style. You could use flex-box css to do this on the client side.
// format an array into columns as sub arrays
var splitter = function (columns, data) {
try {
var out = [], col = [];
var colSize = Math.ceil(data.length / columns);
var count = 0;
for (key in data) {
if (count > colSize) {
out.push(col);
col = [];
@robmint
robmint / url-test.js
Created February 11, 2016 00:53
Huffman compression of URLs in node.js
// just a test to see if compressing url-encoded json looks/works any better
var strofa = require("strofa");
cl = function(obj) { console.dir(obj, {depth: null, colors: true}); };
var string = '{"subjectonly_keyword":["ACCTG 713"]}';
cl(string);
cl(encodeURIComponent(string));
@robmint
robmint / pagination.js
Last active January 12, 2016 00:49
standard pagination pattern for node
extend = require(extend);
/* Creates an array of items representing the pagination navigation.
You can then pass this to your template engine eg handlebars
Also see the PHP version:
https://gist.github.com/robmint/e4b037aba1687195524c
Based on the work of Jason Coleman
http://www.strangerstudios.com/blog/2006/07/paginate-your-site-like-digg/
*/
@robmint
robmint / list_of_lists.hbs
Created January 6, 2016 01:42
Iterating over a list of lists in a Handlebars template
<!--- json input data:
index: [
[
{divider: true, text: "A", url: ""},
{text: "ACCTG 371", url: "", count:23},
{text: "ACCTG 371", url: "", count:23},
{text: "ACCTG 371", url: "", count:23},
{text: "ACCTG 371", url: "", count:23},
{text: "ACCTG 371", url: "", count:23}
],
@robmint
robmint / popArray.js
Last active December 8, 2015 01:26
Adds lists to an object, pushing to a list if a known key is specified.
// works well with querystring.stringify to produce GET parameters
function objectArrayPush(data, key, value) {
if (typeof data[key] != 'undefined' && data[key] instanceof Array) {
// EXISTS: pushing onto list
data[key].push(value);
} else {
// CREATING: pushing onto list
data[key] = [];
data[key].push(value);
@robmint
robmint / hashList.js
Last active December 7, 2015 00:42
iterating through hash of lists in javascript
data = {
subject: ['Medical Science', 'Philosophy'],
year: ['2015', '2013']
};
for (key in data) {
if (data.hasOwnProperty(key) && data[key] instanceof Array) {
list = data[key];
list.forEach(function (value) {
console.log(key + " : " + value);
@robmint
robmint / gist:3120600
Created July 16, 2012 04:44
add sections to the wordpress customize panel
<?php
require_once( ABSPATH . WPINC . '/class-wp-customize-setting.php' );
require_once( ABSPATH . WPINC . '/class-wp-customize-section.php' );
require_once( ABSPATH . WPINC . '/class-wp-customize-control.php' );
class WP_Customize_Palette_Control extends WP_Customize_Image_Control {
public $type = 'palette';
public $removed = '';
public $context;
@robmint
robmint / Apache wildcard virtual host config
Created February 26, 2012 23:04
Many virtual hosts under one domain
NameVirtualHost *
<VirtualHost *>
ServerName fruit.com
DocumentRoot /www/html/htdocs/fruitsite/docroot
</VirtualHost>
<VirtualHost *>
ServerName www.apple.com
ServerAlias www.banana.com www.cherry.com