Skip to content

Instantly share code, notes, and snippets.

View simmo's full-sized avatar

Mike Simmonds simmo

View GitHub Profile
@simmo
simmo / widowKiller.js
Created August 7, 2015 14:24
JavaScript function to remove widows form elements.
// 'elements' should be a string of the CSS selectors you wish to match
function widowKiller(elements) {
var nodes = document.querySelectorAll(elements);
function removeWidow(element) {
var walk = document.createTreeWalker(element, NodeFilter.SHOW_TEXT, null, false);
var node, arr;
while(node = walk.nextNode()) {
arr = node.nodeValue.split(' ');
@simmo
simmo / xhr_upload.js
Created September 8, 2011 14:40
Basic XHR Upload (JQuery-ish)
$('#new_file').submit(function(e) {
e.preventDefault();
var $this = $(this);
var file = this.files[0];
console.log("The file is " + file.fileName + '::' + file.size + '--' + file.type);
function progess_status(event) {
var percent = Math.round(event.loaded / event.total * 100);
console.log(percent);
}
function upload_complete(event){
@simmo
simmo / domain.rb
Created September 13, 2011 13:21
Domain Regex
class Website < ActiveRecord::Base
validates :domain, :format => { :with => /^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix }
end
@simmo
simmo / gist:1213803
Created September 13, 2011 13:29
Google Analytics Regex
validates :ga, :format => { :with => /^UA-[1-9]+-[1-9]+$/x }
@simmo
simmo / gist:1276187
Created October 10, 2011 18:56
Rails 3: Limit attributes return with JSON/XML
class User < ActiveRecord::Base
respond_to :html, :json, :xml
def show
respond_with @user = User.find(params[:id]), :except => [:password, :email]
end
end
@simmo
simmo / gist:2235117
Created March 29, 2012 08:49
SASS: Using @content (3.2) - responding to devices
@mixin respond_to($device) {
@if $device == handheld {
@media only screen and (max-width: 767px) {
@content
}
} @else if $device == handheld-landscape {
@media only screen and (max-width: 767px) and (orientation: landscape) {
@content
}
} @else if $device == handheld-portrait {
@simmo
simmo / gist:2920592
Created June 12, 2012 22:42
Base58 Trait (like Base62) Encode/Decode
//
// Base58 Encode/Decode Trait for PHP
// ==================================
// Base58 is like Base62 except that it removes certain problematic characters such as 0, O, I, and l.
// This is great if you need the encoded output to be easily human-readable. Like short URLs...
//
trait Base58 {
private $alphabet = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
@simmo
simmo / gist:5769912
Created June 12, 2013 23:07
Transliterate and parameterize functions for JavaScript.
CHARACTER_MAP =
'À':'A', 'Á':'A', 'Â':'A', 'Ã':'A', 'Ä':'A', 'Å':'A', 'Æ':'AE', 'Ç':'C',
'È':'E', 'É':'E', 'Ê':'E', 'Ë':'E', 'Ì':'I', 'Í':'I', 'Î':'I', 'Ï':'I',
'Ð':'D', 'Ñ':'N', 'Ò':'O', 'Ó':'O', 'Ô':'O', 'Õ':'O', 'Ö':'O', 'Ő':'O',
'Ø':'O', 'Ù':'U', 'Ú':'U', 'Û':'U', 'Ü':'U', 'Ű':'U', 'Ý':'Y', 'Þ':'TH',
'ß':'ss', 'à':'a', 'á':'a', 'â':'a', 'ã':'a', 'ä':'a', 'å':'a', 'æ':'ae',
'ç':'c', 'è':'e', 'é':'e', 'ê':'e', 'ë':'e', 'ì':'i', 'í':'i', 'î':'i',
'ï':'i', 'ð':'d', 'ñ':'n', 'ò':'o', 'ó':'o', 'ô':'o', 'õ':'o', 'ö':'o',
'ő':'o', 'ø':'o', 'ù':'u', 'ú':'u', 'û':'u', 'ü':'u', 'ű':'u', 'ý':'y',
'þ':'th', 'ÿ':'y', '©':'(c)', 'α':'a', 'β':'b', 'γ':'g', 'δ':'d', 'ε':'e',
@simmo
simmo / helpers.js
Created December 16, 2016 12:26
Redux helpers
export const mapDispatchToProps = actions => dispatch => Object.keys(actions).reduce((obj, key) => {
obj.actions[key] = bindActionCreators(actions[key], dispatch)
return obj
}, { actions: {} })
// connect(null, mapDispatchToProps({ action1, action2 }))(ReactComponent)
// => this.props.actions.action1, this.props.actions.action2
export const mapStateToProps = properties => store => properties.reduce((obj, property) => {
if (store.hasOwnProperty(property)) {
@simmo
simmo / promise.js
Created December 21, 2016 08:35
Redux Middleware: Cacheable Promises
// Utility to check if value is a Promise
function isPromise(value) {
if (value !== null && typeof value === 'object') {
return value && typeof value.then === 'function'
}
return false
}
// Cache