Skip to content

Instantly share code, notes, and snippets.

@psaxton
psaxton / lsup.sh
Last active January 6, 2022 23:06
sh function for finding the first existence of a file in a parent directory. Usage: `lsup [path] file`
lsup() {
if [ $# -eq 1 ]; then
local file=${1} path=$(pwd)
elif [ $# -eq 2 ]; then
local file=${2} path=$(realpath -es "${1}")
else
echo >&2 "Usage: lsup [path] filename"
echo >&2 " At least filename is required"
return 1
fi
@psaxton
psaxton / fix-swagger-collection-parameters.jq
Created February 24, 2021 20:19
JQ to add `collectionFormat` property to array type parameters in a swagger document
walk(
if (type == "object" and has("parameters") ) then
.parameters |= [
.[] | if ( .schema.type == "array" ) then
.schema.collectionFormat |= "csv"
else
.
end
]
) else
@psaxton
psaxton / DefaultingEnumConverter.cs
Last active March 1, 2019 15:11
Provides a Enum reader for Newtonsoft.Json which will return the 0th (default) enum value if the provided value cannot be deserialized.
/// File: DefaultingEnumConverter.cs
using System;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public class DefaultingEnumConverter : JsonConverter
{
@psaxton
psaxton / git_latest_all.sh
Last active August 27, 2018 21:39
Gets latest develop branch from default remote git repository
#!/bin/bash
for gitdir in $(find . -maxdepth 1 -type d -exec test -e '{}'/.git \; -print); do
pushd $gitdir > /dev/null 2>&1;
if [ -z "$(git status --short)" ]
then
git checkout develop > /dev/null 2>&1;
git fetch > /dev/null 2>&1;
git pull > /dev/null 2>&1;
echo \'$gitdir\' has been updated to latest.;
@psaxton
psaxton / .gitconfig
Last active August 24, 2020 21:47
Handy default git aliases
[alias]
browse = "! f() {\n local CurrentBranch=$(git symbolic-ref --short HEAD);\n local DefaultRemote=$(git config branch.${CurrentBranch}.pushRemote || git config branch.${CurrentBranch}.remote || git config remote.pushDefault || echo origin);\n local RemoteUrl=$(git remote get-url ${DefaultRemote} 2> /dev/null);\n if (test -n \"${RemoteUrl}\") then\n start \"${RemoteUrl}\";\n else\n echo \"No remote url found!\";\n fi\n}; f"
graph = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)'
ignore = ! gi() {\n local outfile="$([ $1 != list ] && echo '-o ./.gitignore')"\n local parts="$(printf "%s," "$@")"\n parts="${parts%,}"\n curl -sL ${outfile} https://www.toptal.com/developers/gitignore/api/${parts}\n}; gi
latest = fetch --all --prune --tags --progress
[diff]
tool = bc
[difftool]
prompt = false
[difftool "bc"]
@psaxton
psaxton / IEnumerableExtensions.ToDataTable.cs
Created March 20, 2017 17:01
Extension method to convert IEnumerable<T> to a DataTable for use in Table Value Type parameters
using System.Collections.Generic;
using System.Data;
using System.Linq;
public static class IEnumerableExtensions
{
public static DataTable ToDataTable<T>(this IEnumerable<T> value)
{
var result = new DataTable();
var tType = typeof(T);
@psaxton
psaxton / LINQPad F# Test Runner.fs
Last active November 22, 2016 23:10
Iterates over all modules with name ending in "Test", Gets the properties from the module and dumps the property name and result to results window. Append to https://gist.github.com/psaxton/c6ec6841a63bf0092304787697dc3beb for an example of use.
/// Iterates over all modules with name ending in "Test",
/// Gets the properties from the module and dumps the property
/// name and result to results window. Append to https://gist.github.com/psaxton/c6ec6841a63bf0092304787697dc3beb
/// for an example of use.
Assembly.GetExecutingAssembly().ExportedTypes
|> Seq.where (fun t -> t.FullName.EndsWith("+Test"))
|> Seq.collect (fun t -> t.GetProperties())
|> Seq.map (fun p -> sprintf "%s %s: %A" (p.DeclaringType.FullName.Replace('+', ' ')) p.Name (p.GetValue(None)))
|> Seq.reduce (fun testA testB -> sprintf "%s\n%s" testA testB)
|> Extensions.Dump
@psaxton
psaxton / Trie.fs
Last active November 7, 2016 16:02
type Trie<'T when 'T : comparison> = {
IsTerminal: bool;
Children: Map<'T, Trie<'T>>;
}
module Trie =
module Map =
let addOrReplace key value map =
map
|> Map.remove key