Skip to content

Instantly share code, notes, and snippets.

View codeguy's full-sized avatar

Josh Lockhart codeguy

View GitHub Profile
@codeguy
codeguy / slugify.js
Created September 24, 2013 13:19
Create slug from string in Javascript
function string_to_slug (str) {
str = str.replace(/^\s+|\s+$/g, ''); // trim
str = str.toLowerCase();
// remove accents, swap ñ for n, etc
var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
var to = "aaaaeeeeiiiioooouuuunc------";
for (var i=0, l=from.length ; i<l ; i++) {
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
}
@codeguy
codeguy / round-date.js
Created July 12, 2013 03:57
Round a Date to nearest Nth minute
var roundToNearestMinute = function (date) {
var coeff = 1000 * 60 * 5; // <-- Replace {5} with interval
return new Date(Math.round(date.getTime() / coeff) * coeff);
};
@codeguy
codeguy / select-state.html
Created November 25, 2013 18:47
US States select box
<select name="state">
<option value="" selected="selected">Select a State</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
@codeguy
codeguy / csv-iterator.php
Created September 24, 2013 01:31
PHP CSV iterator
<?php
class CsvIterator implements \Iterator
{
const ROW_SIZE = 4096;
/**
* The pointer to the cvs file.
* @var resource
* @access protected
*/
@codeguy
codeguy / get-current-location.js
Created September 24, 2013 18:33
Get user's current gelocation with HTML Geolocation API
if ('geolocation' in navigator) {
console.log('Supports HTML geolocation API');
(function () {
var onSuccess = function (location) {
console.log('User location', location);
var userLat = location.coords.latitude,
userLon = location .coords.longitude;
},
# Views and Blocks
* Cache views
* Export views to code
* Revert views to code
# Performance
* Enable page cache
* Enable block cache
@codeguy
codeguy / geocode-address.js
Created September 24, 2013 18:37
Geocode address with Google Maps API
/**
* Geocode address
*
* REQUIREMENTS
* - Google Maps API
* - HTML5 Geolocation API
*/
var geocode = function (address, onSuccess, onError) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ address: address }, function (results, status) {
@codeguy
codeguy / html5-csv-parser.js
Created September 24, 2013 01:44
Upload, parse, and iterate CSV file with HTML5 File APIs
/**
* CSV upload and parser
*
* This code will use the HTML5 File and FileReader APIs
* to upload and parse a CSV file.
*
* REQUIREMENTS:
* - jQuery
* - D3.js
* - HTML5 File APIs
Verifying that +codeguy is my blockchain ID. https://onename.com/codeguy
@codeguy
codeguy / placeholders.js
Created December 9, 2013 15:50
Add placeholder attribute fallback for older browsers
placeholderSupport = ("placeholder" in document.createElement("input"));
if (placeholderSupport === false) {
$('[placeholder]').each(function () {
var $input = $(this),
placeholder = $input.attr('placeholder');
$input
.val(placeholder)
.focus(function () {