Skip to content

Instantly share code, notes, and snippets.

View jorgepinon's full-sized avatar

Jorge Luis Piñon jorgepinon

View GitHub Profile
@jorgepinon
jorgepinon / Dropdown.js
Last active July 23, 2022 22:33
Dropdown class
function Dropdown(toggle, menu) {
this.toggle = toggle;
this.menu = menu;
this.menuPlacement = {
top: menu.classList.contains('dropdown-menu-top'),
end: menu.classList.contains('dropdown-menu-end')
};
this.toggle.addEventListener('click', this.clickHandler.bind(this));
@jorgepinon
jorgepinon / validateAjaxMailchimpForm.js
Last active December 8, 2020 09:11
Mailchimp Validation Script
/**
* A validation script for use with a "naked" Mailchimp embedded form.
*
* credit to this post on CSS-Tricks:
* https://css-tricks.com/form-validation-part-4-validating-mailchimp-subscribe-form/
*
* I added a couple of minor Bootstrap 4 classes
**/
@jorgepinon
jorgepinon / php_function_arguement_defaults.php
Last active January 5, 2018 18:57
php pattern : test if properties exists and set defaults
/**
* Function does something...
* @param obj $obj object with required and optional properties.
*/
function something($obj) {
// bail if required properties aren't set
if( !isset($obj->req_prop_1) || !isset($obj->req_prop_1) ) { return ''; }
$opt_prop_1 = $obj->opt_prop_1;
$opt_prop_2 = $obj->opt_prop_2;
@jorgepinon
jorgepinon / formIsDirty()
Created November 7, 2017 03:35
javascript function to check if a form has inputs that have been changed
/**
* Determines if a form is dirty by comparing the current value of each element
* with its default value. Nicely done on StackOverflow: https://stackoverflow.com/a/155812/1558564
*
* @param {Form} form the form to be checked.
* @return {Boolean} true if the form is dirty, false if it's not.
*/
function formIsDirty(form) {
for (var i = 0; i < form.elements.length; i++) {
var element = form.elements[i];
@jorgepinon
jorgepinon / gist:a2ffcd9a7c33083dd433f57f3c86c7fc
Created October 23, 2017 14:03
Promise with async/await structure
/* thanks to @mathias for this. https://twitter.com/mathias/status/922460187671216129 */
const fetchAndDisplay = async function(url, element) {
showLoadingSpinner();
try {
const response = await fetch(url);
const text = await response.text();
element.textContent = text;
@jorgepinon
jorgepinon / function largestLessThanMaxLimit(limit = 10)
Created April 2, 2017 17:53
largestLessThanMaxLimit: higher-order function for reducing arrays to largest value while not going over the max defined
function largestLessThanMaxLimit(limit = 10) {
var maxLimit = limit;
return function largestLessThanMaxLimit(...args) {
return args.reduce( function(acc, val) {
if( val > acc && val < maxLimit ) {
return val;
}
else {
return acc;
}
var myModule = (function () {
var privateMethod = function () {
console.log('A private method');
},
someMethod = function () {
console.log('A public method');
},
anotherMethod = function () {
console.log('Another public method');
};
validate domain (only letter, numbers, and hypens... and must start with a letter)
^[a-zA-Z]+[a-zA-Z0-9\-]*$
@jorgepinon
jorgepinon / formInputsToJson
Created August 17, 2015 16:51
convert form data to json obj (for ajax post)
function formInputsToJson(form) {
var $form = $(form);
var fArr = $form.serializeArray()
var fObj = {};
for (var i in fArr) {
fObj[fArr[i].name] = fArr[i].value;
}
return fObj;
@jorgepinon
jorgepinon / showRandomItem
Last active August 29, 2015 14:17
displays a random item from an unordered list (like for random promo images)
//todo: de-jquery this
function showRandomItem(list) {
// random ad on refresh. Each a element should have style="display:none"
var items = $(list).children('li'),
tot = items.length;
if (tot > 1) {
var randNum = Math.round(Math.random()*(tot-1));
items.eq(randNum).show();
} else {
$(items).show();