Skip to content

Instantly share code, notes, and snippets.

View AlexGeb's full-sized avatar
🏠
Working from home

Alexandre Behaghel AlexGeb

🏠
Working from home
View GitHub Profile
@AlexGeb
AlexGeb / hasDefinedKey.ts
Created March 26, 2025 16:46
hasDefinedKey
export const hasDefinedKey =
<K extends PropertyKey>(key: K) =>
<T extends Partial<Record<K, unknown>>>(
obj: T,
): obj is T & Record<K, Exclude<T[K], undefined>> =>
key in obj && obj[key] !== undefined;
@AlexGeb
AlexGeb / split.ts
Created February 6, 2025 09:33
split.ts
type Split<
V extends string,
S extends string,
> = V extends `${infer Head}${S}${infer Rest}`
? [Head, ...Split<Rest, S>]
: Array<V>;
const split = <V extends string, S extends string>(
value: V,
separator: S,
@AlexGeb
AlexGeb / DiseaseAreaRepository.spec.ts
Last active October 23, 2024 13:09
This gist demonstrate the use of sql dataloaders in effect
import { Model } from '@effect/sql';
import { PgClient } from '@effect/sql-pg';
import { layer } from '@effect/vitest';
import {
Array,
Config,
Effect,
Exit,
Fiber,
flow,
@AlexGeb
AlexGeb / FormRenderer.ts
Created September 11, 2024 09:37
Renders a form given an Effect schema
import type { Schema } from '@effect/schema';
import { AST } from '@effect/schema';
import { effectTsResolver } from '@hookform/resolvers/effect-ts';
import { Button } from '@inato/ui';
import { Match, Option } from 'effect';
import type {
FieldValues,
SubmitErrorHandler,
SubmitHandler,
} from 'react-hook-form';
@AlexGeb
AlexGeb / FormReact19.tsx
Created August 25, 2024 22:38
React 19 form example, with custom hook and effect schema validation
import { ArrayFormatter, Schema } from '@effect/schema';
import { Effect, Either, flow, Record } from 'effect';
import { Suspense, use, useActionState } from 'react';
const updateName = async (name: string) => {
await Effect.runPromise(Effect.sleep(2000));
return name;
};
type State<T> =
@AlexGeb
AlexGeb / transform.ts
Last active June 14, 2024 14:43
Change imports of pipe from 'fp-ts/function' to 'effect'
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { API, FileInfo, JSCodeshift } from 'jscodeshift';
const transform = (fileInfo: FileInfo, api: API) => {
const j: JSCodeshift = api.jscodeshift;
const root = j(fileInfo.source);
const replaceFpTsFunctionImportByEffectImport = (name: string) => {
const imports = root.find(j.ImportDeclaration);
const getFpTsFunctionImport = (name: string) =>
@AlexGeb
AlexGeb / TinyUrlShorteningService.ts
Created May 3, 2024 07:18
Example of use of effect platform HttpClient to wrap an api call into a service
import { HttpClient } from '@effect/platform';
import { Schema } from '@effect/schema';
import { Failure, utils } from '@inato/shared-kernel';
import { Effect, Layer, flow } from 'effect';
import { UrlShorteningService } from '~/application/services/UrlShorteningService';
class Response extends Schema.Class<Response>('Response')({
data: Schema.Struct({ tiny_url: utils.CustomSchemas.URL }),
}) {
@AlexGeb
AlexGeb / pqueue.ts
Created January 16, 2024 13:47
Priority Queue implementation with Effect
import { Effect, Queue, pipe } from 'effect';
import type { LazyArg } from 'effect/Function';
class PQueue {
private boundedQueue;
constructor({ concurrency }: { concurrency: number }) {
this.boundedQueue = Effect.runSync(Queue.bounded<any>(concurrency));
}
private queueTask<T>(task: Effect.Effect<never, never, T>) {
@AlexGeb
AlexGeb / effectFromFpTs.ts
Last active November 22, 2023 13:33
fp-ts to effect conversions
import { Context, Effect, pipe } from 'effect';
import { either } from 'fp-ts';
import { ReaderTaskEither } from 'fp-ts/ReaderTaskEither';
import { TaskEither } from 'fp-ts/TaskEither';
export const effectFromEither = either.matchW(Effect.fail, Effect.succeed);
export const effectFromTaskEither = <E, A>(
program: TaskEither<E, A>,
): Effect.Effect<never, E, A> =>
const binomialCoefficient = (n: number, k: number): number => {
if (k < 0 || k > n) {
return 0;
}
if (k === 0 || k === n) {
return 1;
}
if (k === 1 || k === n - 1) {
return n;
}