haste-compiler-0.5.5.0: Haskell To ECMAScript compiler

Safe HaskellNone
LanguageHaskell98

Haste.JSString

Contents

Description

JSString standard functions, to make them a more viable alternative to the horribly inefficient standard Strings.

Many functions have linear time complexity due to JavaScript engines not implementing slicing, etc. in constant time.

All functions are supported on both client and server, with the exception of match, matches, regex and replace, which are wrappers on top of JavaScript's native regular expressions and thus only supported on the client.

Synopsis

Building JSStrings

empty :: JSString

O(1) The empty JSString.

singleton :: Char -> JSString

O(1) JSString consisting of a single character.

pack :: [Char] -> JSString

O(n) Convert a list of Char into a JSString.

cons :: Char -> JSString -> JSString infixr 5

O(n) Prepend a character to a JSString.

snoc :: JSString -> Char -> JSString infixl 5

O(n) Append a character to a JSString.

append :: JSString -> JSString -> JSString

O(n) Append two JSStrings.

replicate :: Int -> Char -> JSString

O(n) Create a JSString containing n instances of a single character.

Deconstructing JSStrings

unpack :: JSString -> [Char]

O(n) Convert a JSString to a list of Char.

head :: JSString -> Char

O(1) Extract the first element of a non-empty JSString.

last :: JSString -> Char

O(1) Extract the last element of a non-empty JSString.

tail :: JSString -> JSString

O(n) All elements but the first of a JSString. Returns an empty JSString if the given JSString is empty.

drop :: Int -> JSString -> JSString

O(n) Drop n elements from the given JSString.

take :: Int -> JSString -> JSString

O(n) Take n elements from the given JSString.

init :: JSString -> JSString

O(n) All elements but the last of a JSString. Returns an empty JSString if the given JSString is empty.

splitAt :: Int -> JSString -> (JSString, JSString)

O(n) Equivalent to (take n xs, drop n xs).

Examining JSStrings

null :: JSString -> Bool

O(1) Test whether a JSString is empty.

length :: JSString -> Int

O(1) Get the length of a JSString as an Int.

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

O(n) Determines whether any character in the string satisfies the given predicate.

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

O(n) Determines whether all characters in the string satisfy the given predicate.

Modifying JSStrings

map :: (Char -> Char) -> JSString -> JSString

O(n) Map a function over the given JSString.

reverse :: JSString -> JSString

O(n) reverse a JSString.

intercalate :: JSString -> [JSString] -> JSString

O(n) Join a list of JSStrings, with a specified separator. Equivalent to join.

foldl' :: (ToAny a, FromAny a) => (a -> Char -> a) -> a -> JSString -> a

O(n) Left fold over a JSString.

foldr :: (ToAny a, FromAny a) => (Char -> a -> a) -> a -> JSString -> a

O(n) Right fold over a JSString.

concat :: [JSString] -> JSString

O(n) Concatenate a list of JSStrings.

concatMap :: (Char -> JSString) -> JSString -> JSString

O(n) Map a function over a JSString, then concatenate the results. Note that this function is actually faster than map in most cases.

Regular expressions (client-side only)

data RegEx

A regular expression. May be used to match and replace JSStrings.

match :: RegEx -> JSString -> [JSString]

O(n) Find all strings corresponding to the given regular expression.

matches :: JSString -> RegEx -> Bool

O(n) Determines whether the given JSString matches the given regular expression or not.

regex

Arguments

:: JSString

Regular expression.

-> JSString

Potential flags.

-> RegEx 

O(n) Compile a regular expression and an (optionally empty) list of flags into a RegEx which can be used to match, replace, etc. on JSStrings.

The regular expression and flags are passed verbatim to the browser's RegEx constructor, meaning that the syntax is the same as when using regular expressions in raw JavaScript.

replace

Arguments

:: JSString

String perform substitution on.

-> RegEx

Regular expression to match.

-> JSString

Replacement string.

-> JSString 

O(n) String substitution using regular expressions.