Add isword module

This commit is contained in:
2023-09-05 11:41:43 -06:00
parent 48d66db30f
commit cc2a1971f0
5 changed files with 58 additions and 2 deletions

View File

@@ -1,4 +1,11 @@
FROM node:18
FROM node:18-ubuntu
# Install american word lists for /isword
RUN apt-get update && apt-get install -y \
wamerican \
# wamerican-insane \
&& rm -rf /var/lib/apt/lists/*
ADD package.json .
ADD yarn.lock .

View File

@@ -2,9 +2,10 @@ import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ParkioModule } from './parkio/parkio.module';
import { IswordModule } from './isword/isword.module';
@Module({
imports: [ParkioModule],
imports: [ParkioModule, IswordModule],
controllers: [AppController],
providers: [AppService],
})

View File

@@ -0,0 +1,24 @@
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import { IswordService } from './isword.service';
export type AreWordsDto = string[]
@Controller('isword')
export class IswordController {
constructor(
private readonly isWordService: IswordService
) {}
@Get(':word')
isWord(@Param('word') word: string) {
return this.isWordService.isWord(word)
}
@Post()
areWords(@Body() body: AreWordsDto) {
return body.reduce((acc, word) => {
acc[word] = this.isWordService.isWord(word)
return acc;
}, {} as {[key: string]: boolean})
}
}

View File

@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { IswordService } from './isword.service';
import { IswordController } from './isword.controller';
@Module({
providers: [IswordService],
controllers: [IswordController]
})
export class IswordModule {}

View File

@@ -0,0 +1,15 @@
import { Injectable } from '@nestjs/common';
import { readFileSync } from 'fs';
@Injectable()
export class IswordService {
private readonly dict: string = '/usr/share/dict/words';
private readonly dictContents: string[];
constructor() {
this.dictContents = readFileSync(this.dict).toString().split('\n')
}
isWord = (word: string) =>
this.dictContents.includes(word)
}