Skip to content

Prepare for 2.0 release #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
"package.json"
],
"dependencies": {
"purescript-either": "^1.0.0",
"purescript-maybe": "^1.0.0"
"purescript-either": "^2.0.0",
"purescript-maybe": "^2.0.0"
},
"devDependencies": {
"purescript-assert": "^1.0.0",
"purescript-console": "^1.0.0",
"purescript-assert": "^2.0.0",
"purescript-console": "^2.0.0",
"purescript-partial": "^1.1.2"
}
}
3 changes: 0 additions & 3 deletions src/Data/Char.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/* global exports */
"use strict";

// module Data.Char

exports.toCharCode = function (c) {
return c.charCodeAt(0);
};
Expand Down
11 changes: 8 additions & 3 deletions src/Data/String.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/* global exports */
"use strict";

// module Data.String

exports._charAt = function (just) {
return function (nothing) {
return function (i) {
Expand Down Expand Up @@ -114,6 +111,14 @@ exports.replace = function (s1) {
};
};

exports.replaceAll = function (s1) {
return function (s2) {
return function (s3) {
return s3.replace(new RegExp(s1.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&"), "g"), s2);
};
};
};

exports.take = function (n) {
return function (s) {
return s.substr(0, n);
Expand Down
165 changes: 99 additions & 66 deletions src/Data/String.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
-- | A String represents a sequence of characters.
-- | For details of the underlying implementation, see [String Reference at MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String).
module Data.String
( charAt
( Pattern(..)
, Replacement(..)
, charAt
, charCodeAt
, fromCharArray
, toChar
Expand All @@ -17,6 +19,7 @@ module Data.String
, singleton
, localeCompare
, replace
, replaceAll
, take
, takeWhile
, drop
Expand All @@ -36,17 +39,39 @@ module Data.String
import Prelude

import Data.Maybe (Maybe(..), isJust)
import Data.Newtype (class Newtype)
import Data.String.Unsafe as U

-- | A newtype used in cases where there is a string to be matched.
newtype Pattern = Pattern String

derive instance eqPattern :: Eq Pattern
derive instance ordPattern :: Ord Pattern
derive instance newtypePattern :: Newtype Pattern _

instance showPattern :: Show Pattern where
show (Pattern s) = "(Pattern " <> s <> ")"

-- | A newtype used in cases to specify a replacement for a pattern.
newtype Replacement = Replacement String

derive instance eqReplacement :: Eq Replacement
derive instance ordReplacement :: Ord Replacement
derive instance newtypeReplacement :: Newtype Replacement _

instance showReplacement :: Show Replacement where
show (Replacement s) = "(Replacement " <> s <> ")"

-- | Returns the character at the given index, if the index is within bounds.
charAt :: Int -> String -> Maybe Char
charAt = _charAt Just Nothing

foreign import _charAt :: (forall a. a -> Maybe a)
-> (forall a. Maybe a)
-> Int
-> String
-> Maybe Char
foreign import _charAt
:: (forall a. a -> Maybe a)
-> (forall a. Maybe a)
-> Int
-> String
-> Maybe Char

-- | Returns a string of length `1` containing the given character.
foreign import singleton :: Char -> String
Expand All @@ -56,19 +81,21 @@ foreign import singleton :: Char -> String
charCodeAt :: Int -> String -> Maybe Int
charCodeAt = _charCodeAt Just Nothing

foreign import _charCodeAt :: (forall a. a -> Maybe a)
-> (forall a. Maybe a)
-> Int
-> String
-> Maybe Int
foreign import _charCodeAt
:: (forall a. a -> Maybe a)
-> (forall a. Maybe a)
-> Int
-> String
-> Maybe Int

toChar :: String -> Maybe Char
toChar = _toChar Just Nothing

foreign import _toChar :: (forall a. a -> Maybe a)
-> (forall a. Maybe a)
-> String
-> Maybe Char
foreign import _toChar
:: (forall a. a -> Maybe a)
-> (forall a. Maybe a)
-> String
-> Maybe Char

-- | Returns `true` if the given string is empty.
null :: String -> Boolean
Expand All @@ -81,7 +108,7 @@ uncons "" = Nothing
uncons s = Just { head: U.charAt zero s, tail: drop one s }

-- | Returns the longest prefix (possibly empty) of characters that satisfy
-- | the predicate:
-- | the predicate.
takeWhile :: (Char -> Boolean) -> String -> String
takeWhile p s = take (count p s) s

Expand All @@ -91,80 +118,82 @@ dropWhile p s = drop (count p s) s

-- | If the string starts with the given prefix, return the portion of the
-- | string left after removing it, as a Just value. Otherwise, return Nothing.
-- | * `stripPrefix "http:" "http://purescript.org" == Just "//purescript.org"`
-- | * `stripPrefix "http:" "https://purescript.org" == Nothing`
stripPrefix :: String -> String -> Maybe String
stripPrefix prefix str =
-- | * `stripPrefix (Pattern "http:") "http://purescript.org" == Just "//purescript.org"`
-- | * `stripPrefix (Pattern "http:") "https://purescript.org" == Nothing`
stripPrefix :: Pattern -> String -> Maybe String
stripPrefix prefix@(Pattern prefixS) str =
case indexOf prefix str of
Just 0 -> Just $ drop (length prefix) str
_ -> Nothing
Just 0 -> Just $ drop (length prefixS) str
_ -> Nothing

-- | If the string ends with the given suffix, return the portion of the
-- | string left after removing it, as a Just value. Otherwise, return Nothing.
-- | * `stripSuffix ".exe" "psc.exe" == Just "psc"`
-- | * `stripSuffix ".exe" "psc" == Nothing`
stripSuffix :: String -> String -> Maybe String
stripSuffix suffix str =
-- | * `stripSuffix (Pattern ".exe") "psc.exe" == Just "psc"`
-- | * `stripSuffix (Pattern ".exe") "psc" == Nothing`
stripSuffix :: Pattern -> String -> Maybe String
stripSuffix suffix@(Pattern suffixS) str =
case lastIndexOf suffix str of
Just x | x == length str - length suffix ->
Just $ take x str
_ ->
Nothing
Just x | x == length str - length suffixS -> Just $ take x str
_ -> Nothing

-- | Converts an array of characters into a string.
foreign import fromCharArray :: Array Char -> String

-- | Checks whether the first string exists in the second string.
contains :: String -> String -> Boolean
contains x s = isJust (indexOf x s)
contains :: Pattern -> String -> Boolean
contains pat = isJust <<< indexOf pat

-- | Returns the index of the first occurrence of the first string in the
-- | second string. Returns `Nothing` if there is no match.
indexOf :: String -> String -> Maybe Int
indexOf :: Pattern -> String -> Maybe Int
indexOf = _indexOf Just Nothing

foreign import _indexOf :: (forall a. a -> Maybe a)
-> (forall a. Maybe a)
-> String
-> String
-> Maybe Int
foreign import _indexOf
:: (forall a. a -> Maybe a)
-> (forall a. Maybe a)
-> Pattern
-> String
-> Maybe Int

-- | Returns the index of the first occurrence of the first string in the
-- | second string, starting at the given index. Returns `Nothing` if there is
-- | no match.
indexOf' :: String -> Int -> String -> Maybe Int
indexOf' :: Pattern -> Int -> String -> Maybe Int
indexOf' = _indexOf' Just Nothing

foreign import _indexOf' :: (forall a. a -> Maybe a)
-> (forall a. Maybe a)
-> String
-> Int
-> String
-> Maybe Int
foreign import _indexOf'
:: (forall a. a -> Maybe a)
-> (forall a. Maybe a)
-> Pattern
-> Int
-> String
-> Maybe Int

-- | Returns the index of the last occurrence of the first string in the
-- | second string. Returns `Nothing` if there is no match.
lastIndexOf :: String -> String -> Maybe Int
lastIndexOf :: Pattern -> String -> Maybe Int
lastIndexOf = _lastIndexOf Just Nothing

foreign import _lastIndexOf :: (forall a. a -> Maybe a)
-> (forall a. Maybe a)
-> String
-> String
-> Maybe Int
foreign import _lastIndexOf
:: (forall a. a -> Maybe a)
-> (forall a. Maybe a)
-> Pattern
-> String
-> Maybe Int

-- | Returns the index of the last occurrence of the first string in the
-- | second string, starting at the given index. Returns `Nothing` if there is
-- | no match.
lastIndexOf' :: String -> Int -> String -> Maybe Int
lastIndexOf' :: Pattern -> Int -> String -> Maybe Int
lastIndexOf' = _lastIndexOf' Just Nothing

foreign import _lastIndexOf' :: (forall a. a -> Maybe a)
-> (forall a. Maybe a)
-> String
-> Int
-> String
-> Maybe Int
foreign import _lastIndexOf'
:: (forall a. a -> Maybe a)
-> (forall a. Maybe a)
-> Pattern
-> Int
-> String
-> Maybe Int

-- | Returns the number of characters the string is composed of.
foreign import length :: String -> Int
Expand All @@ -173,15 +202,19 @@ foreign import length :: String -> Int
localeCompare :: String -> String -> Ordering
localeCompare = _localeCompare LT EQ GT

foreign import _localeCompare :: Ordering
-> Ordering
-> Ordering
-> String
-> String
-> Ordering
foreign import _localeCompare
:: Ordering
-> Ordering
-> Ordering
-> String
-> String
-> Ordering

-- | Replaces the first occurence of the first argument with the second argument.
foreign import replace :: String -> String -> String -> String
foreign import replace :: Pattern -> Replacement -> String -> String

-- | Replaces all occurences of the first argument with the second argument.
foreign import replaceAll :: Pattern -> Replacement -> String -> String

-- | Returns the first `n` characters of the string.
foreign import take :: Int -> String -> String
Expand All @@ -196,7 +229,7 @@ foreign import count :: (Char -> Boolean) -> String -> Int
-- | Returns the substrings of the second string separated along occurences
-- | of the first string.
-- | * `split " " "hello world" == ["hello", "world"]`
foreign import split :: String -> String -> Array String
foreign import split :: Pattern -> String -> Array String

-- | Returns the substrings of split at the given index, if the index is within bounds.
splitAt :: Int -> String -> Maybe (Array String)
Expand Down
22 changes: 22 additions & 0 deletions src/Data/String/CaseInsensitiveString.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Data.String.CaseInsensitive where

import Prelude

import Data.Newtype (class Newtype)
import Data.String (toLower)

-- | A newtype for case insensitive string comparisons and ordering.
newtype CaseInsensitiveString = CaseInsensitiveString String

instance eqCaseInsensitiveString :: Eq CaseInsensitiveString where
eq (CaseInsensitiveString s1) (CaseInsensitiveString s2) =
toLower s1 == toLower s2

instance ordCaseInsensitiveString :: Ord CaseInsensitiveString where
compare (CaseInsensitiveString s1) (CaseInsensitiveString s2) =
compare (toLower s1) (toLower s2)

instance showCaseInsensitiveString :: Show CaseInsensitiveString where
show (CaseInsensitiveString s) = "(CaseInsensitiveString " <> s <> ")"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a thought: this (and several other Show instances) could use wrap now, which would make things a bit shorter.


derive instance newtypeCaseInsensitiveString :: Newtype CaseInsensitiveString _
3 changes: 0 additions & 3 deletions src/Data/String/Regex.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/* global exports */
"use strict";

// module Data.String.Regex

exports["showRegex'"] = function (r) {
return "" + r;
};
Expand Down
Loading