Skip to content

Commit 1fb59e1

Browse files
committed
Modify @hdgarrood's PR to use Data.Op.
1 parent a7b4244 commit 1fb59e1

File tree

6 files changed

+144
-188
lines changed

6 files changed

+144
-188
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.pulp-cache
12
.psci
23
.psci_modules/
34
node_modules/

bower.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
],
1818
"dependencies": {
1919
"purescript-foreign": "~0.7.0",
20-
"purescript-unsafe-coerce": "~0.1.0"
20+
"purescript-maps": "~0.5.4",
21+
"purescript-tuples": "~0.4.0",
22+
"purescript-monoid": "~0.3.2",
23+
"purescript-maybe": "~0.3.5",
24+
"purescript-contravariant": "~0.2.3"
2125
},
2226
"devDependencies": {
2327
"purescript-console": "~0.1.1"

docs/Data/Options.md

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,99 @@
33
#### `Options`
44

55
``` purescript
6-
data Options :: * -> *
6+
newtype Options opt
77
```
88

9+
The `Options` type represents a set of options. The type argument is a
10+
phantom type, which is useful for ensuring that options for one particular
11+
API are not accidentally passed to some other API.
12+
913
##### Instances
1014
``` purescript
11-
instance semigroupOptions :: Semigroup (Options a)
12-
instance monoidOptions :: Monoid (Options a)
15+
Semigroup (Options opt)
16+
Monoid (Options opt)
1317
```
1418

15-
#### `Option`
19+
#### `runOptions`
1620

1721
``` purescript
18-
data Option :: * -> * -> *
22+
runOptions :: forall opt. Options opt -> Array (Tuple String Foreign)
1923
```
2024

21-
#### `IsOption`
25+
#### `options`
2226

2327
``` purescript
24-
class IsOption value where
25-
assoc :: forall opt. Option opt value -> value -> Options opt
28+
options :: forall opt. Options opt -> Foreign
2629
```
2730

28-
##### Instances
31+
Convert an `Options` value into a JavaScript object, suitable for passing
32+
to JavaScript APIs.
33+
34+
#### `Option`
35+
2936
``` purescript
30-
instance isOptionString :: IsOption String
31-
instance isOptionBoolean :: IsOption Boolean
32-
instance isOptionNumber :: IsOption Number
33-
instance isOptionInt :: IsOption Int
34-
instance isOptionRecord :: IsOption { | a }
35-
instance isOptionUnit :: IsOption Unit
36-
instance isOptionFunction :: IsOption (a -> b)
37-
instance isOptionArray :: (IsOption a) => IsOption (Array a)
38-
instance isOptionMaybe :: (IsOption a) => IsOption (Maybe a)
37+
type Option opt = Op (Options opt)
3938
```
4039

41-
#### `(:=)`
40+
An `Option` represents an opportunity to configure a specific attribute
41+
of a call to some API. This normally corresponds to one specific property
42+
of an "options" object in JavaScript APIs, but can in general correspond
43+
to zero or more actual properties.
44+
45+
#### `assoc`
4246

4347
``` purescript
44-
(:=) :: forall opt value. (IsOption value) => Option opt value -> value -> Options opt
48+
assoc :: forall opt value. Option opt value -> value -> Options opt
4549
```
4650

47-
_right-associative / precedence 6_
51+
Associates a value with a specific option.
4852

49-
#### `optionFn`
53+
#### `(:=)`
5054

5155
``` purescript
52-
optionFn :: forall opt from to. Option opt from -> Option opt to
56+
(:=) :: forall opt value. Option opt value -> value -> Options opt
5357
```
5458

55-
#### `key`
59+
_right-associative / precedence 6_
60+
61+
An infix version of `assoc`.
62+
63+
#### `optional`
5664

5765
``` purescript
58-
key :: forall opt value. Option opt value -> String
66+
optional :: forall opt value. Option opt value -> Option opt (Maybe value)
5967
```
6068

69+
A version of `assoc` which takes possibly absent values. `Nothing` values
70+
are ignored; passing `Nothing` for the second argument will result in an
71+
empty `Options`.
72+
6173
#### `opt`
6274

6375
``` purescript
64-
opt :: forall opt value. (IsOption value) => String -> Option opt value
76+
opt :: forall opt value. String -> Option opt value
6577
```
6678

67-
#### `runOptions`
79+
The default way of creating `Option` values. Constructs an `Option` with
80+
the given key, which passes the given value through unchanged.
81+
82+
#### `tag`
6883

6984
``` purescript
70-
runOptions :: forall a. Options a -> Array (Tuple String Foreign)
85+
tag :: forall opt value. Option opt value -> value -> Option opt Unit
7186
```
7287

73-
#### `options`
88+
Create a `tag`, by fixing an `Option` to a single value.
89+
90+
#### `defaultToOptions`
7491

7592
``` purescript
76-
options :: forall a. Options a -> Foreign
93+
defaultToOptions :: forall opt value. String -> value -> Options opt
7794
```
7895

96+
The default method for turning a string property key into an
97+
`Option`. This function simply calls `toForeign` on the value. If
98+
you need some other behaviour, you can write your own function to replace
99+
this one, and construct an `Option` yourself.
100+
79101

src/Data/Options.js

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/Data/Options.purs

Lines changed: 59 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,78 @@
11
module Data.Options
22
( Options()
3-
, Option()
4-
, IsOption
5-
, assoc, (:=)
6-
, optionFn
7-
, options
83
, runOptions
9-
, opt, key
4+
, options
5+
, Option
6+
, assoc, (:=)
7+
, optional
8+
, opt
9+
, tag
10+
, defaultToOptions
1011
) where
1112

1213
import Prelude
1314

14-
import Data.Foreign (Foreign())
15-
16-
import Data.Function (Fn2(), runFn2)
15+
import Data.Foreign (toForeign, Foreign())
16+
import Data.Maybe (Maybe(), maybe)
17+
import Data.Monoid (mempty, Monoid)
18+
import Data.Op (Op(Op), runOp)
19+
import Data.StrMap as StrMap
20+
import Data.Tuple (Tuple(..))
1721

18-
import Data.Maybe (Maybe(..))
22+
-- | The `Options` type represents a set of options. The type argument is a
23+
-- | phantom type, which is useful for ensuring that options for one particular
24+
-- | API are not accidentally passed to some other API.
25+
newtype Options opt =
26+
Options (Array (Tuple String Foreign))
1927

20-
import Data.Monoid (Monoid)
28+
runOptions :: forall opt. Options opt -> Array (Tuple String Foreign)
29+
runOptions (Options xs) = xs
2130

22-
import Data.Tuple (Tuple(..))
31+
instance semigroupOptions :: Semigroup (Options opt) where
32+
append (Options xs) (Options ys) = Options (xs <> ys)
2333

24-
import Unsafe.Coerce (unsafeCoerce)
34+
instance monoidOptions :: Monoid (Options opt) where
35+
mempty = Options []
2536

26-
foreign import data Options :: * -> *
37+
-- | Convert an `Options` value into a JavaScript object, suitable for passing
38+
-- | to JavaScript APIs.
39+
options :: forall opt. Options opt -> Foreign
40+
options = toForeign <<< StrMap.fromFoldable <<< runOptions
2741

28-
foreign import data Option :: * -> * -> *
42+
-- | An `Option` represents an opportunity to configure a specific attribute
43+
-- | of a call to some API. This normally corresponds to one specific property
44+
-- | of an "options" object in JavaScript APIs, but can in general correspond
45+
-- | to zero or more actual properties.
46+
type Option opt = Op (Options opt)
2947

30-
class IsOption value where
31-
assoc :: forall opt. Option opt value -> value -> Options opt
48+
-- | Associates a value with a specific option.
49+
assoc :: forall opt value. Option opt value -> value -> Options opt
50+
assoc o value = runOp o value
3251

3352
infixr 6 :=
3453

35-
(:=) :: forall opt value. (IsOption value) => Option opt value -> value -> Options opt
54+
-- | An infix version of `assoc`.
55+
(:=) :: forall opt value. Option opt value -> value -> Options opt
3656
(:=) = assoc
3757

38-
instance semigroupOptions :: Semigroup (Options a) where
39-
append = runFn2 appendFn
40-
41-
instance monoidOptions :: Monoid (Options a) where
42-
mempty = memptyFn
43-
44-
instance isOptionString :: IsOption String where
45-
assoc = runFn2 isOptionPrimFn
46-
47-
instance isOptionBoolean :: IsOption Boolean where
48-
assoc = runFn2 isOptionPrimFn
49-
50-
instance isOptionNumber :: IsOption Number where
51-
assoc = runFn2 isOptionPrimFn
52-
53-
instance isOptionInt :: IsOption Int where
54-
assoc = runFn2 isOptionPrimFn
55-
56-
instance isOptionRecord :: IsOption { | a } where
57-
assoc = runFn2 isOptionPrimFn
58-
59-
instance isOptionUnit :: IsOption Unit where
60-
assoc = runFn2 isOptionPrimFn
61-
62-
instance isOptionFunction :: IsOption (a -> b) where
63-
assoc = runFn2 isOptionPrimFn
64-
65-
instance isOptionArray :: (IsOption a) => IsOption (Array a) where
66-
assoc k vs = joinFn $ assoc (optionFn k) <$> vs
67-
68-
instance isOptionMaybe :: (IsOption a) => IsOption (Maybe a) where
69-
assoc k Nothing = memptyFn
70-
assoc k (Just a) = assoc (optionFn k) a
71-
72-
optionFn :: forall opt from to. Option opt from -> Option opt to
73-
optionFn = unsafeCoerce
74-
75-
key :: forall opt value. Option opt value -> String
76-
key = unsafeCoerce
77-
78-
opt :: forall opt value. (IsOption value) => String -> Option opt value
79-
opt = unsafeCoerce
80-
81-
runOptions :: forall a. Options a -> Array (Tuple String Foreign)
82-
runOptions = runOptionsFn Tuple
83-
84-
foreign import memptyFn :: forall a. Options a
85-
86-
foreign import appendFn :: forall a. Fn2 (Options a) (Options a) (Options a)
87-
88-
foreign import joinFn :: forall a b. Array (Options a) -> Options b
89-
90-
foreign import isOptionPrimFn :: forall b a. Fn2 (Option b a) a (Options b)
91-
92-
foreign import options :: forall a. Options a -> Foreign
93-
94-
foreign import runOptionsFn :: forall a. (String -> Foreign -> Tuple String Foreign) -> Options a -> Array (Tuple String Foreign)
58+
-- | A version of `assoc` which takes possibly absent values. `Nothing` values
59+
-- | are ignored; passing `Nothing` for the second argument will result in an
60+
-- | empty `Options`.
61+
optional :: forall opt value. Option opt value -> Option opt (Maybe value)
62+
optional option = Op $ maybe mempty (option :=)
63+
64+
-- | The default way of creating `Option` values. Constructs an `Option` with
65+
-- | the given key, which passes the given value through unchanged.
66+
opt :: forall opt value. String -> Option opt value
67+
opt = Op <<< defaultToOptions
68+
69+
-- | Create a `tag`, by fixing an `Option` to a single value.
70+
tag :: forall opt value. Option opt value -> value -> Option opt Unit
71+
tag o value = Op \_ -> o := value
72+
73+
-- | The default method for turning a string property key into an
74+
-- | `Option`. This function simply calls `toForeign` on the value. If
75+
-- | you need some other behaviour, you can write your own function to replace
76+
-- | this one, and construct an `Option` yourself.
77+
defaultToOptions :: forall opt value. String -> value -> Options opt
78+
defaultToOptions k v = Options [Tuple k (toForeign v)]

0 commit comments

Comments
 (0)