Skip to content

Instantly share code, notes, and snippets.

View moriarty99779's full-sized avatar

Brian Wasner moriarty99779

  • 09:28 (UTC -05:00)
View GitHub Profile
@moriarty99779
moriarty99779 / gist:d140399cd260a386dc906b7290d2ceea
Created May 29, 2024 22:06
Javascript - Check if a MAC address is valid
const isMACAddressValid = (mac) => /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/.test(mac);
document.write(isValidMACAddress("AA:22:FF:04:2D"));
@moriarty99779
moriarty99779 / gist:7d4250fb0c1027759155a7f826f9a137
Last active May 29, 2024 22:09
Javascript - Convert RGB to HSL
const convertRGBToHSL = (r, g, b) => {
  [r, g, b] = [r, g, b].map(val => val / 255);
  const [max, min] = [Math.max(r, g, b), Math.min(r, g, b)];
  let h = (max !== min) ? ((max === r ? g - b : (max === g ? b - r : r - g)) / (max - min) + (max === g ? 2 : (max === b ? 4 : 0))) / 6 : 0;
  let s = (max !== min) ? (l => l > 0.5 ? (max - min) / (2 - max - min) : (max - min) / (max + min))(l) : 0;
  let l = (max + min) / 2;
  return { h: Math.round(h * 360), s: Math.round(s * 100), l: Math.round(l * 100) };
};
document.write(convertRGBToHSL(77, 123, 44));
@moriarty99779
moriarty99779 / gist:9bbf369aa6bb03434399fcb8ddd33a28
Created May 6, 2024 15:00
Powershell Get hash of the directory names and attributes
$pwd = "C:\Users\someusername"
$dirinfo = Get-ChildItem($pwd) -recurse
$d = $dirinfo.ToString()
$stringAsStream = [System.IO.MemoryStream]::new()
$writer = [System.IO.StreamWriter]::new($stringAsStream)
$writer.write($d)
$writer.Flush()
$stringAsStream.Position = 0
$output = Get-FileHash -InputStream $stringAsStream | Select-Object Hash
$output.hash.ToString()
@moriarty99779
moriarty99779 / gist:83880be8d00f6904318cf47deb02cab5
Created May 6, 2024 14:48
PHP Find sum of all numbers from 1..N
function get_sum(int $n = 100) {
$sum = $n * ($n + 1) / 2;
return $sum;
}
@moriarty99779
moriarty99779 / gist:1261b09635dda62afaa31f40b1e6d8e9
Created May 6, 2024 14:34
PHP Improved database connection object as a singleton - PDO <-> MySQL
<?php
// Program: DatabaseConnection.php
// Programmer: Brian Wasner <[email protected]>
// Created: 27 March 2023 0847
// Purpose: Provides a reusable singleton connection to the database
class DatabaseConnection {
protected static $db;
@moriarty99779
moriarty99779 / gist:34d1f6e447a8e2029c96d5786c41c6d6
Created May 6, 2024 14:31
Rust Simple Echo Server on TCP Port 49152
use std::thread;
use std::net::{TcpListener, TcpStream, Shutdown};
use std::io::{Read, Write};
use std::process::Command;
fn handle_client(mut stream: TcpStream) {
let mut data = [0 as u8; 255];
while match stream.read(&mut data) {
Ok(size) => {
stream.write(&data[0..size]).unwrap();
@moriarty99779
moriarty99779 / gist:e5c9572815d6a66eaf8376a3b5148d73
Created May 6, 2024 14:13
Javascript Test if string is a valid email address
var email = '[email protected]';
var re = /^(([^<>()[\]\\.,;:\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 (re.test(email) == false || re.test(email) == '') {
alert("Bad or Missing Email Address");
}
@moriarty99779
moriarty99779 / gist:d01349749a81ee7f0458e5550371de4e
Last active May 6, 2024 14:06
Javascript Validate IP Address
function validate_ip_address(ipaddress) {
var pattern = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/g;
var validip = pattern.test(ipaddress);
return (validip);
}
@moriarty99779
moriarty99779 / gist:6e85b7d0543a5f2aa41fc35bbf9c41e1
Created May 6, 2024 14:00
Python 3 Find Most Frequent Element in List
from collections import Counter
def most_frequent(lst):
data = Counter(lst)
return data.most_common(1) # returns most frequent 1 element
list = [2, 1, 2, 2, 1, 3, 3, 3, 2, 3, 3, 1]
print(most_frequent(list))
@moriarty99779
moriarty99779 / gist:6e484733843974d6fa560ae620bedc6c
Created May 6, 2024 13:57
Python 3 MySQL Database Connection using MySQL Connector
import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect(host='database_server',
database='database_name',
user='database_username',
password='database_password')
if connection.is_connected():
db_Info = connection.get_server_info()