Skip to content

Instantly share code, notes, and snippets.

View Goldziher's full-sized avatar
🐙
Codoctupus

Na'aman Hirschfeld Goldziher

🐙
Codoctupus
  • Philipps & Byrne GmbH
  • Berlin
  • 12:45 (UTC +02:00)
  • LinkedIn in/nhirschfeld
View GitHub Profile
@Goldziher
Goldziher / User.dto.factory.ts
Last active August 1, 2021 06:45
Reduced code duplication factory example
export const UserDTOFactory = new FixtureFactory<UserDTO>(() => {
const { profile, ...user } = UserFactory.buildSync();
return {
...user,
...profile,
};
});
@Goldziher
Goldziher / UserProfile.spec.ts
Created August 1, 2021 06:46
Example of using FixtureFactory in a snapshot test
import { UserDTOFactory } from '@shared/factories/User.dto.factory';
import { render } from '@testing-library/react';
import UserProfile from '@frontend/pages/user-profile';
describe('UserProfile', () => {
it('matches snapshot', () => {
const user = UserDTOFactory.fixtureSync(
__dirname + '/UserProfile.spec.json',
)
const { container } = render(<UserProfile user={user} /> );
@Goldziher
Goldziher / backend-api.service.ts
Created August 1, 2021 06:47
Example backend api client using axios
import { UserDTO } from '@shared/dto/user.dto';
import axios, { AxiosInstance } from 'axios';
class BackendClient {
private instance: AxiosInstance;
constructor() {
this.instance = axios.create({
baseURL: process.env.BACKEND_API_URL,
headers: {
@Goldziher
Goldziher / backend-api.service.ts
Created August 1, 2021 06:48
Service method returning mock data
// ...
getUser = async (id: string): Promise<UserDTO> => {
const { data } = await UserDTOFactory.build({ id });
return data;
};
@Goldziher
Goldziher / UserProfile.story.ts
Created August 1, 2021 06:49
Example usage of Interface Forge factory in storybook
import { Meta } from '@storybook/react';
import { UserDTO } from '@shared/dto/User.dto';
import { UserDTOFactory } from '@shared/factories/User.dto.factory';
import React from 'react';
import UserProfile from '@frontend/pages/user-profile';
export default {
title: 'Users/Profile',
component: UserProfile,
} as Meta;
@Goldziher
Goldziher / useInterval.ts
Created October 21, 2021 19:27
React useInterval hook
import { useEffect, useRef } from 'react';
export function useInterval(
callback: (clearInterval: () => void) => any,
delay: number | null,
): () => void {
const idRef = useRef<null | number>(null);
const clearInterval = () => {
if (idRef.current) {
@Goldziher
Goldziher / chunk.py
Created November 10, 2021 13:32
generic python chunk function
from itertools import islice
from typing import Iterator, Sequence, TypeVar
T = TypeVar("T")
def chunk(sequence: Sequence[T], size: int) -> Iterator[list[T]]:
"""given a sequence, return an iterator of lists of the given size"""
return iter(lambda: list(islice(iter(sequence), size)), [])
@Goldziher
Goldziher / duck.js
Last active November 27, 2021 16:50
Duck typing in javascript
function isDuck(value) {
return !!(
value &&
typeof value === 'object' &&
typeof Reflect.get(value, 'quack') === 'function'
);
}
@Goldziher
Goldziher / duck.ts
Last active November 27, 2021 16:50
Duck typing in typescript
interface Duck {
quack(): string;
}
function isDuck(value: unknown): value is Duck {
return !!(
value &&
typeof value === 'object' &&
typeof Reflect.get(value, 'quack') === 'function'
);
@Goldziher
Goldziher / duck.py
Last active November 27, 2021 18:52
python duck typing example
from typing import Any
def is_duck(value: Any) -> bool:
try:
value.quack()
return True
except (Attribute, ValueError):
return False