-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathCommon.purs
103 lines (91 loc) · 2.8 KB
/
Common.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
module Data.String.Common
( null
, localeCompare
, replace
, replaceAll
, split
, toLower
, toUpper
, trim
, joinWith
) where
import Prelude
import Data.String.Pattern (Pattern, Replacement)
-- | Returns `true` if the given string is empty.
-- |
-- | ```purescript
-- | null "" == true
-- | null "Hi" == false
-- | ```
null :: String -> Boolean
null s = s == ""
-- | Compare two strings in a locale-aware fashion. This is in contrast to
-- | the `Ord` instance on `String` which treats strings as arrays of code
-- | units:
-- |
-- | ```purescript
-- | "ä" `localeCompare` "b" == LT
-- | "ä" `compare` "b" == GT
-- | ```
localeCompare :: String -> String -> Ordering
localeCompare = _localeCompare LT EQ GT
foreign import _localeCompare
:: Ordering
-> Ordering
-> Ordering
-> String
-> String
-> Ordering
-- | Replaces the first occurence of the pattern with the replacement string.
-- |
-- | ```purescript
-- | replace (Pattern "<=") (Replacement "≤") "a <= b <= c" == "a ≤ b <= c"
-- | ```
foreign import replace :: Pattern -> Replacement -> String -> String
-- | Replaces all occurences of the pattern with the replacement string.
-- |
-- | ```purescript
-- | replaceAll (Pattern "<=") (Replacement "≤") "a <= b <= c" == "a ≤ b ≤ c"
-- | ```
foreign import replaceAll :: Pattern -> Replacement -> String -> String
-- | Returns the substrings of the second string separated along occurrences
-- | of the first string.
-- |
-- | ```purescript
-- | -- single match
-- | split (Pattern " ") "hello world" == ["hello", "world"]
-- | -- multiple matches
-- | split (Pattern " | ") "foo | bar | baz" == ["foo", "bar", "baz"]
-- | -- no match
-- | split (Pattern "baz") "foo bar" == ["foo bar"]
-- | -- empty string
-- | split (Pattern "") "foo bar" == ["f","o","o"," ","b","a","r"]
-- | ```
foreign import split :: Pattern -> String -> Array String
-- | Returns the argument converted to lowercase.
-- |
-- | ```purescript
-- | toLower "hElLo" == "hello"
-- | ```
foreign import toLower :: String -> String
-- | Returns the argument converted to uppercase.
-- |
-- | ```purescript
-- | toUpper "Hello" == "HELLO"
-- | ```
foreign import toUpper :: String -> String
-- | Removes whitespace from the beginning and end of a string, including
-- | [whitespace characters](http://www.ecma-international.org/ecma-262/5.1/#sec-7.2)
-- | and [line terminators](http://www.ecma-international.org/ecma-262/5.1/#sec-7.3).
-- |
-- | ```purescript
-- | trim " Hello \n World\n\t " == "Hello \n World"
-- | ```
foreign import trim :: String -> String
-- | Joins the strings in the array together, inserting the first argument
-- | as separator between them.
-- |
-- | ```purescript
-- | joinWith ", " ["apple", "banana", "orange"] == "apple, banana, orange"
-- | ```
foreign import joinWith :: String -> Array String -> String