Skip to content

Instantly share code, notes, and snippets.

Avatar
💪
Crushing it

Adam Crampton adamcrampton

💪
Crushing it
View GitHub Profile
@adamcrampton
adamcrampton / youTubeDurationParser.php
Created Jun 22, 2020
Parse YouTube duration string to seconds integer
View youTubeDurationParser.php
<?php
/**
* Match pattern PT#H#M#S (leading PT, hours, minutes, seconds) and return seconds value.
*
* @param string $string
* @return int
*/
private static function convertDuration($string)
{
// Comprehensive regex that should match any value.
@adamcrampton
adamcrampton / JsonCheck.php
Created May 26, 2020
Determine if PHP string is valid JSON
View JsonCheck.php
<?php
/**
* Checks if string is valid JSON.
*
* @param string $string
* @return bool
*/
public function jsonCheck($string)
{
@adamcrampton
adamcrampton / ajaxFullPageSpinner.html
Created May 7, 2020
Full page CSS spinner useful for pages with AJAX
View ajaxFullPageSpinner.html
<div class="loading hidden"><i class="fa fa-spin fa-circle-o-notch" style="font-size: 48px;"></i></div>
<script type="text/javascript">
const formData = new FormData();
formData.append('key', 'value');
$.ajax({
async: true,
contentType: false,
processData: false,
@adamcrampton
adamcrampton / jqueryAjaxPostBoilerplate.js
Last active Mar 13, 2020
Boilerplate for jQuery AJAX POST request
View jqueryAjaxPostBoilerplate.js
const endpoint = 'https://api.some.endpoint.test';
const formId = 'this-form-id-value';
const data = new FormData(document.getElementById('formId'));
$.ajax({
async: true,
contentType: false,
processData: false,
url: endpoint,
method: 'POST',
@adamcrampton
adamcrampton / ttlArray.php
Created Sep 30, 2019
Handy array useful for configuring cache TTLs etc
View ttlArray.php
<?php
/*
|--------------------------------------------------------------------------
| Cache TTL
|--------------------------------------------------------------------------
|
| Set commonly used TTL values.
|
*/
return [
@adamcrampton
adamcrampton / mysql_import.sh
Created Sep 23, 2019
MySQL command line import
View mysql_import.sh
#!/bin/bash
# Argument is path + filename of .sql file dump.
mysql database_name -u user_name -p --host=127.0.0.1 --port=33060 < $1
@adamcrampton
adamcrampton / fake_progress_bar.html
Created Sep 18, 2019
Progress bar that increments itself via a loop
View fake_progress_bar.html
<div id="progress-bar"><div style="width:0;"></div>
<script>
let progressBar = document.querySelector("#progress-bar div")
let items = [];
for (let index = 0; index <= 100; index++) {
items.push(index);
}
@adamcrampton
adamcrampton / sidebarToggleActions.js
Created Sep 12, 2019
jQuery - Toggle locking the page position and grey out background when sidebar drawer opens
View sidebarToggleActions.js
// This is for use with AdminLTE, but the same stuff can be applied to other drawer plugins.
$(document).ready(function() {
// Lock the page when drawer is open.
$('[data-toggle="control-sidebar"]').on('expanded.controlsidebar', sidebarOpenActions);
// Unlock.
$('[data-toggle="control-sidebar"]').on('collapsed.controlsidebar', sidebarCloseActions);
// Allow Esc to close the drawer.
$(document).keyup(function(e) {
@adamcrampton
adamcrampton / validationErrorMacro.php
Last active Aug 29, 2019
Laravel macro for returning validation errors to front end using Form class
View validationErrorMacro.php
<?php
/**
* Bootstrap macros in Service Provider.
*
* @return void
*/
public function boot()
{
// Build form error macro to easily set inline error labels.
Form::macro('errorMessage', function($field) {
@adamcrampton
adamcrampton / setElementValueFromLocalStorage.js
Created Aug 29, 2019
Set a jQuery element's value using a specified key in local storage
View setElementValueFromLocalStorage.js
// Fetch storage item if it exists and set element value
function updateFieldValueFromStorage(elementId, storageKey) {
let storage = window.localStorage;
if (storage.getItem(storageKey)) {
($('#' + elementId).val(storage.getItem(storageKey)));
}
}
You can’t perform that action at this time.