Skip to content

Instantly share code, notes, and snippets.

View tw3's full-sized avatar

Ted Weatherly tw3

  • Austin, TX
View GitHub Profile
tw3 / trim-on-input.directive.ts
Created February 9, 2024 17:33
Trims text for an Angular form control as the text is input
import { Directive, HostListener } from '@angular/core';
import { NgControl } from '@angular/forms';
// Trims text for an Angular form control as the text is input, e.g. " foobar " -> "foobar"
selector: '[trimOnInput]',
standalone: true,
export class TrimOnInputDirective {
tw3 / parseCsp.js
Last active June 21, 2024 18:50
Parse Content Security Policy (CSP)
// Playground here:
function parseCsp(csp) {
const result = [];
const cspArr = csp.split(';');
cspArr.forEach((x) => {
const vals = x.trim().split(' ').filter(Boolean);
let idx = 0;
const entryArr = [];
entryArr.push(vals[idx++]); // resource, e.g. default-src
tw3 / rotating-iterator.ts
Last active February 18, 2022 14:47
Rotates the starting index and ending index when looping over an array multiple times. At any given point you have the option to reset the starting index and have the array loop to the first item after reaching the end of the array(resetAtNextIndex()). Example usage can be found here:
export class RotatingIterator<T> implements Iterable<T> {
private readonly origStart: number;
private nextIndex: number;
private isDone: boolean;
private hasLooped: boolean;
constructor(private readonly arr: T[], private startIndex = 0) {
this.origStart = startIndex;
this.nextIndex = startIndex;
tw3 / gdbak alias
Created January 27, 2022 17:22
Bash alias that does a git diff to see the files that have changed and creates a tarball of those files in a separate folder. Useful if you want to manually backup your diffs.
alias gdbak='GDBAK_FILENAME="../../temp/diff-bak-$(date +%s%3N)_$(git branch --show-current).tar"; tar cvf "${GDBAK_FILENAME}" `git diff --name-only`; echo "${GDBAK_FILENAME}"'
tw3 /
Last active January 27, 2022 17:18
Bash script to cherry pick any new commits in a reference branch to a source branch. Use at your own risk!
#!/usr/bin/env bash
TARGET_BRANCH=$(git branch --show-current)
if [ -z "${TARGET_BRANCH}" ]; then
echo "Please cd into to the target branch"
echo "Cherry-picking to branch: ${TARGET_BRANCH}"
read -p "Cherry-picking from branch: " SOURCE_BRANCH
tw3 /
Last active January 27, 2022 17:15
Git Bash "script" (for Windows OS) to modify commit messages in bulk, e.g. if you made a bunch of commits listing one ticket id, then decided to create a sub-task ticket and need to update your commits to use that id instead
#!/usr/bin/env bash
main() {
echo "Here's an example:"
echo ""
echo "\"C:/Program Files/Python39/python.exe\" c:/bin/ --commit-callback '"
echo "msg = commit.message.decode(\"utf-8\")"
echo "newmsg = msg.replace(\"TEXT_FROM\", \"TEXT_TO\")"
echo "commit.message = newmsg.encode(\"utf-8\")"
* Example:
* const builder = new XPathBuilder();
* const xpathStr = builder
* .addDescendant({ nodeName: 'carbon-dropdown' })
* .addDescendant({ className: 'bx--label', innerText: 'AWS Region' })
* .addAncestor({ nodeName: 'carbon-dropdown' })
* .build();
tw3 / monitor-active-requests.ts
Last active July 9, 2020 11:52
RxJS operator that keeps a running counter of the number of active requests
import { defer, Observable, ObservableInput } from 'rxjs';
import { finalize } from 'rxjs/operators';
* This is an RxJS operator that keeps a running counter of the number of active requests.
* This can be used to show a spinner for the first of a series of requests, and hide the spinner
* once all requests in the series are complete.
* For example:
* const activeRequestMonitor: ActiveRequestMonitor = monitorActiveRequests({
tw3 / session-expire-alarm.service.ts
Created June 30, 2020 14:07
Service that creates and maintains an alarm that can trigger session timeout warnings and session expiration events
import { Injectable } from '@angular/core';
import {
merge as observableMerge,
of as observableOf,
timer as observableTimer
} from 'rxjs';
import { delay, expand, map, takeUntil, takeWhile } from 'rxjs/operators';
tw3 /
Last active April 6, 2020 18:53
Git pre-commit and pre-push hooks to avoid committing to master or release branches

Git pre-commit and pre-push hook

It will check if current branch is master or a release branch and, if so, prevents a commit or push

To install

  1. Enable git templates
    git config --global init.templatedir '~/.git-templates'