Skip to content

Instantly share code, notes, and snippets.

View mattkenefick's full-sized avatar
🏠
Working from home

Matt Kenefick mattkenefick

🏠
Working from home
View GitHub Profile
@mattkenefick
mattkenefick / base-command.php
Created August 14, 2021 16:47
Magic safety in PHP
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
abstract class BaseCommand extends Command
{
/**
* Attempt to attach template over the network.
* It attempts to derive an HTML tag from the filename,
* but we could do anything here.
*
* @param string filename
*/
static async attachRemote(filename) {
const filenameMatches = filename.match(/\/([^\.\/]+)\.html/i);
@mattkenefick
mattkenefick / index.html
Last active August 4, 2021 15:03
Shadow DOM
<main>
<my-paragraph>
Lorem ispum sit amet dolor
</my-paragraph>
<hr />
<labeled-input>
This is the form label
</labeled-input>
@mattkenefick
mattkenefick / example.cs
Created August 4, 2021 14:12
Snippets in JavaScript: Converting PascaleCase to kebab-case
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main() {
string input = "MyParagraphElement";
string output = Regex.Replace(input, @"([a-z0–9])([A-Z])", "$1-$2").ToLower();
 
Console.WriteLine(output);
@mattkenefick
mattkenefick / breakdown.cs
Created July 31, 2021 17:51
Snippets in C#: More ways to check for NULL
// Option B: Checks if is NOT null
if (dog?.breed is { }) return dog.breed;
// Option C: Checks if is NOT null, then assigns to local variable "breedA"
if (dog?.breed is { } breedA) return breedA;
// Option D: Uses property pattern to check on object and return props as variables
if (dog?.breed is { Length: var lengthA } breedB) return $"{breedB} ({lengthA} characters long)";
@mattkenefick
mattkenefick / breakdown.js
Created July 30, 2021 01:37
Regular Expressions: Markdown to HTML anchors
/ \[([^\]]+)\]\(([^\)]+)\) /
\[([^\]]+)\]
\[ Look for a literal left bracket, by escaping it
( Start a capture group to retrieve the contents
[^\]]+ Repeatedly find a character that isn't a closing bracket
) Close the capture group
\] Look for a literal right bracket, by escaping it
\(([^\)]+)\)
@mattkenefick
mattkenefick / basic-pluck.php
Last active July 30, 2021 01:16
Snippets in PHP: PluckMultiple for Laravel
<?PHP
// Setup code
// ...
$emails = Models\User::pluck('email');
@mattkenefick
mattkenefick / example.js
Created July 30, 2021 00:34
Snippets in JavaScript: Alternative Alphabet
/**
* Generate a range of characters based on strings
*
* @param string startCharacter
* @param string endCharacter
* @return string[]
*/
function generateCharacterRange(startCharacter, endCharacter) {
let letters = [];
@mattkenefick
mattkenefick / example.js
Created July 30, 2021 00:25
Snippets in JavaScript: Alphabet as array
String.fromCharCode(...Array(123).keys()).slice(97).split('');
// ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
@mattkenefick
mattkenefick / example.ts
Last active July 29, 2021 23:54
Snippet in TypeScript: Extract YouTube ID with Regular Expressions
/**
* Check URL against Regular Expression
*
* @param string url
* @return string|boolean
*/
function parseYoutubeId(url: string): string|boolean {
const regexp: RegExp = new RegExp('(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/\s]{11})', 'i');
const matches: string[] = url.match(regexp);