├── .gitignore ├── LICENSE ├── README.md ├── elm-package.json ├── src └── String │ └── Extra.elm └── tests ├── .gitignore ├── TestRunner.elm ├── Tests.elm └── elm-package.json /.gitignore: -------------------------------------------------------------------------------- 1 | elm-stuff 2 | elm.js 3 | 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, NoRedInk 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of elm-string-extra nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DEPRECATED: use [elm-community/string-extra](http://package.elm-lang.org/packages/elm-community/string-extra/latest) instead 2 | 3 | 4 | # Convenience functions for working with Strings in Elm 5 | 6 | These functions supplement Elm's core [String package](http://package.elm-lang.org/packages/elm-lang/core/latest/String). 7 | 8 | --- 9 | [![NoRedInk](https://cloud.githubusercontent.com/assets/1094080/9069346/99522418-3a9d-11e5-8175-1c2bfd7a2ffe.png)][team] 10 | [team]: http://noredink.com/about/team 11 | 12 | -------------------------------------------------------------------------------- /elm-package.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "1.3.1", 3 | "summary": "Convenience functions for working with Strings in Elm.", 4 | "repository": "https://github.com/NoRedInk/elm-string-extra.git", 5 | "license": "BSD-3-Clause", 6 | "source-directories": [ 7 | "src" 8 | ], 9 | "exposed-modules": [ 10 | "String.Extra" 11 | ], 12 | "dependencies": { 13 | "elm-lang/core": "4.0.0 <= v < 5.0.0" 14 | }, 15 | "elm-version": "0.17.0 <= v < 0.18.0" 16 | } -------------------------------------------------------------------------------- /src/String/Extra.elm: -------------------------------------------------------------------------------- 1 | module String.Extra exposing (pluralize, capitalize, toSentence, isWhitespace) 2 | 3 | {-| Convenience functions for working with Strings 4 | 5 | # Formatting 6 | @docs capitalize, pluralize, toSentence 7 | 8 | # Whitespace 9 | @docs isWhitespace 10 | 11 | -} 12 | 13 | import Regex exposing (Regex) 14 | import String 15 | import Char 16 | 17 | 18 | {-| Capitalize or uncapitalize the given string. 19 | 20 | capitalize True "foo" 21 | -- "Foo" 22 | 23 | capitalize False "BAR" 24 | -- "bAR" 25 | -} 26 | capitalize : Bool -> String -> String 27 | capitalize shouldCapitalize str = 28 | case String.uncons str of 29 | Nothing -> 30 | str 31 | 32 | Just ( firstLetter, rest ) -> 33 | let 34 | newFirstLetter = 35 | if shouldCapitalize then 36 | Char.toUpper firstLetter 37 | else 38 | Char.toLower firstLetter 39 | in 40 | String.cons newFirstLetter rest 41 | 42 | 43 | {-| Given a number, a singular string, and a plural string, returns the number 44 | followed by a space, followed by either the singular string if the number was 1, 45 | or the plural string otherwise. 46 | 47 | pluralize "elf" "elves" 2 == "2 elves" 48 | pluralize "elf" "elves" 1 == "1 elf" 49 | pluralize "elf" "elves" 0 == "0 elves" 50 | 51 | -} 52 | pluralize : String -> String -> number -> String 53 | pluralize singular plural count = 54 | if count == 1 then 55 | "1 " ++ singular 56 | else 57 | (toString count) ++ " " ++ plural 58 | 59 | 60 | {-| Converts a list of strings into a human formatted readable list 61 | 62 | 63 | toSentence [] ---> "" 64 | toSentence ["lions"] ---> "lions" 65 | toSentence ["lions", "tigers"] --> "lions and tigers" 66 | toSentence ["lions", "tigers", "bears"] --> "lions, tigers, and bears" 67 | 68 | notes: 69 | * It *DOES* include an oxford comma 70 | * It *DOES NOT* include a period 71 | * It *DOES NOT* include the phrase "...oh my!" 72 | 73 | -} 74 | toSentence : List String -> String 75 | toSentence list = 76 | case list of 77 | [] -> 78 | "" 79 | 80 | x :: [] -> 81 | x 82 | 83 | x :: y :: [] -> 84 | x ++ " and " ++ y 85 | 86 | x :: y :: more -> 87 | toSentenceHelper (x ++ ", " ++ y) more 88 | 89 | 90 | toSentenceHelper : String -> List String -> String 91 | toSentenceHelper sentence list = 92 | case list of 93 | [] -> 94 | sentence ++ "" 95 | 96 | x :: [] -> 97 | sentence ++ ", and " ++ x 98 | 99 | x :: xs -> 100 | toSentenceHelper (sentence ++ ", " ++ x) xs 101 | 102 | 103 | {-| Returns True iff the given String is 1 or more whitespace characters, 104 | and nothing else. 105 | 106 | (Whitespace is defined as the regular expression `\s` matcher.) 107 | 108 | isWhitespace "" == False 109 | isWhitespace " " == True 110 | isWhitespace " " == True 111 | isWhitespace " x" == False 112 | isWhitespace "x " == False 113 | -} 114 | isWhitespace : String -> Bool 115 | isWhitespace = 116 | Regex.contains isWhitespaceRegex 117 | 118 | 119 | isWhitespaceRegex : Regex 120 | isWhitespaceRegex = 121 | Regex.regex "^\\s+$" 122 | -------------------------------------------------------------------------------- /tests/.gitignore: -------------------------------------------------------------------------------- 1 | elm-stuff -------------------------------------------------------------------------------- /tests/TestRunner.elm: -------------------------------------------------------------------------------- 1 | module Main (..) where 2 | 3 | import ElmTest exposing (..) 4 | import Tests 5 | 6 | main = 7 | elementRunner Tests.all 8 | -------------------------------------------------------------------------------- /tests/Tests.elm: -------------------------------------------------------------------------------- 1 | module Tests (..) where 2 | 3 | import ElmTest exposing (..) 4 | import String.Extra exposing (toSentence) 5 | 6 | all : Test 7 | all = 8 | suite 9 | "toSentence" 10 | [ toSentence [] 11 | |> assertEqual "" 12 | |> test "should be an empty string for empty lists" 13 | , toSentence ["lion"] 14 | |> assertEqual "lion" 15 | |> test "should be just a word for a list length 1" 16 | , toSentence ["lion", "tiger"] 17 | |> assertEqual "lion and tiger" 18 | |> test "should just give 'and' for list length 2" 19 | , toSentence ["lion", "tiger", "bear"] 20 | |> assertEqual "lion, tiger, and bear" 21 | |> test "should make a human readable list for list length 3" 22 | , toSentence ["lion", "tiger", "bear", "dog", "horse"] 23 | |> assertEqual "lion, tiger, bear, dog, and horse" 24 | |> test "should make a human readable list for list length n" 25 | ] 26 | 27 | -------------------------------------------------------------------------------- /tests/elm-package.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "1.0.0", 3 | "summary": "helpful summary of your project, less than 80 characters", 4 | "repository": "https://github.com/user/project.git", 5 | "license": "BSD3", 6 | "source-directories": [ 7 | ".", 8 | "../src" 9 | 10 | ], 11 | "exposed-modules": [], 12 | "dependencies": { 13 | "deadfoxygrandpa/elm-test": "3.0.0 <= v < 4.0.0", 14 | "elm-lang/core": "3.0.0 <= v < 4.0.0" 15 | }, 16 | "elm-version": "0.16.0 <= v < 0.17.0" 17 | } 18 | --------------------------------------------------------------------------------