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 / 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 / phaser.js
Created June 25, 2014 07:03
Wrapper module for running Phaser.js on Node.js.
// this is an ingenius hack that allows us to run Phaser without a browser
// ... and yes, it took some time to figure out how to do this
var Canvas = require('canvas')
, jsdom = require('jsdom')
, document = jsdom.jsdom(null)
, window = document.parentWindow
, Phaser;
// expose a few things to all the modules
@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 / 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 / 99-bottles.ts
Last active September 5, 2022 07:31
My solution for generating the lyrics for the song "99 bottles on the wall" written in TypeScript.
const capitalize = (word: string) => word[0].toUpperCase() + word.substr(1);
const pluralize = (word: string, amount: number) => {
if (amount > 1) return `${amount} ${word}s`;
if (amount === 1) return `${amount} ${word}`;
return `no more ${word}s`;
};
const countBottles = (amount: number) =>
`${pluralize("bottle", amount)} of beer`;
@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 / 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 / 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';