Skip to content

Instantly share code, notes, and snippets.

View danrichards's full-sized avatar
🎯
Focusing

Dan Richards danrichards

🎯
Focusing
View GitHub Profile
@danrichards
danrichards / Model.php
Last active June 5, 2020 02:42
Laravel Base Model with some simple caching mechanisms using attribute mutators.
<?php
namespace App;
use App\Utils\Data;
use App\Utils\Str;
use Cache;
use Closure;
use DateTime;
use DB;
@danrichards
danrichards / Model.php
Last active August 27, 2016 23:35
$queryProps property for Eloquent Model with example.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model as BaseModel;
/**
* Class Model
*/
abstract class Model extends BaseModel
@danrichards
danrichards / handlebars.ifCond.js
Created July 27, 2016 13:57
Comparison Helper for Handlebars JS.
// @see http://bdadam.com/blog/comparison-helper-for-handlebars.html
(function() {
function checkCondition(v1, operator, v2) {
switch(operator) {
case '==':
return (v1 == v2);
case '===':
return (v1 === v2);
case '!==':
{{-- This area is for mounting global JS objects. --}}
<script>
"use strict";
/**
* Please keep App and its dependencies lean. It's loaded everywhere.
*
* @constructor
@danrichards
danrichards / $.addPasswordStrengthMeter.js
Last active July 19, 2016 19:43
Add a password strength meter to your sign up forms.
(function ( $ ) {
$.fn.addPasswordStrengthMeter = function(meter_el) {
var that = this;
if (typeof meter_el == 'undefined') {
meter_el = $('.password-strength-meter');
}
var bars = meter_el.children();
@danrichards
danrichards / isIE.mixin.js
Created May 23, 2016 19:14
Underscore Mixin for IE (Internet Explorer) browser detection.
// https://jsfiddle.net/danrichards/16mxh9th/
_.mixin({
isIE: function(mixed) {
if (_.isUndefined(mixed)) {
mixed = [/MSIE\s7\./, /MSIE\s8\./, /MSIE\s9\./, /MSIE\s10\./, /Trident.*rv\:11\./];
} else if (_.isArray(mixed)) {
mixed = mixed.map(function(browser) {
return new RegExp('MSIE\s'+browser.toString()+'\.', 'g');
});
@danrichards
danrichards / modulo-splice.php
Last active April 7, 2016 17:35
Use dynamic modulo to incrementally splice an array.
<?php
/**
* Provided a threshold, determine modulo to keep every m-th item in the
* array where m = ceil(count($array) / $threshold)
*
* @param $arr
* @param $threshold
* @param string $round
@danrichards
danrichards / Wizard.js
Created March 28, 2016 02:36
Javascript wizard class, uses Jquery(for AJAX), and Underscore.
/**
* SubmitWizard
*
* @returns {SubmitWizard}
* @constructor
*/
function SubmitWizard(options) {
var that = this;
/**
@danrichards
danrichards / ternaries.md
Last active June 5, 2020 02:53
PHP for those who love ternaries

PHP Ternary cheat sheet

Be careful with your ternaries, folks.

var_dump('' ?: 'empty stringy');                 // empty string
var_dump(null ?: 'nully');                       // nully
var_dump(0 ?: 'zeroy');                          // zeroy
var_dump(false ?: 'falsey'); // falsey
@danrichards
danrichards / $.populate.js
Last active February 21, 2016 07:25
$.populate(form, obj) Provided a selector and object, set the corresponding form fields.
/**
* Provided a data Object, set the corresponding form fields.
*
* @param obj Object Your data.
* @return Array Fields updated.
*/
$.fn.populate = function(obj) {
var updated = [];
for (var field in obj) {