@caelus-utils/strfmt
v0.1.0
Published
A lightweight utility library providing a comprehensive set of string formatting and manipulation functions for JavaScript/TypeScript projects.
Readme
String formatting @caelus-utils/strfmt
A lightweight utility library providing a comprehensive set of string formatting and manipulation functions for JavaScript/TypeScript projects. This package simplifies common string operations such as case conversion, splitting, replacing, padding, and more. It aims to be a one-stop solution for your string manipulation needs, making your code cleaner and more efficient.
Installation
- using
npm
npm install @caelus-utils/strfmt- using
yarn
yarn add @caelus-utils/strfmt- using
pnpm
pnpm add @caelus-utils/strfmtFunctions
isEmptyStr(str, config)
Checks if a given string is empty.
str: The string to check.config: An optional configuration object.ignoreWhitespace: Determines whether whitespace characters should be ignored during processing. Defaults totrue.
Returns true if the string is considered empty, false otherwise.
Usage Example
import {isEmptyStr} from "@caelus-utils/strfmt";
isEmptyStr(''); // true
isEmptyStr(' '); // true
isEmptyStr(' ', {ignoreWhitespace: false}); // false
isEmptyStr(' hello '); // falsesplitStr(sentence, onlyLetterNumber)
Splits a given sentence into an array of words based on various delimiters and patterns.
sentence: The input string to be split into words.onlyLetterNumber: Iftrue, filters the resulting array to include only words containing alphanumeric characters (letters and numbers only). Defaults tofalse.
Returns an array of words (string) obtained by splitting the input sentence.
Usage Example
import {splitStr} from "@caelus-utils/strfmt";
splitStr('hello world'); // ['hello', 'world']strReplace(string, payload)
Replaces occurrences of a search string or multiple search strings in the given input string with the specified replacement value(s).
string: The input string to perform replacements on.payload: The replacement payload(s). It can be a single payload object or an array of payload objects. Each payload includes thesearchstring to be replaced and thereplacevalue.search: A string or regular expression (string | RegExp) to specify the search pattern.replace: A string or a function to replace parts of a string.
Returns the resulting string after all replacements have been applied.
Usage Example
import {strReplace} from "@caelus-utils/strfmt";
strReplace('hello world', {search: 'world', replace: 'everyone'}); // 'hello everyone'
strReplace('hello world', [{search: 'hello', replace: 'hi'}, {search: 'world', replace: 'everyone!'}]); // 'hi everyone!'capitalize(str)
Capitalizes the first letter of a given string.
str: The input string to be capitalized.
Returns the modified string with the first letter capitalized.
Usage Example
import {capitalize} from "@caelus-utils/strfmt";
capitalize('hello world'); // Hello worlduncapitalize(str)
Converts the first character of a string to lowercase and returns the modified string.
str: The input string to process.
Returns the input string with the first character converted to lowercase.
Usage Example
import {uncapitalize} from "@caelus-utils/strfmt";
uncapitalize('Hello World'); // hello WorldtitleCase(str)
Converts a given string into title case format, where the first letter of each word is capitalized.
str: The input string to be transformed into title case.
Returns the transformed string in title case.
Usage Example
import {titleCase} from "@caelus-utils/strfmt";
titleCase('hello world'); // Hello WorldcamelCase(str)
Converts a given string to camelCase format.
str: The input string to be transformed into camelCase.
Returns the camelCase formatted string.
Usage Example
import {camelCase} from "@caelus-utils/strfmt";
camelCase('hello world'); // helloWorldconstantCase(str)
Converts a given string to constant case, where all letters are uppercase and words are separated by underscores.
str: The input string to be converted.
Returns the string converted to constant case.
Usage Example
import {constantCase} from "@caelus-utils/strfmt";
constantCase('hello world'); // HELLO_WORLDsnakeCase(str)
Converts a given string into snake_case format.
str: The input string to be converted.
Returns the transformed string in snake_case format.
Usage Example
import {snakeCase} from "@caelus-utils/strfmt";
snakeCase('hello world'); // hello_worldkebabCase(str)
Converts a given string to kebab-case by transforming it to lowercase and separating words with dashes.
str: The input string to be transformed into kebab-case.
Returns the transformed string in kebab-case format.
Usage Example
import {kebabCase} from "@caelus-utils/strfmt";
kebabCase('hello world'); // hello-worldpascalCase(str)
Converts a given string to PascalCase.
str: The input string to be transformed into PascalCase.
Returns the string converted to PascalCase.
Usage Example
import {pascalCase} from "@caelus-utils/strfmt";
pascalCase('hello world'); // HelloWorldstrRmSpace(str)
Removes all whitespace characters from the given string.
str: The input string from which spaces will be removed.
Returns the resulting string with all spaces removed.
Usage Example
import {strRmSpace} from "@caelus-utils/strfmt";
strRmSpace(' hello world '); // helloworldpadStr(str, config)
Pads the given string or number with a specified prefix to a desired length.
str: The string or number to be padded.config: The configuration object for padding.size: The desired length of the final padded string. Defaults to 2.position: The position where the padding should be added, either 'start' or 'end'. Defaults tostart.prefix: The string to be used as the padding character(s). Defaults to an empty string.
Note: if no prefix was provided nothing occur to the str value
Returns the padded string based on the configuration provided.
Usage Example
import {padStr} from "@caelus-utils/strfmt";
padStr('1', {size: 3, prefix: '0'}); // '001'
padStr(1, {size: 3, prefix: 0}); // '001'charCode(input, hexCase)
Converts the characters in the input string to their hexadecimal Unicode representation.
input: The input string to be converted to hexadecimal representation.hexCase: Iftrue, the output hexadecimal values will be in uppercase; otherwise, they will be in lowercase. Defaults tofalse.
Returns the string containing the hexadecimal representation of the input characters.
Usage Example
import {charCode} from "@caelus-utils/strfmt";
charCode('A'); // '41'strConcat(payload, sharedDivider)
Concatenates an iterable of strings, numbers, or booleans with a specified divider.
payload: Can be a singleStrConcatPayloadobject or an array of them. Containsvaluesanddividervalues: An iterable of strings, numbers or booleans to be concatenated.divider: An optional string used to divide between the concatenated values. Defaults to an empty string.
sharedDivider: An optional string used as a divider between concatenated values for all payloads in casepayloadis an array.
Returns the concatenated string with the specified divider between elements or an array of strings when the input is an array of payloads.
Usage Example
import {strConcat} from "@caelus-utils/strfmt";
strConcat({values: ['hello', 'world'], divider: '-'}); // 'hello-world'
// Each value has it's own divider
strConcat([{values: ['hello', 'world'], divider: '-'}, {values: ['hi', 'everyone!'], divider: '-'}]); // ['hello-world', 'hi-everyone!']
// All the values will share the same divider
strConcat([{values: ['hello', 'world']}, {values: ['hi', 'everyone!']}], '-'); // ['hello-world', 'hi-everyone!']Contributing
Contributions are welcome! Please fork the repository and submit a pull request.
