elm-community/string-extra/1.3.2

NOTE: this package is not compatible with Elm 0.19.1

String.Extra

Additional functions for working with Strings

Change words casing

toSentenceCase : String -> String

Make a string's first character uppercase

toSentenceCase "this is a phrase" == "This is a phrase"
toSentenceCase "hello, world" == "Hello, world"
toTitleCase : String -> String

Uppercase the first character of each word in a string

toTitleCase "this is a phrase" == "This Is A Phrase"
toTitleCase "hello, world" == "Hello, World"
decapitalize : String -> String

Make a string's first character lowercase.

decapitalize "This is a phrase" == "this is a phrase"
decapitalize "Hello, World" == "hello, World"

Inflector functions

Functions borrowed from the Rails Inflector class

camelize : String -> String

Converts underscored or dasherized string to a camelized one.

camelize "-moz-transform" == "MozTransform"
classify : String -> String

Converts string to camelized string starting with an uppercase. All non-word characters will be stripped out of the original string.

classify "some_class_name" == "SomeClassName"
classify "myLittleCamel.class.name" == "MyLittleCamelClassName"
underscored : String -> String

Returns a string joined by underscores after separating it by its uppercase characters. Any sequence of spaces or dashes will also be converted to a single underscore. The final string will be lowercased

underscore "SomeClassName" == "some_class_name"
underscore "some-class-name" == "some_class_name"
underscore "SomeClass name" == "some_class_name
dasherize : String -> String

Returns a string joined by dashes after separating it by its uppercase characters. Any sequence of spaces or underscored will also be converted to a single dash. The final string will be lowercased

dasherize "SomeClassName" == "-some-class-name"
dasherize "some_class_name" = "some-class-name"
dasherize "someClass name" = "some-class-name"
humanize : String -> String

Converts an underscored, camelized, or dasherized string into one that can be read by humans. Also removes beginning and ending whitespace, and removes the postfix '_id'. The first character will be capitalized

humanize "this_is_great" == "This is great"
humanize "ThisIsGreat" = "This is great"
humanize "this-is-great" = "This is great"
humanize "author_id" = "Author"

Replace and Splice

replace : String -> String -> String -> String

Replace all occurrences of the search string with the substitution string.

replace "Mary" "Sue" "Hello, Mary" == "Hello, Sue"
replaceSlice : String -> Int -> Int -> String -> String

Replace text within a portion of a string given a substitution string, a start index and an end index. The substitution includes the character at the start index but not the one at the end index.

replaceSlice "Sue" 4 7 "Hi, Bob" == "Hi, Sue"
replaceSlice "elephants" 0  6 "snakes on a plane!" == "elephants on a plane!"
replaceSlice "under" 7  9 "snakes on a plane!" == "snakes under a plane!"
insertAt : String -> Int -> String -> String

Inserts a substring at the specified index.

insertAt "world" 6 "Hello " == "Hello world"
clean : String -> String

Trims the whitespace of both sides of the string and compresses repeated whitespace internally to a single whitespace char.

clean " The   quick brown   fox    " == "The quick brown fox"
nonEmpty : String -> Maybe String

Converts a string to a Nothing when empty.

nonEmpty "" == Nothing
nonEmpty "Hello world" == Just "Hello world"

Splitting

break : Int -> String -> List String

Breaks a string into a list of strings of maximum the provided size.

break 10 "The quick brown fox" == ["The quick ", "brown fox"]
break 2 "" == [""]
softBreak : Int -> String -> List String

Breaks a string into a list of strings of maximum the provided size, without cutting words at the edge.

softBreak 6 "The quick brown fox" == ["The quick", " brown", " fox"]

Wrapping

wrap : Int -> String -> String

Chops a given string into parts of a given width, separating them using a new line.

wrap 7 "My very long text" === "My very\nlong te\nxt"
wrap 100 "Too short" === "Too short"
wrapWith : Int -> String -> String -> String

Separates a string into parts of a given width, using a given separator.

