MartinSStewart/elm-nonempty-string/2.0.0

String.Nonempty

A string that cannot be empty. The head and tail can be accessed without Maybes. Most other string functions are available.

Strings

= NonemptyString Char String

A string with at least one character.

Get the length of a nonempty string.

Reverse a string.

Building

Append a string onto the beginning of a nonempty string.

Append a string onto the end of a nonempty string.

Concatenate many nonempty strings into one.

fromString : String -> Maybe NonemptyString

Create a nonempty string from an ordinary string, failing on the empty string.

Convert to an ordinary string.

Get Substrings

slice : Int -> Int -> NonemptyString -> String

Take a substring given a start and end index. Negative indexes are taken starting from the end of the list.

text = NonemptyString 's' "nakes on a plane!"


slice  7  9 text == "on"
slice  0  6 text == "snakes"
slice  0 -7 text == "snakes on a"
slice -6 -1 text == "plane"
left : Int -> NonemptyString -> String

Take n characters from the left side of a nonempty string.

dropLeft : Int -> NonemptyString -> String

Drop n characters from the left side of a nonempty string.

dropRight : Int -> NonemptyString -> String

Drop n characters from the right side of a nonempty string.

tail : NonemptyString -> String

Return all the characters after the first one.

Check for Substrings

contains : String -> NonemptyString -> Bool

See if the second string contains the first one.

startsWith : String -> NonemptyString -> Bool

See if the second string starts with the first one.

endsWith : String -> NonemptyString -> Bool

See if the second string ends with the first one.

indexes : String -> NonemptyString -> List Int

Get all of the indexes for a substring in another string.

indexes "i" (NonemptyString 'M' "ississippi") == [ 1, 4, 7, 10 ]


indexes "ss" (NonemptyString 'M' "ississippi") == [ 2, 5 ]
indices : String -> NonemptyString -> List Int

Alias for indexes.

Int Conversions

toInt : NonemptyString -> Maybe Int

Try to convert a string into an int, failing on improperly formatted nonempty strings.

Convert an Int to a NonemptyString.

Float Conversions

toFloat : NonemptyString -> Maybe Float

Try to convert a string into a float, failing on improperly formatted nonempty strings.

Convert a Float to a NonemptyString.

Char Conversions

Create a string from a given character.

fromChar 'a' == NonemptyString 'a' ""

Add a character to the beginning of a nonempty string.

uncons : NonemptyString -> ( Char, String )

Split a nonempty string into its head and tail. This lets you pattern match on strings exactly as you would with lists.

List Conversions

toNonemptyList : NonemptyString -> Nonempty Char

Convert a nonempty string to a nonempty list of characters.

fromNonemptyList : Nonempty Char -> NonemptyString

Convert a nonempty list of characters into a nonempty string.

Formatting

Cosmetic operations such as padding with extra characters or trimming whitespace.

Convert a string to all upper case. Useful for case-insensitive comparisons and VIRTUAL YELLING.

Convert a string to all lower case. Useful for case-insensitive comparisons.

pad : Int -> Char -> NonemptyString -> NonemptyString

Pad a nonempty string on both sides until it has a given length.

Pad a nonempty string on the left until it has a given length.

Pad a nonempty string on the right until it has a given length.

trim : NonemptyString -> String

Get rid of whitespace on both sides of a nonempty string.

Get rid of whitespace on the left of a nonempty string.

Get rid of whitespace on the right of a nonempty string.

Higher-Order Functions

map : (Char -> Char) -> NonemptyString -> NonemptyString

Transform every character in a nonempty string

filter : (Char -> Bool) -> NonemptyString -> String

Keep only the characters that pass the test.

foldl : (Char -> b -> b) -> b -> NonemptyString -> b

Reduce a nonempty string from the left.

foldr : (Char -> b -> b) -> b -> NonemptyString -> b

Reduce a nonempty string from the right.

any : (Char -> Bool) -> NonemptyString -> Bool

Determine whether any characters pass the test.

all : (Char -> Bool) -> NonemptyString -> Bool

Determine whether all characters pass the test.