The minimalistic document archiving platform.
refactor(extractors): replace String() with stringify utility (#1275)
Corentin Thomassetb800603f653d
diff --git a/packages/lecture/src/extractors/doc.extractor.ts b/packages/lecture/src/extractors/doc.extractor.tsindex 2f992175..8d4e3775 100644--- a/packages/lecture/src/extractors/doc.extractor.ts+++ b/packages/lecture/src/extractors/doc.extractor.ts@@ -1,6 +1,7 @@import { XMLParser } from 'fast-xml-parser';import { defineTextExtractor } from '../extractors.models';import { getFileContentFromArchive } from '../utils/archive';+import { stringify } from '../utils/string';export const docExtractorDefinition = defineTextExtractor({name: 'doc',@@ -64,7 +65,7 @@ function extractTextFromDocx(parsed: unknown): string {// Handle text nodesconst nodeObj = node as Record<string, unknown>;if (nodeObj['#text']) {- const trimmed = String(nodeObj['#text']).trim();+ const trimmed = stringify(nodeObj['#text']).trim();if (trimmed) {texts.push(trimmed);}diff --git a/packages/lecture/src/extractors/odp.extractor.ts b/packages/lecture/src/extractors/odp.extractor.tsindex 71d3d30b..f29cd25c 100644--- a/packages/lecture/src/extractors/odp.extractor.ts+++ b/packages/lecture/src/extractors/odp.extractor.ts@@ -1,6 +1,7 @@import { XMLParser } from 'fast-xml-parser';import { defineTextExtractor } from '../extractors.models';import { getFileContentFromArchive } from '../utils/archive';+import { stringify } from '../utils/string';export const odpExtractorDefinition = defineTextExtractor({name: 'odp',@@ -53,7 +54,7 @@ function extractTextFromOdp(parsed: unknown): string {// Handle text nodesif (nodeObj['#text']) {- const trimmed = String(nodeObj['#text']).trim();+ const trimmed = stringify(nodeObj['#text']).trim();if (trimmed) {texts.push(trimmed);}diff --git a/packages/lecture/src/extractors/ods.extractor.ts b/packages/lecture/src/extractors/ods.extractor.tsindex fec438ce..3b895b58 100644--- a/packages/lecture/src/extractors/ods.extractor.ts+++ b/packages/lecture/src/extractors/ods.extractor.ts@@ -1,6 +1,7 @@import { XMLParser } from 'fast-xml-parser';import { defineTextExtractor } from '../extractors.models';import { getFileContentFromArchive } from '../utils/archive';+import { stringify } from '../utils/string';type XmlNode = Record<string, unknown>;@@ -28,7 +29,7 @@ function extractText(node: unknown): string {return '';}if (typeof node !== 'object') {- return String(node).trim();+ return stringify(node).trim();}if (Array.isArray(node)) {return node.map(extractText).filter(Boolean).join(' ');@@ -36,7 +37,7 @@ function extractText(node: unknown): string {const obj = node as XmlNode;const parts: string[] = [];if (obj['#text'] != null) {- const t = String(obj['#text']).trim();+ const t = stringify(obj['#text']).trim();if (t) {parts.push(t);}diff --git a/packages/lecture/src/extractors/odt.extractor.ts b/packages/lecture/src/extractors/odt.extractor.tsindex 657f8ee2..e7ebed2d 100644--- a/packages/lecture/src/extractors/odt.extractor.ts+++ b/packages/lecture/src/extractors/odt.extractor.ts@@ -1,6 +1,7 @@import { XMLParser } from 'fast-xml-parser';import { defineTextExtractor } from '../extractors.models';import { getFileContentFromArchive } from '../utils/archive';+import { stringify } from '../utils/string';export const odtExtractorDefinition = defineTextExtractor({name: 'odt',@@ -57,7 +58,7 @@ function extractTextFromOdt(parsed: unknown): string {// Handle text nodesconst nodeObj = node as Record<string, unknown>;if (nodeObj['#text']) {- const trimmed = String(nodeObj['#text']).trim();+ const trimmed = stringify(nodeObj['#text']).trim();if (trimmed) {texts.push(trimmed);}diff --git a/packages/lecture/src/extractors/pptx.extractor.ts b/packages/lecture/src/extractors/pptx.extractor.tsindex 08ba1d99..6504c3ba 100644--- a/packages/lecture/src/extractors/pptx.extractor.ts+++ b/packages/lecture/src/extractors/pptx.extractor.ts@@ -1,6 +1,7 @@import { XMLParser } from 'fast-xml-parser';import JSZip from 'jszip';import { defineTextExtractor } from '../extractors.models';+import { stringify } from '../utils/string';export const pptxExtractorDefinition = defineTextExtractor({name: 'pptx',@@ -75,7 +76,7 @@ function extractTextFromSlide(parsed: unknown): string {// Handle text nodesif (nodeObj['#text']) {- const trimmed = String(nodeObj['#text']).trim();+ const trimmed = stringify(nodeObj['#text']).trim();if (trimmed) {texts.push(trimmed);}diff --git a/packages/lecture/src/extractors/xlsx.extractor.ts b/packages/lecture/src/extractors/xlsx.extractor.tsindex 75fbd8df..2a00435e 100644--- a/packages/lecture/src/extractors/xlsx.extractor.ts+++ b/packages/lecture/src/extractors/xlsx.extractor.ts@@ -1,6 +1,7 @@import { XMLParser } from 'fast-xml-parser';import JSZip from 'jszip';import { defineTextExtractor } from '../extractors.models';+import { stringify } from '../utils/string';type XmlNode = Record<string, unknown>;@@ -28,10 +29,10 @@ function extractTText(t: unknown): string {return '';}if (typeof t !== 'object') {- return String(t);+ return stringify(t);}const obj = t as XmlNode;- return obj['#text'] != null ? String(obj['#text']) : '';+ return obj['#text'] != null ? stringify(obj['#text']) : '';}export const xlsxExtractorDefinition = defineTextExtractor({@@ -145,9 +146,9 @@ export const xlsxExtractorDefinition = defineTextExtractor({text = extractSharedString(inlineStr);} else if (cellType === 'str' && value != null) {// formula result string- text = String(value);+ text = stringify(value);} else if (value != null) {- text = String(value);+ text = stringify(value);}if (text.trim()) {diff --git a/packages/lecture/src/utils/string.test.ts b/packages/lecture/src/utils/string.test.tsnew file mode 100644index 00000000..18db9e3d--- /dev/null+++ b/packages/lecture/src/utils/string.test.ts@@ -0,0 +1,50 @@+import { describe, expect, test } from 'vitest';+import { stringify } from './string';++describe('string', () => {+ describe('stringify', () => {+ test('returns strings unchanged', () => {+ expect(stringify('hello')).toBe('hello');+ expect(stringify('')).toBe('');+ });++ test('coerces numbers, including special values', () => {+ expect(stringify(42)).toBe('42');+ expect(stringify(-3.14)).toBe('-3.14');+ expect(stringify(0)).toBe('0');+ expect(stringify(Number.NaN)).toBe('NaN');+ expect(stringify(Number.POSITIVE_INFINITY)).toBe('Infinity');+ });++ test('coerces booleans', () => {+ expect(stringify(true)).toBe('true');+ expect(stringify(false)).toBe('false');+ });++ test('coerces bigints', () => {+ expect(stringify(9007199254740993n)).toBe('9007199254740993');+ });++ test('coerces null and undefined to their textual form', () => {+ expect(stringify(null)).toBe('null');+ expect(stringify(undefined)).toBe('undefined');+ });++ test('coerces symbols', () => {+ expect(stringify(Symbol('tag'))).toBe('Symbol(tag)');+ });++ test('serializes plain objects and arrays as JSON', () => {+ expect(stringify({ a: 1, b: 'two' })).toBe('{"a":1,"b":"two"}');+ expect(stringify([1, 'two', true])).toBe('[1,"two",true]');+ expect(stringify({ nested: { value: [1, 2] } })).toBe('{"nested":{"value":[1,2]}}');+ });++ test('falls back to a safe tag when JSON serialization throws', () => {+ const circular: Record<string, unknown> = {};+ circular.self = circular;++ expect(stringify(circular)).toBe('[object Object]');+ });+ });+});diff --git a/packages/lecture/src/utils/string.ts b/packages/lecture/src/utils/string.tsnew file mode 100644index 00000000..8e67baaf--- /dev/null+++ b/packages/lecture/src/utils/string.ts@@ -0,0 +1,17 @@+export function stringify(value: unknown): string {+ if (typeof value === 'string') {+ return value;+ }++ if (typeof value === 'object' && value !== null) {+ try {+ return JSON.stringify(value);+ } catch {+ // JSON.stringify throws on circular references and bigint values, fall back to a safe tag.+ return Object.prototype.toString.call(value);+ }+ }++ // number, boolean, bigint, symbol, function, null and undefined are all safe to coerce with String.+ return String(value);+}