Skip to content

Instantly share code, notes, and snippets.

View pzi's full-sized avatar
🇨🇭
Grüessech

Patrik Affentranger pzi

🇨🇭
Grüessech
View GitHub Profile
@pzi
pzi / object-proxy.ts
Last active May 16, 2023 03:17
Create a type-safe object based on an interface in TypeScript
// TS Playground: https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgDIHsDmoBi6oC2OwEANgCYDOyA3gFDLICul0IcBEAXMpWFKEwNkABziVKAd3zkA-Dz4CQmANx0AvnTpgAniJR5CxMlQA8AFQB8yALy1hAaigQ45dCFI7kAbQDWEL1Bkfx10GGRzAF0AWnlggI0tGCYQBDBgd2ZWYwoAOQ4ISgtLAAoASh5DIhIKIqt7RmcwJigQZBAISWQABSh0AA8dEpp1ZHFkKpyzKwAaBsZkTAgwEoB9OZE+-ShdMvmF5CaWts30bd1hRk0rssS6BHc+ZBga8nzOajsWCCn3wtMMNgQJNXpRSrcDsgAPRQ5AAPVkSVef0oADpvq0CpdIYwYfDES8TCjUWIJNIoORsTi8Qi6IS8gU0aSyTIqZCaYj7o90KQIKjSFgSvS3oz0axMZxbnQ8QBZOAifTkCJ6FAy9DkYCEqCULjS2EACzAYBEOphknNqNCTGaACM+Q8CFDJHAwAh9bIAG42AAMAC9MAB1dCrTAgX0ANQAkkA
interface LoginFormFields {
username: string
password?: string;
}
type FormFields<T> = {
// Mapped Type Modifiers:
// https://www.youtube.com/watch?v=0zgWo_gnzVI
@pzi
pzi / layout-service-factory.test.ts
Created July 8, 2022 07:50
Refactored Layout Service Factory to handle query params
import {placeholderHasQueryParam, processServerUrl, processPlaceholderUrl} from './layout-service-factory';
/**
* @jest-environment node
*/
jest.mock('@sitecore-jss/sitecore-jss-nextjs', () => ({
getPublicUrl: jest.fn().mockReturnValue('http://dev.url'),
}));
@pzi
pzi / context.tsx
Created April 12, 2022 07:27 — forked from JLarky/README.md
Ultimate example of react context hook with nice type-safe (TypeScript) wrappers and reduced boilerplate by using `ReturnType`
import React from "react";
import { useImmer } from "use-immer";
function useProviderValue() {
const [moved, setMoved] = React.useState(false);
const [point, setPoint] = useImmer<{
x: number;
y: number;
}>({ x: 0, y: 0 }); // using immer to illustrate that you can easily derive setPoint type instead of writing types for Context manually
const value = React.useMemo(
@pzi
pzi / pdf-encryption-check.ts
Last active October 11, 2022 13:09
Async method to determine whether or not a PDF has any encryption applied to it.
export const checkPDFEncryption = (file: Blob): Promise<boolean> => {
const allTrailerOccurrences = (source: string, search: string) => {
const trailers = [];
// TODO: Reverse the search as trailers should appear at the end of the file according to the spec.
for (let i = 0; i < source.length; ++i) {
if (source.substring(i, i + search.length) === search) {
trailers.push(i);
}
}
@pzi
pzi / perth_suburbs_geolocations.csv
Created January 13, 2021 10:41
Geolocations of Perth suburbs (via Google Maps API)
Suburb Lat Lng
Alexander Heights -31.8269026 115.8671293
Alfred Cove -32.0318604 115.8181396
Alkimos -31.6253527 115.6899207
Anketell -32.2179086 115.8593099
Applecross -32.0116062 115.8387269
Ardross -32.0221019 115.8353491
Armadale -32.1473012 116.0128764
Ascot -31.9354582 115.9297299
Ashby -31.7331506 115.7993996
@pzi
pzi / blockContent-type.ts
Created September 7, 2020 03:16
FAQ Document & block content section for Sanity
// add to blockContent array
{
type: 'faqSection',
},
@pzi
pzi / snippets.ts
Last active August 10, 2020 01:52
TypeScript helper snippets
// Typed object keys
export const objectKeys = Object.keys as <T>(o: T) => (Extract<keyof T, string>)[];
function isEqualArray(a, b) {
if (a === b) return true
if (a == null || b == null) return a !== a && b !== b
if (a.length !== b.length) return false
const aHasB = a.every(aValue => b.includes(aValue))
const bHasA = b.every(bValue => a.includes(bValue))
return aHasB && bHasA
}
@pzi
pzi / fallback-image.js
Last active June 23, 2020 03:42
Fallback image display if the original src errors (i.e. 404) - http://jsfiddle.net/pezzi/j9Ls2v5r/
function fallbackImage(el) {
if (el.hasAttribute('data-fallback-url')) {
el.onerror = null; // resetting the error so avoid an endless loop
el.src = el.getAttribute('data-fallback-url'); // update the src
el.alt = el.getAttribute('data-fallback-alt') || '' // update the `alt` attribute for a11y
}
}
@pzi
pzi / github_test_ci_workflow.yml
Created April 22, 2020 02:20
GitHub Workflow to cache node_modules and run Jest.
name: Test
on: [push]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix: