mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
feat: create getLanguage() function, used for testing purposes
This commit is contained in:
parent
0d126ad3b7
commit
de8eba4079
1 changed files with 14 additions and 0 deletions
14
src/test.ts
14
src/test.ts
|
|
@ -1,3 +1,5 @@
|
|||
import ISO6391, { LanguageCode } from 'iso-639-1';
|
||||
import lande from 'lande';
|
||||
import { NostrEvent } from '@nostrify/nostrify';
|
||||
import { finalizeEvent, generateSecretKey } from 'nostr-tools';
|
||||
|
||||
|
|
@ -65,3 +67,15 @@ export async function createTestDB() {
|
|||
export function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
export function getLanguage(text: string): LanguageCode | undefined {
|
||||
const [topResult] = lande(text);
|
||||
if (topResult) {
|
||||
const [iso6393] = topResult;
|
||||
const locale = new Intl.Locale(iso6393);
|
||||
if (ISO6391.validate(locale.language)) {
|
||||
return locale.language as LanguageCode;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue