Skip to content

Instantly share code, notes, and snippets.

View karenpayneoregon's full-sized avatar
🎯
Focusing

Karen Payne karenpayneoregon

🎯
Focusing
View GitHub Profile
@karenpayneoregon
karenpayneoregon / Person.cs
Created March 16, 2025 16:26
Record PrintMembers custom
public record Person(string FirstName, string LastName, DateOnly BirthDate, string[] PhoneNumbers)
{
protected virtual bool PrintMembers(StringBuilder sb)
{
sb.Append($"FirstName = {FirstName}, LastName = {LastName}, Birth = {BirthDate:MM/dd/yyyy}");
if (!(PhoneNumbers?.Length > 0)) return true;
sb.Append(", PhoneNumbers: ");
sb.Append(string.Join(", ", PhoneNumbers));
@karenpayneoregon
karenpayneoregon / DateTimeHelpers.cs
Last active March 16, 2025 19:12
How AI can assist a developer
public static class DateTimeHelpers
{
/// <summary>
/// Generates a list of dates representing the next week's dates starting from the upcoming Sunday.
/// </summary>
/// <returns>A list of <see cref="DateOnly"/> objects representing the dates of the next week.</returns>
public static List<DateOnly> NextWeeksDates()
{
var start = DateTime.Now;
var nextSunday = DateOnly.FromDateTime(start).Next(DayOfWeek.Sunday);
@karenpayneoregon
karenpayneoregon / FixedDecimalJsonConverter.cs
Created March 13, 2025 21:34
Serialize decimals to 2 places
/// <summary>
/// Provides a custom JSON converter for <see cref="decimal"/> values, ensuring consistent formatting and parsing.
/// </summary>
/// <remarks>
/// This converter reads <see cref="decimal"/> values from JSON as strings and parses them using
/// <see cref="CultureInfo.InvariantCulture"/>. When writing, it formats <see cref="decimal"/> values
/// as strings with two decimal places using the same culture.
/// </remarks>
public class FixedDecimalJsonConverter : JsonConverter<decimal>
{
@karenpayneoregon
karenpayneoregon / Demo.cs
Last active March 12, 2025 13:03
C# extension to extracts all substrings enclosed in quotes from the provided string.
string text = "Splits a \"PascalCase\" or \"camelCase\" string into " +
"separate \"words\" by inserting \"spaces\" before \"uppercase letters\".";
var parts = text.StringsBetweenQuotes1();
foreach (var item in parts)
{
Console.WriteLine(item);
}
@karenpayneoregon
karenpayneoregon / Demo.cs
Last active February 26, 2025 22:39
C# 11 list pattern
using System.Text.Json;
namespace TODO;
class Samples
{
public static void IntegerListMatch()
{
List<List<int>> list =
@karenpayneoregon
karenpayneoregon / accordionUtilities1.js
Created February 24, 2025 22:08
Toggle accordion item by id
function expandToolSectionAccordionItem() {
let collapseElement = document.getElementById('collapseTools');
if (collapseElement) {
let bsCollapse = new bootstrap.Collapse(collapseElement, {
toggle: true
});
}
}
@karenpayneoregon
karenpayneoregon / accordionUtilities.js
Created February 24, 2025 17:47
Provides JavaScript helper methods to expand and collapse all items in a Bootstrap 5.3 accordion
function expandAll() {
document.querySelectorAll('.accordion-collapse').forEach(item => {
let bsCollapse = new bootstrap.Collapse(item, { toggle: false });
bsCollapse.show();
});
}
function collapseAll() {
document.querySelectorAll('.accordion-collapse').forEach(item => {
let bsCollapse = new bootstrap.Collapse(item, { toggle: false });
@karenpayneoregon
karenpayneoregon / Helpers.cs
Created February 14, 2025 14:13
For Twitter
string DefaultInterpolatedStringHandler()
{
var title = "Mr.";
var firstName = "John";
var middleName = "Q.";
var lastName = "Doe";
DefaultInterpolatedStringHandler stringHandler = new(50, 4);
stringHandler.AppendLiteral("Hello ");
stringHandler.AppendFormatted(title);
stringHandler.AppendLiteral(" ");
@karenpayneoregon
karenpayneoregon / demo.html
Last active February 12, 2025 22:53
Scrollable modal
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Bootstrap scrollable modal</title>
<link href="Lib/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
<!-- REQUIRED -->
<script src="Lib/bootstrap/js/bootstrap.bundle.min.js"></script>
@karenpayneoregon
karenpayneoregon / ExceptionHelpers.cs
Last active February 10, 2025 22:38
Spectre.Console custom colored runtime exceptions
using Spectre.Console;
namespace SpectreExceptionsLibrary;
/// <summary>
/// Custom setting for presenting runtime exceptions using AnsiConsole.WriteException.
///
/// The idea here is to present different types of exceptions with different colors while
/// one would be for all exceptions and the other(s) for specific exception types.
/// </summary>