Recreating ProfanityFilterService in Effect-TS with Dependency Injection
I want to recreate this service in Effect-TS
my initial idea was to create two services, one that instantiates and returns the CensorSensor class, and one that injects that instance & provides the methods
What can be improved? (or rather, how many mistakes did I make)
export class ProfanityFilterService {
private readonly censorSensor: CensorSensor;
constructor() {
this.censorSensor = new CensorSensor();
for (let index = 2; index <= 5; index++) {
this.censorSensor.disableTier(index);
}
}
cleanProfanity(stringToClean: string) {
return this.censorSensor.cleanProfanity(stringToClean);
}
isProfane(stringToCheck: string) {
return this.censorSensor.isProfane(stringToCheck);
}
}export class ProfanityFilterService {
private readonly censorSensor: CensorSensor;
constructor() {
this.censorSensor = new CensorSensor();
for (let index = 2; index <= 5; index++) {
this.censorSensor.disableTier(index);
}
}
cleanProfanity(stringToClean: string) {
return this.censorSensor.cleanProfanity(stringToClean);
}
isProfane(stringToCheck: string) {
return this.censorSensor.isProfane(stringToCheck);
}
}my initial idea was to create two services, one that instantiates and returns the CensorSensor class, and one that injects that instance & provides the methods
import { CensorSensor } from "censor-sensor";
import { Context, Effect, Layer } from "effect";
class CensorSensorService extends Context.Tag("CensorSensorService")<
CensorSensorService,
CensorSensor
>() {
static readonly Live = Layer.effect(
this,
Effect.gen(function* () {
const censorSensor = new CensorSensor();
// ... modify the tier
return censorSensor;
})
);
}
const make = Effect.gen(function* () {
const censorSensor = yield* CensorSensorService;
const cleanProfanity = (stringToClean: string) => {
return Effect.sync(() => censorSensor.cleanProfanity(stringToClean));
};
const isProfane = (stringToCheck: string) => {
return Effect.sync(() => censorSensor.isProfane(stringToCheck));
};
return {
cleanProfanity,
isProfane
} as const;
});
export class ProfanityFilterService extends Context.Tag("ProfanityFilterService")<
ProfanityFilterService,
Effect.Effect.Success<typeof make>
>() {
static readonly Live = Layer.effect(this, make).pipe(Layer.provide(CensorSensorService.Live));
}import { CensorSensor } from "censor-sensor";
import { Context, Effect, Layer } from "effect";
class CensorSensorService extends Context.Tag("CensorSensorService")<
CensorSensorService,
CensorSensor
>() {
static readonly Live = Layer.effect(
this,
Effect.gen(function* () {
const censorSensor = new CensorSensor();
// ... modify the tier
return censorSensor;
})
);
}
const make = Effect.gen(function* () {
const censorSensor = yield* CensorSensorService;
const cleanProfanity = (stringToClean: string) => {
return Effect.sync(() => censorSensor.cleanProfanity(stringToClean));
};
const isProfane = (stringToCheck: string) => {
return Effect.sync(() => censorSensor.isProfane(stringToCheck));
};
return {
cleanProfanity,
isProfane
} as const;
});
export class ProfanityFilterService extends Context.Tag("ProfanityFilterService")<
ProfanityFilterService,
Effect.Effect.Success<typeof make>
>() {
static readonly Live = Layer.effect(this, make).pipe(Layer.provide(CensorSensorService.Live));
}What can be improved? (or rather, how many mistakes did I make)
