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 / 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 / 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 / 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 / .htaccess
Created September 7, 2016 00:36
Apache Config for React Router
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
@alexsasharegan
alexsasharegan / flexbox.css
Last active December 15, 2023 16:32
Some helpful classes to start using flexbox in your styles. Classes are vendor prefixed, and set up to add one property only—this enables building up styles with class composition. The flexbox class names help convey how the css property will affect your html.
.flex {
display : -webkit-box;
display : -ms-flexbox;
display : flex;
}
.flex-row {
display : -webkit-box;
display : -ms-flexbox;
@alexsasharegan
alexsasharegan / promisify.js
Last active August 21, 2017 16:16
Promisify Node async functions dynamically. (curries when provided no additional arguments)
function promisify(func, ...args) {
if (arguments.length === 1) {
// Curry with the decremented arity
return promisify.bind(null, func)
}
return new Promise((resolve, reject) => {
func(...args, (error, ...cbArgs) => {
if (error) {
return reject(error)