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 / 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 / Array.prototype.isEqualTo.js
Created October 30, 2018 03:30
An array method to check if one array is equal to another. NOTE: this is only built for simple arrays with singular values, e.g: ['a','b','c'], not complex arrays, e.g: ['a', {this:'that',up:'down'}]
Array.prototype.isEqualTo = function(arr2){ // defining the Array method
var arr1 = this; // accesing the first array
if (arr1.length==arr2.length){ // if they have the same length
arr1 = arr1.filter(item=>{ // filter through arr1
if (arr2.indexOf(item) > -1){ // if item from arr1 is in arr2
return false; // remove from arr1 so we know its been accounted for
}
return true; // keep in arr1 as not in arr2
});
@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 / validEmail.js
Created July 17, 2018 12:24
Simple REGEX based JavaScript function to validate an email address
function validEmail(address) {
// Regular Express from http://emailregex.com
const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (emailRegex.test(address)) return true;
return false;
}
@LB-Digital
LB-Digital / JS_Read-JSON-File.js
Created July 7, 2018 00:06
Reading JSON files in Javascript
var xmlhttp = new XMLHttpRequest();
var url = "myFile.json";
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var myArr = JSON.parse(this.responseText);
myFunction(myArr);
}
};
xmlhttp.open("GET", url, true);
@LB-Digital
LB-Digital / hexToRGBA.js
Created July 4, 2018 16:28
JS function for converting a `HEX` colour code string with an alpha component, to `RGBA`.
function hexToRGBA(hex, opacity){
var rgba = "rgba(";
var inc = Math.abs(hex.length - 5);
for (var i = 1; i < hex.length; i+=inc){
var component = hex[i] + hex[i + (inc-1)];
rgba += parseInt(component, 16) + ",";
}
return rgba + opacity + ")";
}
// abs() gives absolute val (modulus); -5 just produces correct increment (1 or 2, based on length of 4 or 7... #f0f vs #ff00ff)
@sevastos
sevastos / aws-multipartUpload.js
Last active May 28, 2024 15:02
Example AWS S3 Multipart Upload with aws-sdk for Node.js - Retries to upload failing parts
// Based on Glacier's example: http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/examples.html#Amazon_Glacier__Multi-part_Upload
var fs = require('fs');
var AWS = require('aws-sdk');
AWS.config.loadFromPath('./aws-config.json');
var s3 = new AWS.S3();
// File
var fileName = '5.pdf';
var filePath = './' + fileName;
var fileKey = fileName;