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 / conditional_types.tsx
Last active July 23, 2019 03:30
TypeScript: Conditional types and discriminated unions & narrowing
type Block = Important | Emphasis | Regular
type BK = Block['kind']
type Important = { kind: 'important'; important: string }
type Emphasis = { kind: 'emphasis'; emphasis: string }
type Regular = { kind: 'text'; text: string }
type SmartOpts<T extends Block['kind']> = Extract<Block, { kind: T }>
type RegularOptions = SmartOpts<'text'>
@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:
@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
}
}
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 / 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>)[];
@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 / 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 / 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 / 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 / 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'),
}));