Look at wrap if you just want to wrap using newlines.

wrapWith 7 "\n" "My very long text" === "My very\nlong text"
wrapWith 100 "\n" "Too short" === "Too short"
softWrap : Int -> String -> String

Chops a given string into parts of a given width without breaking works apart, and then separating them using a new line.

softWrap 7 "My very long text" === "My very\nlong text"
softWrap 3 "Hello World" === "Hello \nWorld"
softWrap 100 "Too short" === "Too short"
softWrapWith : Int -> String -> String -> String

Chops a given string into parts of a given width without breaking works apart, and then separating them using the given separator.

softWrapWith 7 "..." "My very long text" === "My very...long text"
softWrapWith 3 "\n" "Hello World" === "Hello \nWorld"
softWrapWith 100 "\t" "Too short" === "Too short"
quote : String -> String

Adds quotes to a string.

quote "foo" == "\"foo\""
surround : String -> String -> String

Surrounds a string with another string.

surround "bar" "foo" == "barfoobar"

Checks

isBlank : String -> Bool

Tests if a string is empty or only contains whitespace

isBlank "" == True
isBlank "\n" == True
isBlank "  " == True
isBlank " a" == False
countOccurrences : String -> String -> Int

Returns the number of occurrences of a substring in another string

countOccurrences "Hello" "Hello World" == 1
countOccurrences "o" "Hello World" == 2

Formatting

clean : String -> String

Trims the whitespace of both sides of the string and compresses repeated whitespace internally to a single whitespace char.

clean " The   quick brown   fox    " == "The quick brown fox"
unquote : String -> String

Removes quotes that surround a string.

unquote "\"foo\"" == "foo"
unquote "\"foo\"bar\""
unsurround : String -> String -> String

Removes surrounding strings from another string.

unsurround "foo" "foobarfoo" == "bar"
unindent : String -> String

Removes the least sequence of leading spaces or tabs on each line of the string, so that at least one of the lines will not have any leading spaces nor tabs and the rest of the lines will have the same amount of indentation removed.

unindent "  Hello\n    World " == "Hello\n  World"
unindent "\t\tHello\n\t\t\t\tWorld" == "Hello\n\t\tWorld"
ellipsis : Int -> String -> String

Truncates the string at the specified length if the string is longer than the specified length, and replaces the end of the truncated string with "...", such that the resulting string is of the specified length.

The resulting string will have at most the specified length.

ellipsis 5 "Hello World" == "He..."
ellipsis 10 "Hello World" == "Hello W..."
ellipsis 10 "Hello" == "Hello"
ellipsis 8 "Hello World" == "Hello..."
softEllipsis : Int -> String -> String

Truncates the string at the last complete word less than or equal to the specified length and appends "...". When the specified length is less than the length of the first word, the ellipsis is appended to the first word. When the specified length is greater than or equal to the length of the string, an identical string is returned.

In contrast to ellipsis, this function will not produce incomplete words, and the resulting string can exceed the specified length. In addition, it removes trailing whitespace and punctuation characters at the end of the truncated string.

softEllipsis 1 "Hello, World" == "Hello..."
softEllipsis 5 "Hello, World" == "Hello..."
softEllipsis 6 "Hello, World" == "Hello..."
softEllipsis 15 "Hello, cruel world" == "Hello, cruel..."
softEllipsis 10 "Hello" == "Hello"
ellipsisWith : Int -> String -> String -> String

Truncates the second string at the specified length if the string is longer than the specified length, and replaces the end of the truncated string with the first string, such that the resulting string is of the specified length.

The resulting string will have at most the specified length.

ellipsisWith 5 " .." "Hello World" == "He .."
ellipsisWith 10 " .."  "Hello World" == "Hello W .."
ellipsisWith 10 " .." "Hello" == "Hello"
ellipsisWith 8 " .." "Hello World" == "Hello .."
stripTags : String -> String

Removes all HTML tags from the string, preserving the text inside them.

