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 / parseMcTxt.php
Created August 23, 2017 16:38
PHP Function for converting MC colour codes to HTML
function parseMcTxt($inputTxt){
$inputTxt = str_replace('||', '<br>', $inputTxt);
$split = explode("&", $inputTxt);
$output = "";
foreach ($split as $key => $value) {
$color = substr($value, 0, 1);
$str = substr($value, 1);
$span = "<span style='color: ";
@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)
@LB-Digital
LB-Digital / RandomVector.js
Created July 4, 2018 20:59
Generate a random X,Y component vector with a magnitude in a given range
// Minimum & Maximum values for the speed of the vector
var minSpeed = 5;
var maxSpeed = 20;
// Random vector components in range -1,1
var randX = (Math.random()*2)-1;
var randY = (Math.random()*2)-1;
// Get magnitude of the random vector
var mag = Math.sqrt(Math.pow(randX,2) + Math.pow(randY,2));
// Use magnitude to normalise the vector components
@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 / chainFunctions.js
Created July 17, 2018 12:21
Method for chaining functions in JS, avoiding an ungodly callback pyramid
var chain = [
()=>{
console.log('chain1111');
return chain.shift()();
},
()=>{
console.log('chain22222');
return chain.shift()();
},
@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 / MojangAPI.js
Last active July 17, 2018 14:37
Basic usage of the Mojang API
// INFO: This script currently relies on the 'request' npm module,
// however this will change in the near future to become independant.
// http://wiki.vg/Mojang_API
const MojangAPI = {
base: "https://api.mojang.com",
getUUID: (username, done)=>{
request(MojangAPI.base + '/users/profiles/minecraft/' + username, (err, response, body)=>{
@LB-Digital
LB-Digital / promise-chain.js
Last active September 10, 2018 23:22
JS Chaining Promises
function myFunc(things, stuff){
return new Promise( (resolve,reject) =>{ // setup
var result = [];
resolve(result);
}).then( (result) =>{ // Action 1
return new Promise( (resolve,reject) =>{
myFirstFunction(things,stuff)
@LB-Digital
LB-Digital / promise-wait-in-for-loop.js
Created September 11, 2018 19:46
Loop through items, awaiting a promise return for each item.
function myFunc(items){
var fn = function addSomethingToItem(item){
return new Promise(resolve => {
myFirstActionFunc()
.then( response =>{
item.value = response;
resolve(item);
@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
});