@bemoje/string
v3.0.0
Published
String manipulation utilities for casing, wrapping, line processing, and character analysis.
Maintainers
Readme
@mono/string
String manipulation utilities for casing, wrapping, line processing, and character analysis.
Exports
- countFloatDecimals: Counts the number of decimal places in a floating-point number.
- endsWithIncompleteUtfPairSurrogate: Returns true if the string ends with an incomplete UTF-16 surrogate pair. This is useful for determining if a string can be safely concatenated with another string.
- strCountCharOccurances: Counts the number of occurrences of a specific character in a string.
- strCountChars: Counts the number of occurrences of each character in a string and returns a Map where the keys are the characters and the values are their counts.
- strEnsureEndsWith: Ensures that a string ends with a specified substring. If the string already ends with the specified substring, it is returned as is. Otherwise, the substring is appended to the end of the string.
- strEnsureStartsWith: Ensures that a string starts with a specified substring. If the string already starts with the specified substring, it is returned as is. Otherwise, the substring is appended to the end of the string.
- strIsLowerCase: Checks if the given string is in lower case.
- strIsMultiLine: Checks if a string contains multiple lines.
- strIsUpperCase: Checks if the given string is in upper case.
- strMaxTwoConsecutiveEmptyLines: Replaces all occurrences of more than two consecutive empty lines with two empty lines.
- strNoConsecutiveEmptyLines: Removes consecutive empty lines from a given string.
- strNoConsecutiveWhitespace: Removes consecutive whitespace characters in a string and replaces them with a single space.
- strParseBoolean: Parses a string into a boolean.
- strPrefixCamelCased: Prepend a camelCased string. Examples:
- strPrependLines: Prepend each line of a string with a specified string.
- strRemoveDuplicateChars: Removes duplicate characters from a string.
- strRemoveEmptyLines: Removes all empty lines from a given string.
- strRemoveFirstAndLastLine: Removes the first and last line from a given string.
- strRemoveNewLines: Removes all new line characters from a string.
- strRepeat: Repeats the given string
ntimes. - strReplaceAll: Replaces all occurrences of a substring in a string with a specified replacement.
- strSortChars: Sorts the characters in a string in alphabetical order.
- strSplitAndTrim: Splits a string by a specified delimiter and trims each resulting substring. Optionally, it can also remove empty lines.
- strSplitCamelCase: Returns an array of words in the string
- strToCharCodes: Converts a string to an array of character codes.
- strToCharSet: Converts a string to a set of unique characters.
- strToGetterMethodName: Prepend a camelCased string with 'get'.
- strToSetterMethodName: Prepend a camelCased string with 'set'.
- strToSortedCharSet: Converts a string to a sorted set of unique characters.
- strTrimLines: Trims leading and trailing whitespace from each line in a string.
- strTrimLinesLeft: Trims the leading whitespace from each line in a string.
- strTrimLinesRight: Trims trailing whitespace from each line in a string.
- strUnwrap: Removes the specified left and right substrings from the input string.
- strWrapBetween: Wraps a string between two other strings.
- strWrapIn: Wraps a given string with another string.
- strWrapInAngleBrackets: Wraps a string in angle brackets.
- strWrapInBraces: Wraps a given string in braces.
- strWrapInBrackets: Wraps a string in brackets.
- strWrapInDoubleQuotes: Wraps a given string in double quotes.
- strWrapInParenthesis: Wraps a given string in parenthesis.
- strWrapInSingleQuotes: Wraps a given string in single quotes.
- stringLineCount: Count the number of lines in a string.
- unwrapDoubleQuotes: Remove double quote from the beginning and end of a string and trims whitespace at the beginning and end of the string
Usage
Case & Casing
import { strFirstCharToUpperCase, strFirstCharToLowerCase, titleCaseWord, strSplitCamelCase } from '@mono/string'
strFirstCharToUpperCase('hello')
// => 'Hello'
strFirstCharToLowerCase('Hello')
// => 'hello'
titleCaseWord('hello')
// => 'Hello'
strSplitCamelCase('someCamel10Case')
// => ['some', 'Camel10', 'Case']Wrapping & Unwrapping
import { strWrapBetween, strWrapIn, strWrapInBraces, strUnwrap, unwrapDoubleQuotes } from '@mono/string'
strWrapBetween('Hello', '<', '>')
// => '<Hello>'
strWrapIn('hello', '*')
// => '*hello*'
strWrapInBraces('key')
// => '{key}'
strUnwrap('[value]', '[', ']')
// => 'value'
unwrapDoubleQuotes('"hello"')
// => 'hello'Ensure Prefix & Suffix
import { strEnsureStartsWith, strEnsureEndsWith, strPrefixCamelCased } from '@mono/string'
strEnsureEndsWith('52', ' kg')
// => '52 kg'
strEnsureStartsWith('/path', '/')
// => '/path'
strPrefixCamelCased('name', 'get')
// => 'getName'Line Processing
import {
stringLineCount,
strPrependLines,
strTrimLines,
strRemoveEmptyLines,
strNoConsecutiveEmptyLines,
} from '@mono/string'
stringLineCount('a\nb\nc')
// => 3
strPrependLines('line1\nline2', '> ')
// => '> line1\n> line2'
strTrimLines(' hello \n world ')
// => 'hello\nworld'
strRemoveEmptyLines('a\n\nb\n\nc')
// => 'a\nb\nc'
strNoConsecutiveEmptyLines('a\n\n\n\nb')
// => 'a\n\nb'Character Analysis
import { strCountChars, strCountCharOccurances, strToCharSet, strToCharCodes } from '@mono/string'
strCountChars('hello')
// => Map { 'h' => 1, 'e' => 1, 'l' => 2, 'o' => 1 }
strCountCharOccurances('banana', 'a')
// => 3
strToCharSet('hello')
// => Set { 'h', 'e', 'l', 'o' }
strToCharCodes('abc')
// => [97, 98, 99]Search & Replace
import { strReplaceAll, strSplitAndTrim } from '@mono/string'
strReplaceAll('a-b-c', '-', '.')
// => 'a.b.c'
strSplitAndTrim('a , b , c', ',')
// => ['a', 'b', 'c']Inspection
import { strIsLowerCase, strIsUpperCase, strIsMultiLine, strParseBoolean } from '@mono/string'
strIsLowerCase('hello')
// => true
strIsUpperCase('HELLO')
// => true
strIsMultiLine('a\nb')
// => true
strParseBoolean('true')
// => true