Skip to content

Instantly share code, notes, and snippets.

View LB-Digital's full-sized avatar

Lucas Bowers LB-Digital

View GitHub Profile
@LB-Digital
LB-Digital / subdomain-basic-auth.js
Created December 12, 2018 21:07
Basic auth header of a subdomain on NodeJS with Express
/** Password protect a subdomain on ExpressJS
* Uses HTTP Basic authorization... https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization
* with the WWW-Authenticate header... https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/WWW-Authenticate
*
* By then checking if the user is visting from the given subdomain, you know they are authorized.
* I use this concept to password protect beta site features that aren't yet released publicly, on which
* certain pages are only accessible via the password protected 'beta.domain.com' url
*/
const config = {
@LB-Digital
LB-Digital / FizzBuzz.js
Last active December 12, 2018 22:31
My attempt at the classic FizzBuzz challenge, with an aim of being as concise as possible
/** FizzBuzz challenge in 3 lines
* "Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”."
*/
for (let i=1; i<=100; i++){
console.log((((i%3==0)?'Fizz':'')+((i%5==0)?'Buzz':''))||i);
}
@LB-Digital
LB-Digital / intersection.js
Last active December 12, 2018 22:31
Find the intersection between two JS arrays of objects, based on an object property (using 'filter' and 'map').
/** Find the intersection between two JS arrays of objects
* finds based on an object property (using 'filter' and 'map')
*/
products = [ // an array of products
{reference:'LOWP8F7H', name:'Nike Blazer', price:120, category:1},
{reference:'3PFH3LDP', name:'Nike Kyrie', price:340, category:1},
{reference:'FGH4P40K', NAME:'Nike Bomber', price:70, category:0}
];
@LB-Digital
LB-Digital / random-int.js
Created December 21, 2018 13:14
Random integer between min, max values
/**
* Using Math.round() results in an uneven distribution, as mentioned here...
* https://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range
*/
function randomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
@LB-Digital
LB-Digital / send-pushover-notif.js
Created February 4, 2019 16:13
A function for sending Pushover Notifications ( https://pushover.net/ )
function sendPushoverNotif(token, target, message, title){
message = encodeURIComponent(message); // make url friendly
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://api.pushover.net/1/messages.json", true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(`token=${token}&user=${target}&message=${message}&title=${title}`);
}
@LB-Digital
LB-Digital / php-url-request.php
Last active February 4, 2019 16:29
Sending URL requests (POST/GET) in PHP using curl
$url = 'https://example.com/api.json';
$fields = array(
'key1'=>urlencode('value 1'),
'key2'=>urlencode('value 2'),
'key3'=>urlencode('value 3')
);
//url-ify the data for the POST
$fields_string = "";
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
@LB-Digital
LB-Digital / get-jwt-payload.js
Created February 12, 2019 18:24
Retrieve the Payload data section of a JSON Web Token.
function getJwtPayload(){
//retrieve JsonWebToken from cookies
var cookies = document.cookie.split(";"),
jwt;
for (cookie of cookies){
var keyValue = cookie.trim().split('=');
if (keyValue[0] === "jwt"){
jwt = keyValue[1];
}
}
@LB-Digital
LB-Digital / handler.js
Created June 29, 2019 17:40
AWS DynamoDB simple scan/put operations
// leads/read.read
'use_strict';
const uuid = require('uuid');
const AWS = require('aws-sdk');
const IsEmail = require('isemail');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
module.exports.create = function(event, context, callback) {
@LB-Digital
LB-Digital / email_regex.txt
Created July 10, 2019 23:58
Email regex for JavaScript, based on https://emailregex.com/
^(?:[a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\\.[a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-zA-Z0-9-]*[a-zA-Z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])$
/*------EXTRACT FORM INPUTS------
* Returns Object of name:value pairs in .data
* .error will contain invalid input name on Error (e.g: empty required input)
*/
function extractFormInputs( formEl ){
// get all inputs from form
var inputs = formEl.querySelectorAll('input,textarea');
// convert inputs to array
var inputsArr = Array.from(inputs);