Tools for learning Japanese.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

86 Zeilen
1.9 KiB

  1. import { describe, it, afterAll } from 'vitest';
  2. import { createReadStream, createWriteStream } from 'fs';
  3. import { unlink } from 'fs/promises';
  4. import { createDownloader, createXmlToJsonLines, Kanjidic } from '../../src';
  5. const DOWNLOAD_FILENAME = 'kanjidic2.xml' as const;
  6. const PROCESS_OUTPUT_FILENAME = 'kanjidic2.jsonl' as const;
  7. // FIXME use tests that do not download the source data
  8. describe.skip('kanjidic', () => {
  9. afterAll(async () => {
  10. try {
  11. await unlink(DOWNLOAD_FILENAME);
  12. } catch {
  13. // noop
  14. }
  15. });
  16. afterAll(async () => {
  17. try {
  18. await unlink(PROCESS_OUTPUT_FILENAME);
  19. } catch {
  20. // noop
  21. }
  22. });
  23. describe('downloader', () => {
  24. it('downloads the source data', async () => {
  25. const readStream = await createDownloader({
  26. type: Kanjidic.SOURCE_ID,
  27. });
  28. return new Promise<void>((resolve, reject) => {
  29. const out = createWriteStream(DOWNLOAD_FILENAME);
  30. readStream.on('error', (err) => {
  31. reject(err);
  32. });
  33. out.on('error', (err) => {
  34. reject(err);
  35. });
  36. out.on('finish', () => {
  37. resolve();
  38. });
  39. readStream.pipe(out);
  40. });
  41. });
  42. });
  43. describe('converter', () => {
  44. it('converts XML to JSON', () => new Promise<void>((resolve, reject) => {
  45. const readStream = createReadStream(DOWNLOAD_FILENAME, { encoding: 'utf-8' });
  46. readStream.on('error', (err) => {
  47. reject(err);
  48. });
  49. const transform = createXmlToJsonLines({
  50. entryTagName: 'character',
  51. });
  52. transform.on('error', (err) => {
  53. reject(err);
  54. });
  55. const out = createWriteStream(PROCESS_OUTPUT_FILENAME);
  56. out.on('error', (err) => {
  57. reject(err);
  58. });
  59. out.on('finish', () => {
  60. resolve();
  61. });
  62. readStream
  63. .pipe(transform)
  64. .pipe(out);
  65. }));
  66. });
  67. });