Skip to content

Instantly share code, notes, and snippets.

View LiamChapman's full-sized avatar

Liam Chapman LiamChapman

View GitHub Profile
@LiamChapman
LiamChapman / element-select-find.js
Last active January 1, 2016 23:59
A quick shorthand for selecting elements using the native css selector in javascript e.g. document.find('#foo'); Need to update it to do some chaining
Document.prototype.find = function (query) {
if (document.querySelectorAll) {
var query = this.querySelectorAll(query);
if (query.length == 1)
return query[0];
else
return query;
}
}
@LiamChapman
LiamChapman / data-attr.js
Last active January 1, 2016 23:59
Shorthand for natively getting and setting data attributes
Element.prototype.data = function(attr, value) {
var attr = 'data-'+attr;
if (value) {
return this.setAttribute(attr, value);
} else {
return this.getAttribute(attr)
}
}
@LiamChapman
LiamChapman / object-merge.js
Last active January 1, 2016 23:59
Basic object merge with javascript e.g. var myObject1 = { 'test1' }; var myObject2 = { 'test2' }; myObject = myObject1.merge(myObject2);
Object.prototype.merge = function (toMerge) {
var obj = {};
// first object
for (var i in this) {
obj[i] = this[i];
}
// second object
for (var i in toMerge) {
obj[i] = toMerge[i];
}
@LiamChapman
LiamChapman / shorthand-event-bind.js
Last active January 1, 2016 23:59
Shorthand cross-browser code for binding events to elements e.g. document.getElementById('#foo').on('click', function () { alert('hi!'); });
Element.prototype.on = function (evnt, func) {
if (this.addEventListener) // W3C DOM
this.addEventListener(evnt, func, false);
else if (this.attachEvent) { // IE DOM
var r = this.attachEvent("on"+evnt, func);
return r;
}
}
@LiamChapman
LiamChapman / facebook_request.php
Created February 21, 2014 09:50
For reading app_data in the URL and other other things on Facebook
<?php
if (!function_exists('facebook_request')) {
function facebook_request () {
if (isset($_REQUEST['signed_request'])) {
if ($request = $_REQUEST['signed_request']) {
list($encoded_sig, $payload) = explode('.', $request, 2);
$decoded = base64_decode(strtr($encoded_sig, '-_', '+/'));
$data = base64_decode(strtr($payload, '-_', '+/'));
return json_decode($data, true);
@LiamChapman
LiamChapman / p.php
Created February 21, 2014 09:52
Shorthand for reading an array or object in a legible way.
<?php
if (!function_exists('p')) {
function p($array) {
echo '<pre>';
print_r($array);
echo '</pre>';
}
}
@LiamChapman
LiamChapman / get_called_class.php
Created February 21, 2014 09:56
Add get_called_class support for php < 5.3. It will be slower though as it uses debug_backtrace.
<?php
if (!function_exists('get_called_class') && version_compare(PHP_VERSION, '5.3', '<')) {
function get_called_class() {
$bt = debug_backtrace();
$l = 0;
do {
$l++;
$lines = file($bt[$l]['file']);
$callerLine = $lines[$bt[$l]['line']-1];
@LiamChapman
LiamChapman / json.php
Created February 21, 2014 09:57
Small shortand function for reading and encoding JSON in php
<?php
if (!function_exists('json')) {
function json ($data) {
// if string we return as array
if (is_string($data)) {
return json_decode($data, true);
// if array or object we encode it
} else if (is_array($data) || is_object($data)) {
return json_encode($data);
}
@LiamChapman
LiamChapman / slugify.php
Created February 21, 2014 09:59
Small function for creating "slugs" or URI in PHP
<?php
if (!function_exists('slugify')) {
function slugify ($str, $replace="-") {
// replace all non letters or digits by $replace
$str = preg_replace('/\W+/', $replace, $str);
// trim and lowercase
$str = strtolower(trim($str, $replace));
return $str;
}
@LiamChapman
LiamChapman / Singleton.php
Created February 21, 2014 10:14
Common Singleton pattern in php
<?php
abstract class Singleton {
protected function __construct() {}
final public static function instance () {
static $instances = array();
$calledClass = get_called_class();
if (!isset($instances[$calledClass])) {