elm-community/string-extra/1.0.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.

replaceSlice "Sue" 4 6 "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 reapeated whitespace internally to a single whitespace char.

clean " The   quick brown   fox    " == "The quick brown fox"

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, seperating 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 seperator.

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 seperating 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 seperating 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" == "\"barfoobar\""
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 reapeated 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 and appends three dots only if the tructated string + the 3 dots have exactly the desired lenght.

The resulting string will have at most the specified lenght

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

Truncates the string at the specified length and appends three dots only if the tructated string + the 3 dots have exactly the desired lenght.

In constrast to ellipsis, this method will produced unfinished words, instead, it will find the closest complete word and apply the ellipsis from there.

Additionally, it will remove any trailing whitespace and punctuation characters at the end of the truncated string.

The resulting stirng can in some cases exceed the specifed lenght, by at most three characters.

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

Truncates the string at the specified lenght and adds the append string only if the combined lenght of the truncated string and the append string have exactly the desired lenght.

The resulting string will have at most the specified lenght

ellipsisWith 5 " .." "Hello World" == "Hello .."
ellipsisWith 10 " .."  "Hello World" == "Hello W..."
ellipsisWith 10 " .." "Hello" == "Hello"
ellipsisWith 8 " .." "Hello World" == "Hello World"
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!')"

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"