haste-compiler-0.5.0.1: Haskell To ECMAScript compiler

Safe HaskellNone
LanguageHaskell98

Haste.JSString

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

Documentation

Building JSStrings

empty :: JSString Source

O(1) The empty JSString.

singleton :: Char -> JSString Source

O(1) JSString consisting of a single character.

pack :: [Char] -> JSString Source

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

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

O(n) Prepend a character to a JSString.

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

O(n) Append a character to a JSString.

append :: JSString -> JSString -> JSString Source

O(n) Append two JSStrings.

replicate :: Int -> Char -> JSString Source

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

Deconstructing JSStrings

unpack :: JSString -> [Char] Source

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

head :: JSString -> Char Source

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

last :: JSString -> Char Source

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

tail :: JSString -> JSString Source

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

drop :: Int -> JSString -> JSString Source

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

take :: Int -> JSString -> JSString Source

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

init :: JSString -> JSString Source

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) Source

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

Examining JSStrings

null :: JSString -> Bool Source

O(1) Test whether a JSString is empty.

length :: JSString -> Int Source

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

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

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

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

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

Modifying JSStrings

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

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

reverse :: JSString -> JSString Source

O(n) reverse a JSString.

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

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 Source

O(n) Left fold over a JSString.

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

O(n) Right fold over a JSString.

concat :: [JSString] -> JSString Source

O(n) Concatenate a list of JSStrings.

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

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 Source

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

Instances

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

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

matches :: JSString -> RegEx -> Bool Source

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

regex Source

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 Source

Arguments

:: JSString

String perform substitution on.

-> RegEx

Regular expression to match.

-> JSString

Replacement string.

-> JSString 

O(n) String substitution using regular expressions.