Skip to content

Instantly share code, notes, and snippets.

View graffhyrum's full-sized avatar

Joshua Pendragon graffhyrum

View GitHub Profile
import {Locator} from '@playwright/test';
/**
* Wait for `elem` to have no mutations for a period of
* `noMutationDuration` ms. If `timeout` ms elapse without a "no mutation"
* period of sufficient length, throw an error.
* @param elem
* @param noMutationDuration
* @param mutTimeout
* @param waitForFirstMutation If true, wait until the first mutation before
@graffhyrum
graffhyrum / poker.rs
Created May 17, 2023 20:59
Rust Exercism Poker challenge
/// Given a list of poker hands, return a list of those hands which win.
///
/// Note the type signature: this function should return _the same_ reference to
/// the winning hand(s) as were passed in, not reconstructed strings which happen to be equal.
pub fn winning_hands<'a>(hands: &[&'a str]) -> Vec<&'a str> {
let mut winning_hands = Vec::new();
let mut winning_hand_rank = rank_hand(hands[0]);
winning_hands.push(hands[0]);
if hands.len() == 1 {
return winning_hands;
@graffhyrum
graffhyrum / lib.rs
Created May 17, 2023 21:01
Exercism Rust Minesweeper
pub fn annotate(minefield: &[&str]) -> Vec<String> {
let row_count = minefield.len();
if row_count == 0 {
return vec![];
}
let col_count = minefield[0].len();
if col_count == 0 {
return vec![String::new()];
}
// 2d u8 vector, 0 is space, 9 is a mine, 1-8 is number of mines
@graffhyrum
graffhyrum / one_of_type.ts
Created May 17, 2023 21:06
Typescript generic to constrain to one key of a given interface/signature
/**
* A custom utility type to allow an object to be only one Type from the provided
* Type.
* @example
* type OneOf = OneOfType<{userId:string, userEmail:string}>;
* //allows {userId:string} || {userEmail:string}, but not both
*/
export type OneOfType<T> = ValueOf<OneOfByKey<T>>;
type OneOfByKey<T> = {[key in keyof T]: OneOnly<T, key>};
type OneOnly<Obj, Key extends keyof Obj> = {
//annotation_list.ts
export const ANNOTATIONKEY = ['fixme', 'skip', 'fail', 'slow'] as const;
export type AnnotationKey = (typeof ANNOTATIONKEY)[number];
type Defect = {
jira: string;
ids: string[];
};
type AnnotationList = Record<AnnotationKey, Defect[]>;
@graffhyrum
graffhyrum / tsconfig.json5
Created September 15, 2023 16:36
Matt Pocock tsconfig defaults
{
"compilerOptions": {
/* Basic Options */
esModuleInterop: true,
forceConsistentCasingInFileNames: true,
skipLibCheck: true,
target: "es2022",
verbatimModuleSyntax: true,
allowJs: true,
resolveJsonModule: true,
@graffhyrum
graffhyrum / devPagePom.ts
Created September 20, 2023 23:43
Class-based POM vs factory function POM
import {expect, Locator, Page} from '@playwright/test';
// Page Object Model
export class PlaywrightDevPage {
readonly page: Page;
readonly getStartedLink: Locator;
readonly gettingStartedHeader: Locator;
readonly pomLink: Locator;
readonly tocList: Locator;
@graffhyrum
graffhyrum / json.ts
Created September 28, 2023 21:49
Typescript JSON type
/**
* JSONValue is a type alias for narrowing or validating JSON.
* Requires an indirection via the DelayedJsonValue interface
* to establish a 'root' type, Record<string,JSONValue> causes
* recursion errors.
*/
export type JSONValue =
| JsonPrimitive
| JsonArray
| JsonObject
@graffhyrum
graffhyrum / xivlogger_script.ps1
Created December 18, 2023 22:23
Script to parse FFXIV text logs, returns a transcript for all logs that include any two characters.
# Script to parse FFXIV text logs, returns a transcript for all logs that include any two characters.
# script will prompt for:
# the name of the file (do not include extension, file should be in the same directory as the script)
# The first name to look for
# The second name to look for
$file = Read-Host -Prompt "Enter the txt file name:"
$nameOfUser = Read-Host -Prompt "Paste or enter the first character's name:"
$personName = Read-Host -Prompt "Paste or enter the second character's name:"
$input_path = $PSScriptRoot + "\" + $file + '.txt'
$output_file = $PSScriptRoot + "\" + $file + "_" + $personName + '_parsedLog2.txt'
@graffhyrum
graffhyrum / branded_types.md
Last active May 20, 2026 23:07
Typescript Branded Type

Branded Types

A "Branded" type is a type that is a subtype of the original type, but has a unique literal value in a common field (the brand). This allows us to define types that are more specific than the original type, but are still compatible with it. For example, we have the type EmailAddress, which is a string that is guaranteed to be a valid email address.

Branded types can only be created by calling the brand function, which takes a value of the original type and returns a value of the branded type.

Usage