MartinSStewart/elm-nonempty-string/1.0.1

NOTE: the latest version is 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

= Nonempty Char String

A string with at least one character.

length : Nonempty -> Int

Get the length of a nonempty string.

Reverse a string.

Building

append : String -> Nonempty -> Nonempty

Append a string onto the beginning of a nonempty string.

append_ : Nonempty -> String -> Nonempty

Append a string onto the end of a nonempty string.

concat : Nonempty Nonempty -> Nonempty

Concatenate many nonempty strings into one.

fromString : String -> Maybe Nonempty

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

toString : Nonempty -> String

Convert to an ordinary string.

Get Substrings

slice : Int -> Int -> Nonempty -> String

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

text = Nonempty '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 -> Nonempty -> String

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

dropLeft : Int -> Nonempty -> String

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

dropRight : Int -> Nonempty -> String

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

tail : Nonempty -> String

Return all the character after the first one.

Check for Substrings

contains : String -> Nonempty -> Bool

See if the second string contains the first one.

startsWith : String -> Nonempty -> Bool

See if the second string starts with the first one.

endsWith : String -> Nonempty -> Bool

See if the second string ends with the first one.

indexes : String -> Nonempty -> List Int

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

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


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

Alias for indexes.

Int Conversions

toInt : Nonempty -> Maybe Int

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

fromInt : Int -> Nonempty

Convert an Int to a Nonempty.

Float Conversions

toFloat : Nonempty -> Maybe Float

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

fromFloat : Float -> Nonempty

Convert a Float to a Nonempty.

Char Conversions

fromChar : Char -> Nonempty

Create a string from a given character.

fromChar 'a' == Nonempty 'a' ""
cons : Char -> Nonempty -> Nonempty

Add a character to the beginning of a nonempty string.

uncons : Nonempty -> ( 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 : Nonempty -> Nonempty Char

Convert a nonempty string to a nonempty list of characters.

fromNonemptyList : Nonempty Char -> Nonempty

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 -> Nonempty -> Nonempty

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

padLeft : Int -> Char -> Nonempty -> Nonempty

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

padRight : Int -> Char -> Nonempty -> Nonempty

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

trim : Nonempty -> String

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

trimLeft : Nonempty -> String

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

trimRight : Nonempty -> String

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

Higher-Order Functions

map : (Char -> Char) -> Nonempty -> Nonempty

Transform every character in a nonempty string

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

Keep only the characters that pass the test.

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

Reduce a nonempty string from the left.

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

Reduce a nonempty string from the right.

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

Determine whether any characters pass the test.

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

Determine whether all characters pass the test.