Text Parser
String Utilities
Overview
This library provides a comprehensive set of functions for string manipulation. It includes methods to transform, sanitize, truncate, and format strings, making it versatile for various text processing tasks in web and application development.
Functions
transform
Transforms a string based on the specified transformation type.
Parameters:
transform: "uppercase" | "capitalize-first" | "capitalize" | "lowercase"
Defines the type of transformation.words: string | undefined
The input string to be transformed.
Returns: A transformed string.
Example:
transform.uppercase("hello-world");
// "HELLO WORLD"
transform.capitalize("hello-world");
// "Hello World"
truncate
Truncates a string to a specified maximum length, preserving word boundaries and adding an ellipsis if truncated.
Parameters:
word: string
The string to truncate.maxWord: number (default: 30)
The maximum length of the string.
Returns: A truncated string.
Example:
truncate("This is a long sentence that needs truncating.", 20);
// "This is a long..."
lowerCasePunctuation
Converts a string to lowercase, removes punctuation, and replaces spaces with hyphens.
Parameters:
str: string
The input string.
Returns: A sanitized and formatted string.
Example:
lowerCasePunctuation("Hello, World!");
// "hello-world"
sanitizedWord
Sanitizes a string by converting to lowercase, removing special characters, and replacing spaces with hyphens.
Parameters:
words: string | undefined
The input string to sanitize.
Returns: A sanitized string.
Example:
sanitizedWord("Hello, World!");
// "hello-world"
desanitizeWord
Converts a sanitized string back to a readable format, capitalizing words while preserving conjunctions.
Parameters:
-
words: string | undefined
The input string to desanitize. -
separator: string | RegExp (default: " " )
The word separator in the input string.
Returns: A desanitized string.
Example:
desanitizeWord("hello-world");
// "Hello World"
compareWords
Compares two strings for equality, ignoring case and special characters.
Parameters:
word1: string | undefined | null
word2: string | undefined | null
Returns: boolean - Whether the strings are equal.
Example:
compareWords("Hello!", "hello");
// true
getFirstString
Extracts the first word from a string, split by spaces, hyphens, or tildes.
Parameters:
name: string
The input string.
Returns: The first word in the string.
Example:
getFirstString("John-Doe");
// "John"
camelToKebab
Converts a camelCase string to kebab-case or snake_case.
Parameters:
-
words: string
The input camelCase string. -
kebab: "underscores" | "hyphens" (default: "hyphens")
The desired output style.
Returns: A kebab-case or snake_case string.
Example:
camelToKebab("helloWorld");
// "hello-world"
camelToKebab("helloWorld", "underscores");
// "hello_world"
kebabToCamel
Converts a kebab-case string to camelCase.
Parameters:
words: string
The input kebab-case string.
Returns: A camelCase string.
Example:
kebabToCamel("hello-world");
// "helloWorld"
toPascal
Converts a kebab-case string to PascalCase.
Parameters:
words: string
The input kebab-case string.
Returns: A PascalCase string.
Example:
toPascal("hello-world");
// "HelloWorld"
formatedProgress
Formats a numeric input as a string. Returns the input unchanged if it contains non-numeric characters.
Parameters:
input: string | undefined
The input string.
Returns: A formatted string or null if input is undefined.
Example:
formatedProgress("42");
// "42"
splitWordsToArray
Splits a string into an array of objects, each containing a word as text.
Parameters:
words: string
The input string.
Returns: An array of objects with the structure { text: string }
.
Example:
splitWordsToArray("hello world");
// [{ text: "hello" }, { text: "world" }]
Helper Functions
- toUpper Converts the first letter of a string to uppercase.
- toLower Converts the first letter of a string to lowercase.
- removePunctuation Removes punctuation from a string.
- combineConsecutiveHyphens Replaces multiple consecutive hyphens with a single hyphen.
HTML Character Entities
Provides a list of mappings for HTML character entities.
Example:
htmlCharacterEntities.find(entity => entity.char === "<");
// { char: "<", entity: "<" }