Skip to content

Instantly share code, notes, and snippets.

View crisu83's full-sized avatar
🎩
Coding

Christoffer Niska crisu83

🎩
Coding
View GitHub Profile
@crisu83
crisu83 / openapi.test.ts
Last active December 12, 2023 19:48
Express + Zod to OpenAPI
const mockRegistry = { definitions: {}, registerPath: jest.fn() };
const mockGenerator = { generateDocument: jest.fn() };
jest.mock('@asteasolutions/zod-to-openapi', () => ({
extendZodWithOpenApi: jest.fn(),
OpenAPIRegistry: function () {
return mockRegistry;
},
OpenApiGeneratorV3: function () {
return mockGenerator;
@crisu83
crisu83 / Validator.kt
Last active September 5, 2022 07:22
A validator implementation written in Kotlin.
sealed interface Validator {
fun validate(value: String): Int?
}
object RequiredValidator : Validator {
override fun validate(value: String): Int? =
if (value.isNotBlank()) null else ValidationError.Required
}
object EmailValidator : Validator {
@crisu83
crisu83 / Example.kt
Last active January 11, 2022 09:12
A predictable state container (like Redux) written in Kotlin for use with Android view models.
data class CounterUiState(val counter: Int = 0) : State
sealed class CounterAction : Action {
object Increment : CounterAction()
object Decrement : CounterAction()
data class SetValue(val value: Int) : CounterAction()
}
class CounterViewModel : ViewModel() {
@crisu83
crisu83 / DatePicker.kt
Last active February 12, 2024 10:22
DatePicker and TimePicker components for Jetpack Compose.
@Composable
fun DatePicker(
label: String,
value: String,
onValueChange: (String) -> Unit = {},
keyboardActions: KeyboardActions = KeyboardActions.Default,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
pattern: String = "yyyy-MM-dd",
) {
val formatter = DateTimeFormatter.ofPattern(pattern)
@crisu83
crisu83 / settings.json
Last active March 28, 2023 14:08
My VSCode settings
{
"[rust]": {
"editor.formatOnSave": true
},
"[typescript]": {
"editor.formatOnSave": true
},
"[typescriptreact]": {
"editor.formatOnSave": true
},
@crisu83
crisu83 / GuessANumber.kt
Last active September 5, 2022 07:25
A "guess a number game" implementation written in Kotlin that we wrote together with my 12 year old son when learning basic concepts in programming.
import kotlin.random.Random
fun main() {
val secretNumber = Random.nextInt(1, 100)
var numTries = 0
var numTriesLeft: Int
var gameWon = false
println("Welcome to Guess a number!")
@crisu83
crisu83 / LoginScreen.kt
Last active October 5, 2021 09:28
LoginScreen
@ExperimentalComposeUiApi
@Composable
fun LoginScreen(
viewModel: LoginViewModel,
navigateToOverview: () -> Unit = {},
onBack: () -> Unit = {},
) {
val uiState by viewModel.uiState.collectAsState()
val (username, setUsername) = viewModel.username
@crisu83
crisu83 / graphql.ts
Created September 4, 2021 09:40
GraphQL API route with remote schema
// See: https://github.com/vercel/next.js/blob/canary/examples/api-routes-apollo-server/pages/api/graphql.js
import {AsyncExecutor} from '@graphql-tools/delegate';
import {introspectSchema, wrapSchema} from '@graphql-tools/wrap';
import {ApolloServer} from 'apollo-server-micro';
import {print} from 'graphql';
import fetch from 'isomorphic-unfetch';
import {NextApiRequest, NextApiResponse} from 'next';
import getConfig from 'next/config';
@crisu83
crisu83 / graphql.ts
Created September 3, 2021 17:57
GraphQL API route
import {ApolloServer} from 'apollo-server-micro';
import {NextApiRequest, NextApiResponse} from 'next';
import {resolvers} from '@/graphql/resolvers';
import typeDefs from '@/graphql/schema.graphql';
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return new ApolloServer(
resolvers,
@crisu83
crisu83 / button.tsx
Last active May 19, 2021 09:02
Design system written in TypeScript and built on top of Style System (and Styled Components).
// Example button component
import shouldForwardProp from '@styled-system/should-forward-prop';
import {darken, lighten} from 'polished';
import React, {FunctionComponent, forwardRef} from 'react';
import {variant as variantFn} from 'styled-system';
import {
Box,
BoxPropsWithRef,