Refactor text utilities and expand their usage
This commit refactors existing text utility functions into the application layer for broad reuse and integrates them across the codebase. Initially, these utilities were confined to test code, which limited their application. Changes: - Move text utilities to the application layer. - Centralize text utilities into dedicated files for better maintainability. - Improve robustness of utility functions with added type checks. - Replace duplicated logic with centralized utility functions throughout the codebase. - Expand unit tests to cover refactored code parts.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { indentText } from '@/application/Common/Text/IndentText';
|
||||
import { formatAssertionMessage } from '@tests/shared/FormatAssertionMessage';
|
||||
import { indentText } from '@tests/shared/Text';
|
||||
|
||||
/**
|
||||
* Asserts that an array deeply includes a specified item by comparing JSON-serialized versions.
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
import { isString } from '@/TypeHelpers';
|
||||
|
||||
export function indentText(
|
||||
text: string,
|
||||
indentLevel = 1,
|
||||
): string {
|
||||
validateText(text);
|
||||
const indentation = '\t'.repeat(indentLevel);
|
||||
return splitTextIntoLines(text)
|
||||
.map((line) => (line ? `${indentation}${line}` : line))
|
||||
.join('\n');
|
||||
}
|
||||
|
||||
export function splitTextIntoLines(text: string): string[] {
|
||||
validateText(text);
|
||||
return text
|
||||
.split(/[\r\n]+/);
|
||||
}
|
||||
|
||||
export function filterEmpty(texts: readonly (string | undefined | null)[]): string[] {
|
||||
return texts
|
||||
.filter((title): title is string => Boolean(title));
|
||||
}
|
||||
|
||||
function validateText(text: string): void {
|
||||
if (!isString(text)) {
|
||||
throw new Error(`text is not a string. It is: ${typeof text}\n${text}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user