stripTags "a <a href=\"#\">link</a>" == "a link"
stripTags "<script>alert('hello world!')</script> == "alert('hello world!')"
pluralize : String -> String -> number -> String

Given a number, a singular string, and a plural string, returns the number followed by a space, followed by either the singular string if the number was 1, or the plural string otherwise. pluralize "elf" "elves" 2 == "2 elves" pluralize "elf" "elves" 1 == "1 elf" pluralize "elf" "elves" 0 == "0 elves"

Converting Lists

toSentence : List String -> String

Converts a list of strings into a human formatted readable list

toSentence [] == ""
toSentence ["lions"] == "lions"
toSentence ["lions", "tigers"] == "lions and tigers"
toSentence ["lions", "tigers", "bears"] == "lions, tigers and bears"
toSentenceOxford : List String -> String

Converts a list of strings into a human formatted readable list using an oxford comma

toSentenceOxford [] == ""
toSentenceOxford ["lions"] == "lions"
toSentenceOxford ["lions", "tigers"] == "lions and tigers"
toSentenceOxford ["lions", "tigers", "bears"] == "lions, tigers, and bears"

Finding

rightOf : String -> String -> String

Searches a string from left to right for a pattern and returns a substring consisting of the characters in the string that are to the right of the pattern.

rightOf "_" "This_is_a_test_string" == "is_a_test_string"
leftOf : String -> String -> String

Searches a string from left to right for a pattern and returns a substring consisting of the characters in the string that are to the left of the pattern.

leftOf "_" "This_is_a_test_string" == "This"
rightOfBack : String -> String -> String

Searches a string from right to left for a pattern and returns a substring consisting of the characters in the string that are to the right of the pattern.

rightOfBack "_" "This_is_a_test_string" == "string"
leftOfBack : String -> String -> String

Searches a string from right to left for a pattern and returns a substring consisting of the characters in the string that are to the right of the pattern.

leftOfBack "_" "This_is_a_test_string" == "This_is_a_test"

Converting Numbers

fromInt : Int -> String

Turn an Int into a String.

This works the same way as Basics.toString except its type is restricted, so if you accidentally pass it something other than an Int, you get an error.

fromFloat : Float -> String

Turns a Float into a String.

This works the same way as Basics.toString except its type is restricted, so if you accidentally pass it something other than a Float, you get an error.

Converting UTF-32

toCodePoints : String -> List Int

Converts a String into a list of UTF-32 code points.

toCodePoints "abc" == [ 97, 98, 99 ]
toCodePoints "©§π" == [ 169, 167, 960 ]

If every character in the string can be represented by a single UTF-16 code unit, toCodePoints is equivalent to

String.toList >> List.map Char.toCode

However, for characters that do not fit into a single UTF-16 code unit and have to be represented by a surrogate pair, the above will return each code unit in the surrogate pair separately:

-- 💩 is U+1F4A9 PILE OF POO
List.map Char.toCode (String.toList "💩!") == [ 55357, 56489, 33 ]

toCodePoints detects and combines surrogate pairs of code units to return a list of valid UTF-32 code points:

toCodePoints "💩!" == [ 128169, 33 ]

Note that this still does not necessarily correspond to logical/visual characters, since it is possible for things like accented characters to be represented as two separate UTF-32 code points (a base character and a combining accent).

fromCodePoints : List Int -> String

Converts a list of UTF-32 code points into a String. Inverse of toCodePoints.

fromCodePoints [ 97, 98, 99 ] == "abc"
fromCodePoints [ 169, 167, 960 ] == "©§π"

If every code point is a valid UTF-16 code unit, fromCodePoints is equivalent to

List.map Char.fromCode >> String.fromList

However, fromCodePoints additionally splits code points that do not fit in a single UTF-16 code unit into surrogate pairs, so that even code points outside the Basic Multilingual Plane (BMP) can be included in the resulting string:

-- Code point 128169 is 💩, U+1F4A9 PILE OF POO
fromCodePoints [ 128169, 33 ] == "💩!"