@algosail/string
v0.1.0
Published
Small collection of FP utilities for working with strings.
Readme
@algosail/string
String comparison, concatenation, and utility functions. All functions are curried. Operations on non-string inputs return safe defaults (false, '', []).
Contents
- equals
- lte / lt / gte / gt
- min / max / clamp
- concat
- empty
- toUpper / toLower / trim
- stripPrefix
- stripSuffix
- words / unwords
- lines / unlines
- splitOn
- splitOnRegex
- joinWith
equals
equals :: String -> String -> BooleanTrue when both values are strings with identical content.
equals('hello')('hello') // => true
equals('hello')('world') // => false
equals('1')(1) // => false — type matterslte / lt / gte / gt
lte :: String -> String -> Boolean
lt :: String -> String -> Boolean
gte :: String -> String -> Boolean
gt :: String -> String -> BooleanLexicographic comparisons (native JS string ordering).
lte('a')('b') // => true
lte('b')('a') // => false
lte('abc')('abd') // => true
lt('a')('b') // => true
lt('b')('b') // => false
gte('b')('a') // => true
gt('b')('a') // => truemin / max / clamp
min :: String -> String -> String
max :: String -> String -> String
clamp :: String -> String -> String -> Stringmin('apple')('banana') // => 'apple'
max('apple')('banana') // => 'banana'
clamp('b')('d')('a') // => 'b' (below lo)
clamp('b')('d')('c') // => 'c' (in range)
clamp('b')('d')('e') // => 'd' (above hi)concat
concat :: String -> String -> StringConcatenates two strings. Returns '' if either argument is not a string.
concat('foo')('bar') // => 'foobar'
concat('hello')(' world') // => 'hello world'empty
empty :: StringThe empty string constant.
empty // => ''toUpper / toLower / trim
toUpper :: String -> String
toLower :: String -> String
trim :: String -> StringtoUpper('hello') // => 'HELLO'
toLower('HELLO') // => 'hello'
trim(' hi ') // => 'hi'
trim('\n text \t') // => 'text'stripPrefix
stripPrefix :: String -> String -> Maybe StringReturns Just the remainder after stripping the prefix, or Nothing if the string does not start with the prefix.
stripPrefix('foo')('foobar') // => just('bar')
stripPrefix('foo')('foo') // => just('')
stripPrefix('foo')('bar') // => nothing()
stripPrefix('')('abc') // => just('abc')stripSuffix
stripSuffix :: String -> String -> Maybe StringReturns Just the string with the suffix removed, or Nothing if it does not end with the suffix.
stripSuffix('bar')('foobar') // => just('foo')
stripSuffix('.js')('index.js') // => just('index')
stripSuffix('.ts')('index.js') // => nothing()words / unwords
words :: String -> Array String
unwords :: Array String -> StringSplit on whitespace / join with a single space. Leading and trailing whitespace is ignored.
words(' foo bar baz ') // => ['foo', 'bar', 'baz']
words('') // => []
words('single') // => ['single']
unwords(['foo', 'bar', 'baz']) // => 'foo bar baz'
unwords([]) // => ''lines / unlines
lines :: String -> Array String
unlines :: Array String -> StringSplit on \n, \r\n, or \r. unlines appends a terminating \n to each line.
lines('a\nb\nc') // => ['a', 'b', 'c']
lines('a\r\nb') // => ['a', 'b']
lines('') // => []
unlines(['a', 'b', 'c']) // => 'a\nb\nc\n'splitOn
splitOn :: String -> String -> Array StringSplits a string on a separator substring.
splitOn(',')('a,b,c') // => ['a', 'b', 'c']
splitOn(', ')('a, b, c') // => ['a', 'b', 'c']
splitOn('::')('a::b::c') // => ['a', 'b', 'c']
splitOn(',')('no-separator') // => ['no-separator']splitOnRegex
splitOnRegex :: RegExp -> String -> Array StringSplits on a regex pattern. The regex must have the g flag.
splitOnRegex(/\s+/g)(' foo bar baz ') // => [' ', 'foo', ' ', 'bar', ' ', 'baz', ' ']
splitOnRegex(/,\s*/g)('a, b,c , d') // => ['a', 'b', 'c', 'd']joinWith
joinWith :: String -> Array String -> StringJoins an array of strings with the given separator.
joinWith('-')(['a', 'b', 'c']) // => 'a-b-c'
joinWith(', ')(['foo', 'bar']) // => 'foo, bar'
joinWith('')(['h', 'e', 'l', 'l', 'o']) // => 'hello'