Skip to content

Instantly share code, notes, and snippets.

View rudfoss's full-sized avatar

Thomas Haugland Rudfoss rudfoss

View GitHub Profile
@rudfoss
rudfoss / DotEnv.cs
Last active August 12, 2024 11:50
A very simple parser for loading .env files in a c# application. Has no external dependencies and supports basic references.
namespace DotEnv
{
public class DotEnv(ILogger<DotEnv> logger)
{
/// <summary>
/// Holds a set of all environment variables set from .env files. Listed if logLevel set do debug.
/// </summary>
private readonly IDictionary<string, string> _finalEnvironmentVariables = new Dictionary<string, string>();
private void LoadAndParse()
@rudfoss
rudfoss / convertDataToCsvRows.ts
Created March 26, 2024 10:40
A small set of helper functions that convert raw data to CSV which in turn can be downloaded. Uses no libraries and escapes necessary characters.
const sanitizeCellValue = (value: unknown, cellSeparator: string): string => {
let valueString = String(value ?? "")
if (
valueString.includes(cellSeparator) ||
valueString.includes(" ") ||
valueString.includes('"') ||
valueString.includes("\n")
) {
valueString = valueString.replaceAll('"', '""')
valueString = `"${valueString}"`
@rudfoss
rudfoss / cross-stage-variables-azure-devops-yaml-pipeline.yml
Last active September 9, 2024 05:55
Cheat-sheet for setting and retrieving variables in Azure DevOps yaml pipelines across different steps, jobs and stages.
# Cheat-sheet for using dynamically defined variables between steps, jobs and stages in Azure DevOps Yaml Pipelines
# Documentation: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch
trigger: none
pr: none
stages:
- stage: DefineVarStage
jobs:
- job: DefineVarJob
@rudfoss
rudfoss / typescript.code-snippets
Created January 2, 2023 10:33
TypeScript code snippets
{
"React Component": {
"prefix": "react-component",
"description": "Creates a react function component without props",
"body": [
"import React from \"react\"",
"",
"export interface ${1:name}Props {",
"\t${0}",
"}",
@rudfoss
rudfoss / ExtendedPropsComponent.tsx
Last active May 28, 2022 11:37
React functional component with extended props
import React from "react";
// This can be defined in some other file as well.
interface SharedProps {
active: boolean
title?: string
}
type MyProps = Omit<SharedProps, "title"> & {
specialTitle: string
@rudfoss
rudfoss / SanitySchema.ts
Last active August 28, 2021 14:14
A starter file of typescript types for Sanity CMS schema definitions. It is by no means complete, but covers most of the basic use cases I've run into.
import React from "react"
import { SanityDocument as SanityQueryDocument, SanityDocumentStub } from "@sanity/client"
/**
* A Sanity name string. Can only a-z A-Z 0-9 and underscore.
*/
export type SanityName = string
export interface SanitySelectorSet {
[key: string]: string
@rudfoss
rudfoss / sanityConditions.ts
Created March 10, 2021 12:38
A recursive schema for building conditions within Sanity.io Condition functions must be implemented as separate input components and handled accordingly, but the basic structure is present.
export const conditionFunction = {
title: "Condition function",
name: "conditionFunction",
type: "object",
fields: [
{
title: "Function name",
name: "name",
type: "string",
description: "The type of condition",
@rudfoss
rudfoss / Set-AppReplyUrlMSGraph.ps1
Last active February 5, 2021 11:13
A small script demonstrating how to authenticate with MS Graph and update redirect uris for an application.
<#
.SYNOPSIS
This script demonstrates how to authenticate with the Microsoft Graph and update the redirect uris for an app registration
Requirements:
1. Register an application in the tenant (management app)
2. Grant the app the following API permissions:
- Application.ReadWrite.All
@rudfoss
rudfoss / MemoryCache.ts
Created January 31, 2021 21:11
A really simple memory cache class that supports running functions on cache expiration.
/**
* A key for an entry in the cache.
*/
type CacheKey = string | symbol
export class CacheError<TInnerError extends Error, TData> extends Error {
public constructor(public innerError: TInnerError, public data: TData) {
super(innerError.message)
}
}
@rudfoss
rudfoss / multi-job-variable-azure-pipeline.yml
Created January 29, 2021 07:34
This gist shows how to use variables defined in one deployment job in another with the runOnce strategy.
name: TEST-$(Date:yyyyMMdd)-$(Build.BuildId)
trigger: none
parameters:
- name: env
type: string
default: 'test'
variables: