Skip to content

Instantly share code, notes, and snippets.

View alexsasharegan's full-sized avatar

Alex Regan alexsasharegan

View GitHub Profile
@alexsasharegan
alexsasharegan / assoc_replace.php
Created August 15, 2016 17:35
Use handlebars-like syntax and do template rendering with associative arrays, where the key is var name, and the value is the rendered content.
<?php
function assoc_replace ($templateStr, $replacementList) {
$searchStrings = [];
$replacements = [];
foreach ($replacementList as $key => $val) {
array_push($searchStrings, '{{'.$key.'}}');
array_push($replacements, $val);
}
return str_replace($searchStrings, $replacements, $templateStr);
}
@alexsasharegan
alexsasharegan / db_connect.php
Created August 15, 2016 17:43
An easy to include function for connecting to an SQL database. Return the database handle. Fill in your default connection, while allowing for case-by-case connection overrides.
<?php
function db_connect ($options = []) {
if (!empty($options)) {
$hostName = !empty($options['hostName']) ? $options['hostName'] : 'someHostName';
$databaseName = !empty($options['databaseName']) ? $options['databaseName'] : 'someDatabaseName';
$dbUserName = !empty($options['dbUserName']) ? $options['dbUserName'] : 'someUsername';
$dbPassword = !empty($options['dbPassword']) ? $options['dbPassword'] : 'somepassword';
} else {
$hostName = 'someHostName';
@alexsasharegan
alexsasharegan / mapObjStyleAttrs.js
Created August 17, 2016 03:54
Get an inline style string by passing in a hash of camelCased style attributes.
//es5
function mapObjStyleAttrs(obj) {
return Object.keys(obj)
.map(function (key) {
return [key.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(), obj[key]]
.join(':');
})
.join(';');
}
@alexsasharegan
alexsasharegan / encodeQueryData.js
Last active August 17, 2016 12:31
Pass an object of key: value pairs to be uri encoded (does prepend the '?')
// es5
function encodeQueryData(data) {
return '?' + Object.keys(data).map(function(key) {
return [key, data[key]].map(encodeURIComponent).join("=");
}).join("&");
}
// es6
const encodeQueryData = data => '?' + Object.keys(data).map(key => [key, data[key]].map(encodeURIComponent).join("=")).join('&');
@alexsasharegan
alexsasharegan / pagination.php
Last active August 18, 2016 19:30
Calculates pages using mySQL offset/limit and renders html for a predefined amount of pages
<?php
$limit = 25;
$pageDisplayLimit = 10 - 1;
$currentPage = $offset / $limit + 1;
$totalPages = ceil($count / $limit);
$pagesRemaining = $totalPages - $currentPage;
$lastPageToDisplay = $currentPage + $pageDisplayLimit <= $totalPages
? $currentPage + $pageDisplayLimit
: $totalPages;
@alexsasharegan
alexsasharegan / isAssoc.php
Created August 22, 2016 20:43
Test an array to see if an associative array (returns true), or a numerically indexed array (returns false)
<?php
# From Stack Overflow
# http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
function isAssoc($array) {
return array_keys($array) !== range(0, count($array) - 1);
}
let emailRegEx = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
if (emailRegEx.test(email)) {
$email.removeClass('invalid').addClass('valid');
} else {
$email.removeClass('valid').addClass('invalid');
}
@alexsasharegan
alexsasharegan / states-select.html
Created December 23, 2016 00:37
Form select with all 50 states.
<select name="state">
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="DC">District Of Columbia</option>
@alexsasharegan
alexsasharegan / gulpfile.js
Created January 7, 2017 20:27
Browserify js bundling and sass compilation
// gulpfile.js
// Heavily inspired by Mike Valstar's solution:
// http://mikevalstar.com/post/fast-gulp-browserify-babelify-watchify-react-build/
"use strict";
const gulp = require( 'gulp' );
const browserify = require( 'browserify' );
const babelify = require( 'babelify' );
const uglifyify = require( 'uglifyify' );
const watchify = require( 'watchify' );
@alexsasharegan
alexsasharegan / regex.js
Created March 6, 2017 21:35
Common RegExes for me
class MyRegEx {
static email(){
return /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
}
static ssn(){
return /^(?!000|666|900|999)(\d{3})-?(?!00)(\d{2})-?(?!0000)(\d{4})$/;
}
}