Skip to content

Instantly share code, notes, and snippets.

View aaronranard's full-sized avatar

Aaron Ranard aaronranard

View GitHub Profile
@aaronranard
aaronranard / cookies.js
Last active August 29, 2015 14:00
Javascript: Cookie Management
/**
* determine whether this is the first visit to the animation page
* @param {string} c_name cookie name
* @return {string} cookie value
*/
function getCookie(c_name){
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start === -1){
c_start = c_value.indexOf(c_name + "=");
@aaronranard
aaronranard / input-max-length.js
Last active April 24, 2019 13:03
Angular: directive to limit an input field to a max number of characters
app.directive('inputMaxlength', function() {
return {
require: 'ngModel',
link: function (scope, element, attrs, ngModelCtrl) {
var maxlength = Number(attrs.inputMaxlength);
function fromUser(text) {
if (text.length > maxlength) {
var transformedInput = text.substring(0, maxlength);
ngModelCtrl.$setViewValue(transformedInput);
ngModelCtrl.$render();
@aaronranard
aaronranard / file-upload.js
Last active August 29, 2015 14:12
Angular: File Uploader
$scope.onFileSelect = function($files) {
// https://github.com/danialfarid/angular-file-upload
var file = $files[0];
$scope.upload = $upload.upload({
url: apiEndpoint + '/api/file/upload',
// method: 'POST' or 'PUT',
// headers: {'header-key': 'header-value'},
// withCredentials: true,
//data: {myObj: $scope.myModelObj},
file: file, // or list of files: $files for html5 only
@aaronranard
aaronranard / _icons-material-design.scss
Created June 16, 2015 16:03
SCSS: Materialize icons gulp build fix
$font-mdi : 'Material-Design-Icons';
$mdi-prefix : 'mdi-';
@font-face {
font-family: "#{$font-mdi}";
src:url("#{$icons-font-path}#{$font-mdi}.eot?#iefix") format("embedded-opentype"),
url("#{$icons-font-path}#{$font-mdi}.woff2") format("woff2"),
url("#{$icons-font-path}#{$font-mdi}.woff") format("woff"),
url("#{$icons-font-path}#{$font-mdi}.ttf") format("truetype"),
url("#{$icons-font-path}#{$font-mdi}.svg##{$font-mdi}") format("svg");
@aaronranard
aaronranard / remove-acf-repeater-row.php
Last active October 23, 2015 22:23
acf's delete_sub_field call http://support.advancedcustomfields.com/forums/topic/remove-sub_field/ only makes the value null it doesn't actually delete the row. This does.
<?php
/*
* deleteSubField
*
* This function will delete a value of a sub field entirely and replace the rows correctly.
* ACF's built in delete_sub_field only sets the value to null
*
* @param $field_key (string) the field key of the top level custom field
* @param $repeater_key (string) the field key of the repeater element
* @param $post_id (int) the post_id of which the repeater is stored in
@aaronranard
aaronranard / acf-select.php
Created November 10, 2015 22:00
PHP: WordPress ACF output select field
/*
* Displaying a single value's Label
*/
$field = get_field_object('field_name');
$value = get_field('field_name');
$label = $field['choices'][ $value ];
@aaronranard
aaronranard / get-earliest-relative.php
Last active November 19, 2015 19:24
PHP: WordPress return top most parent ID
<?php
/**
* [Gets the top most parent of a post. If post is top most parent, returns ID]
* @return int [ID of top most parent]
*/
function get_earliest_relative($post){
if ($post->post_parent){
$ancestors=get_post_ancestors($post->ID);
$root=count($ancestors)-1;
$parent = $ancestors[$root];
@aaronranard
aaronranard / phpecc.php
Created January 28, 2016 22:55
PHP: PHPECC
<?php
$loader = new PemLoader();
$math = EccFactory::getAdapter();
$this->messages = new MessageFactory($math);
$privKeySerializer = new PemPrivateKeySerializer(new DerPrivateKeySerializer());
$pubKeySerializer = new PemPublicKeySerializer(new DerPublicKeySerializer());
$PrivateKeyPath = env('KEY_LOCATION').'my.priv';
$PublicKeyPath = storage_path().'/app/'.uniqid().'.pub';
@aaronranard
aaronranard / currency-validator.js
Last active October 27, 2016 14:45 — forked from chrisvfritz/currency-validator.js
Currency Validator to account for commas in input
var currencyValidator = {
format: function (number) {
return (Math.trunc(number * 100) / 100).toFixed(2)
},
parse: function (newString, oldNumber) {
var CleanParse = function (value) {
return { value: value }
}
var StringParse = function (string) {
return parseFloat(string.replace(/,/g, ''))
@aaronranard
aaronranard / ValidateAuthyMiddleware.php
Created March 31, 2017 19:22
Laravel Middleware for Authy OneTouch callback
<?php
namespace App\Http\Middleware;
use Closure;
class ValidateAuthyRequest {
/**
* Handle an incoming request.
*