Module

Data.String.NonEmpty.CodeUnits

Package
purescript-strings
Repository
purescript/purescript-strings

#fromCharArray Source

fromCharArray :: Array Char -> Maybe NonEmptyString

Creates a NonEmptyString from a character array String, returning Nothing if the input is empty.

fromCharArray [] = Nothing
fromCharArray ['a', 'b', 'c'] = Just (NonEmptyString "abc")

#singleton Source

singleton :: Char -> NonEmptyString

Creates a NonEmptyString from a character.

#cons Source

cons :: Char -> String -> NonEmptyString

Creates a NonEmptyString from a string by prepending a character.

cons 'a' "bc" = NonEmptyString "abc"
cons 'a' "" = NonEmptyString "a"

#snoc Source

snoc :: Char -> String -> NonEmptyString

Creates a NonEmptyString from a string by appending a character.

snoc 'c' "ab" = NonEmptyString "abc"
snoc 'a' "" = NonEmptyString "a"

#fromFoldable1 Source

fromFoldable1 :: forall f. Foldable1 f => f Char -> NonEmptyString

Creates a NonEmptyString from a Foldable1 container carrying characters.

#toCharArray Source

toCharArray :: NonEmptyString -> Array Char

Converts the NonEmptyString into an array of characters.

toCharArray (NonEmptyString "Hello☺\n") == ['H','e','l','l','o','☺','\n']

#toNonEmptyCharArray Source

toNonEmptyCharArray :: NonEmptyString -> NonEmptyArray Char

Converts the NonEmptyString into a non-empty array of characters.

#charAt Source

charAt :: Int -> NonEmptyString -> Maybe Char

Returns the character at the given index, if the index is within bounds.

charAt 2 (NonEmptyString "Hello") == Just 'l'
charAt 10 (NonEmptyString "Hello") == Nothing

#toChar Source

toChar :: NonEmptyString -> Maybe Char

Converts the NonEmptyString to a character, if the length of the string is exactly 1.

toChar "H" == Just 'H'
toChar "Hi" == Nothing

#indexOf Source

indexOf :: Pattern -> NonEmptyString -> Maybe Int

Returns the index of the first occurrence of the pattern in the given string. Returns Nothing if there is no match.

indexOf (Pattern "c") (NonEmptyString "abcdc") == Just 2
indexOf (Pattern "c") (NonEmptyString "aaa") == Nothing

#indexOf' Source

indexOf' :: Pattern -> Int -> NonEmptyString -> Maybe Int

Returns the index of the first occurrence of the pattern in the given string, starting at the specified index. Returns Nothing if there is no match.

indexOf' (Pattern "a") 2 (NonEmptyString "ababa") == Just 2
indexOf' (Pattern "a") 3 (NonEmptyString "ababa") == Just 4

#lastIndexOf Source

lastIndexOf :: Pattern -> NonEmptyString -> Maybe Int

Returns the index of the last occurrence of the pattern in the given string. Returns Nothing if there is no match.

lastIndexOf (Pattern "c") (NonEmptyString "abcdc") == Just 4
lastIndexOf (Pattern "c") (NonEmptyString "aaa") == Nothing

#lastIndexOf' Source

lastIndexOf' :: Pattern -> Int -> NonEmptyString -> Maybe Int

Returns the index of the last occurrence of the pattern in the given string, starting at the specified index and searching backwards towards the beginning of the string. Returns Nothing if there is no match.

lastIndexOf' (Pattern "a") 1 (NonEmptyString "ababa") == Just 0
lastIndexOf' (Pattern "a") 3 (NonEmptyString "ababa") == Just 2
lastIndexOf' (Pattern "a") 4 (NonEmptyString "ababa") == Just 4

#uncons Source

uncons :: NonEmptyString -> { head :: Char, tail :: Maybe NonEmptyString }

Returns the first character and the rest of the string.

uncons "a" == { head: 'a', tail: Nothing }
uncons "Hello World" == { head: 'H', tail: Just (NonEmptyString "ello World") }

#length Source

length :: NonEmptyString -> Int

Returns the number of characters the string is composed of.

length (NonEmptyString "Hello World") == 11

#take Source

take :: Int -> NonEmptyString -> Maybe NonEmptyString

Returns the first n characters of the string. Returns Nothing if n is less than 1.

take 5 (NonEmptyString "Hello World") == Just (NonEmptyString "Hello")
take 0 (NonEmptyString "Hello World") == Nothing

#takeRight Source

takeRight :: Int -> NonEmptyString -> Maybe NonEmptyString

Returns the last n characters of the string. Returns Nothing if n is less than 1.

take 5 (NonEmptyString "Hello World") == Just (NonEmptyString "World")
take 0 (NonEmptyString "Hello World") == Nothing

#takeWhile Source

takeWhile :: (Char -> Boolean) -> NonEmptyString -> Maybe NonEmptyString

Returns the longest prefix of characters that satisfy the predicate. Nothing is returned if there is no matching prefix.

takeWhile (_ /= ':') (NonEmptyString "http://purescript.org") == Just (NonEmptyString "http")
takeWhile (_ == 'a') (NonEmptyString "xyz") == Nothing

#drop Source

drop :: Int -> NonEmptyString -> Maybe NonEmptyString

Returns the string without the first n characters. Returns Nothing if more characters are dropped than the string is long.

drop 6 (NonEmptyString "Hello World") == Just (NonEmptyString "World")
drop 20 (NonEmptyString "Hello World") == Nothing

#dropRight Source

dropRight :: Int -> NonEmptyString -> Maybe NonEmptyString

Returns the string without the last n characters. Returns Nothing if more characters are dropped than the string is long.

dropRight 6 (NonEmptyString "Hello World") == Just (NonEmptyString "Hello")
dropRight 20 (NonEmptyString "Hello World") == Nothing

#dropWhile Source

dropWhile :: (Char -> Boolean) -> NonEmptyString -> Maybe NonEmptyString

Returns the suffix remaining after takeWhile.

dropWhile (_ /= '.') (NonEmptyString "Test.purs") == Just (NonEmptyString ".purs")

#countPrefix Source

countPrefix :: (Char -> Boolean) -> NonEmptyString -> Int

Returns the number of contiguous characters at the beginning of the string for which the predicate holds.

countPrefix (_ /= 'o') (NonEmptyString "Hello World") == 4

#splitAt Source

splitAt :: Int -> NonEmptyString -> { after :: Maybe NonEmptyString, before :: Maybe NonEmptyString }

Returns the substrings of a split at the given index, if the index is within bounds.

splitAt 2 (NonEmptyString "Hello World") == Just { before: Just (NonEmptyString "He"), after: Just (NonEmptyString "llo World") }
splitAt 10 (NonEmptyString "Hi") == Nothing