Skip to content

Instantly share code, notes, and snippets.

View MaikelVeen's full-sized avatar

Maikel Veen MaikelVeen

View GitHub Profile
@MaikelVeen
MaikelVeen / retry-main.go
Created March 6, 2022 12:16
Using the Retry pattern
var count int
func GetPdfUrl(ctx context.Context) (string, error) {
count++
if count <= 3 {
return "", errors.New("boom")
} else {
return "https://linktopdf.com", nil
}
@MaikelVeen
MaikelVeen / retry.go
Created March 6, 2022 12:15
The Retry Pattern in Go
type Effector func(context.Context) (string, error)
func Retry(effector Effector, retries int, delay time.Duration) Effector {
return func(ctx context.Context) (string, error) {
for r := 0; ; r++ {
response, err := effector(ctx)
if err == nil || r >= retries {
// Return when there is no error or the maximum amount
// of retries is reached.
return response, err
@MaikelVeen
MaikelVeen / squares.go
Created March 6, 2022 11:59
An example of an closure / anonymous function
// squares returns a function that returns
// the next square number each time it is called.
func squares() func() int {
var x int
return func() int {
x++
return x * x
}
}
const GetUrlElement = ({ host, route, date }: Url): string => {
if (date) {
return `<url><loc>${host}${route}</loc><lastmod>${date}</lastmod></url>`;
} else return `<url><loc>${host}${route}</loc></url>`;
};
const GetSitemapXml = (urls: Array<Url>): string => `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${urls.map((url) => GetUrlElement(url)).join('')}
</urlset>`;
@MaikelVeen
MaikelVeen / sitemap.tsx
Created February 28, 2021 12:44
Full sitemap functionality
import { GetServerSideProps } from 'next';
import fs from 'fs';
import path from 'path';
// This functions returns a domain based on the NODE_ENV setting
import { GetCurrentDomain } from '../utils/UrlBuilder';
export default function Sitemap() {}
type Url = {
<nav class="c-breadcrumb">
<ol itemscope="" itemtype="https://schema.org/BreadcrumbList" class="breadcrumb__list">
<li itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem" class="breadcrumb-element">
<a itemprop="item" href="https://example.com/" class="breadcrumb-element__link"
><span itemprop="name" class="breadcrumb-element__name">Home</span></a
><meta itemprop="position" content="1" />
</li>
<span class="breadcrumb-element__divider">&gt;</span>
<li itemprop="itemListElement" itemscope="" itemtype="https://schema.org/ListItem" class="breadcrumb-element">
<a itemprop="item" href="https://example.com/bikes" class="breadcrumb-element__link"
<Breadcrumb breadcrumbItems={breadcrumb} />
/// Data passed could be something like:
const breadcrumb = [
{url:'https://example.com/', name:"Home"},
{url:'https://example.com/bikes', name:"Bikes"},
{url:'https://example.com/bikes/bike-x', name:"Bike-X"}];
function BreadcrumbItem({ url, name, index, isLast }:
{ url: string, name: string, index: number, isLast: boolean }) {
return (
<React.Fragment>
<li itemProp="itemListElement" itemScope
itemType="https://schema.org/ListItem" key={index} className="breadcrumb-element">
<a itemProp="item" href={url} className="breadcrumb-element__link">
<span itemProp="name" className="breadcrumb-element__name">{name}</span></a>
<meta itemProp="position" content={index.toString()} />
</li>
export function Breadcrumb({ breadcrumbItems }: BreadcrumbProps) {
return (
<nav className="c-breadcrumb">
<ol itemScope itemType="https://schema.org/BreadcrumbList" className="breadcrumb__list">
{breadcrumbItems.map((item, index, array) =>
<BreadcrumbItem {...item} index={index + 1} isLast={array.length - 1 === index} key={index} />
</ol>
</nav>
)
}
interface BreadcrumbProps {
breadcrumbItems: Array<BreadcrumbItemProps>;
}
interface BreadcrumbItemProps {
url: string
name: string
}