-
-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
8d7a7eb
commit 851917e
Showing
45 changed files
with
563 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { isArray } from '@/TypeHelpers'; | ||
|
||
export type OptionalString = string | undefined | null; | ||
|
||
export function filterEmptyStrings( | ||
texts: readonly OptionalString[], | ||
isArrayType: typeof isArray = isArray, | ||
): string[] { | ||
if (!isArrayType(texts)) { | ||
throw new Error(`Invalid input: Expected an array, but received type ${typeof texts}.`); | ||
} | ||
assertArrayItemsAreStringLike(texts); | ||
return texts | ||
.filter((title): title is string => Boolean(title)); | ||
} | ||
|
||
function assertArrayItemsAreStringLike( | ||
texts: readonly unknown[], | ||
): asserts texts is readonly OptionalString[] { | ||
const invalidItems = texts.filter((item) => !(typeof item === 'string' || item === undefined || item === null)); | ||
if (invalidItems.length > 0) { | ||
const invalidTypes = invalidItems.map((item) => typeof item).join(', '); | ||
throw new Error(`Invalid array items: Expected items as string, undefined, or null. Received invalid types: ${invalidTypes}.`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { isString } from '@/TypeHelpers'; | ||
import { splitTextIntoLines } from './SplitTextIntoLines'; | ||
|
||
export function indentText( | ||
text: string, | ||
indentLevel = 1, | ||
utilities: TextIndentationUtilities = DefaultUtilities, | ||
): string { | ||
if (!utilities.isStringType(text)) { | ||
throw new Error(`Indentation error: The input must be a string. Received type: ${typeof text}.`); | ||
} | ||
if (indentLevel <= 0) { | ||
throw new Error(`Indentation error: The indent level must be a positive integer. Received: ${indentLevel}.`); | ||
} | ||
const indentation = '\t'.repeat(indentLevel); | ||
return utilities.splitIntoLines(text) | ||
.map((line) => (line ? `${indentation}${line}` : line)) | ||
.join('\n'); | ||
} | ||
|
||
interface TextIndentationUtilities { | ||
readonly splitIntoLines: typeof splitTextIntoLines; | ||
readonly isStringType: typeof isString; | ||
} | ||
|
||
const DefaultUtilities: TextIndentationUtilities = { | ||
splitIntoLines: splitTextIntoLines, | ||
isStringType: isString, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { isString } from '@/TypeHelpers'; | ||
|
||
export function splitTextIntoLines( | ||
text: string, | ||
isStringType = isString, | ||
): string[] { | ||
if (!isStringType(text)) { | ||
throw new Error(`Line splitting error: Expected a string but received type '${typeof text}'.`); | ||
} | ||
return text.split(/\r\n|\r|\n/); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
tests/checks/desktop-runtime-errors/check-desktop-runtime-errors/main.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
tests/checks/desktop-runtime-errors/check-desktop-runtime-errors/utils/run-command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.