Skip to content

Commit e776049

Browse files
glennslzth
authored andcommitted
refactor: WithDefault -> Or
1 parent 911bcf3 commit e776049

14 files changed

+111
-47
lines changed

src/Core__Null.mjs

+8-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function compare(a, b, cmp) {
2020
return Core__Option.compare(a === null ? undefined : Caml_option.some(a), b === null ? undefined : Caml_option.some(b), cmp);
2121
}
2222

23-
function getWithDefault(value, $$default) {
23+
function getOr(value, $$default) {
2424
if (value !== null) {
2525
return value;
2626
} else {
@@ -47,7 +47,7 @@ function map(value, f) {
4747
}
4848
}
4949

50-
function mapWithDefault(value, $$default, f) {
50+
function mapOr(value, $$default, f) {
5151
if (value !== null) {
5252
return Curry._1(f, value);
5353
} else {
@@ -63,13 +63,19 @@ function flatMap(value, f) {
6363
}
6464
}
6565

66+
var getWithDefault = getOr;
67+
68+
var mapWithDefault = mapOr;
69+
6670
export {
6771
equal ,
6872
compare ,
6973
fromOption ,
74+
getOr ,
7075
getWithDefault ,
7176
getExn ,
7277
map ,
78+
mapOr ,
7379
mapWithDefault ,
7480
flatMap ,
7581
}

src/Core__Null.res

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ let equal = (a, b, eq) => Core__Option.equal(a->toOption, b->toOption, eq)
1818

1919
let compare = (a, b, cmp) => Core__Option.compare(a->toOption, b->toOption, cmp)
2020

21-
let getWithDefault = (value, default) =>
21+
let getOr = (value, default) =>
2222
switch value->toOption {
2323
| Some(x) => x
2424
| None => default
2525
}
2626

27+
let getWithDefault = getOr
28+
2729
let getExn: t<'a> => 'a = value =>
2830
switch value->toOption {
2931
| Some(x) => x
@@ -38,12 +40,14 @@ let map = (value, f) =>
3840
| None => null
3941
}
4042

41-
let mapWithDefault = (value, default, f) =>
43+
let mapOr = (value, default, f) =>
4244
switch value->toOption {
4345
| Some(x) => f(x)
4446
| None => default
4547
}
4648

49+
let mapWithDefault = mapOr
50+
4751
let flatMap = (value, f) =>
4852
switch value->toOption {
4953
| Some(x) => f(x)

src/Core__Null.resi

+13-7
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,25 @@ Console.log(asNull == null) // Logs `true` to the console.
7777
let fromOption: option<'a> => t<'a>
7878

7979
/**
80-
`getWithDefault(value, default)` returns `value` if not `null`, otherwise return
80+
`getOr(value, default)` returns `value` if not `null`, otherwise return
8181
`default`.
8282

8383
## Examples
8484

8585
```rescript
86-
Null.getWithDefault(null, "Banana") // Banana
87-
Null.getWithDefault(Nulalble.make("Apple"), "Banana") // Apple
86+
Null.getOr(null, "Banana") // Banana
87+
Null.getOr(Nulalble.make("Apple"), "Banana") // Apple
8888

8989
let greet = (firstName: option<string>) =>
90-
"Greetings " ++ firstName->Null.getWithDefault("Anonymous")
90+
"Greetings " ++ firstName->Null.getOr("Anonymous")
9191

9292
Null.make("Jane")->greet // "Greetings Jane"
9393
null->greet // "Greetings Anonymous"
9494
```
9595
*/
96+
let getOr: (t<'a>, 'a) => 'a
97+
98+
@deprecated("Use getOr instead")
9699
let getWithDefault: (t<'a>, 'a) => 'a
97100

98101
/**
@@ -139,19 +142,22 @@ Null.map(null, x => x * x) // null
139142
let map: (t<'a>, 'a => 'b) => t<'b>
140143

141144
/**
142-
`mapWithDefault(value, default, f)` returns `f(value)` if `value` is not `null`,
145+
`mapOr(value, default, f)` returns `f(value)` if `value` is not `null`,
143146
otherwise returns `default`.
144147

145148
## Examples
146149

147150
```rescript
148151
let someValue = Null.make(3)
149-
someValue->Null.mapWithDefault(0, x => x + 5) // 8
152+
someValue->Null.mapOr(0, x => x + 5) // 8
150153

151154
let noneValue = null
152-
noneValue->Null.mapWithDefault(0, x => x + 5) // 0
155+
noneValue->Null.mapOr(0, x => x + 5) // 0
153156
```
154157
*/
158+
let mapOr: (t<'a>, 'b, 'a => 'b) => 'b
159+
160+
@deprecated("Use mapOr instead")
155161
let mapWithDefault: (t<'a>, 'b, 'a => 'b) => 'b
156162

157163
/**

src/Core__Nullable.mjs

+8-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function compare(a, b, cmp) {
1919
return Core__Option.compare((a == null) ? undefined : Caml_option.some(a), (b == null) ? undefined : Caml_option.some(b), cmp);
2020
}
2121

22-
function getWithDefault(value, $$default) {
22+
function getOr(value, $$default) {
2323
if (value == null) {
2424
return $$default;
2525
} else {
@@ -46,7 +46,7 @@ function map(value, f) {
4646
}
4747
}
4848

49-
function mapWithDefault(value, $$default, f) {
49+
function mapOr(value, $$default, f) {
5050
if (value == null) {
5151
return $$default;
5252
} else {
@@ -62,13 +62,19 @@ function flatMap(value, f) {
6262
}
6363
}
6464

65+
var getWithDefault = getOr;
66+
67+
var mapWithDefault = mapOr;
68+
6569
export {
6670
equal ,
6771
compare ,
6872
fromOption ,
73+
getOr ,
6974
getWithDefault ,
7075
getExn ,
7176
map ,
77+
mapOr ,
7278
mapWithDefault ,
7379
flatMap ,
7480
}

src/Core__Nullable.res

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ let equal = (a, b, eq) => Core__Option.equal(a->toOption, b->toOption, eq)
1818

1919
let compare = (a, b, cmp) => Core__Option.compare(a->toOption, b->toOption, cmp)
2020

21-
let getWithDefault = (value, default) =>
21+
let getOr = (value, default) =>
2222
switch value->toOption {
2323
| Some(x) => x
2424
| None => default
2525
}
2626

27+
let getWithDefault = getOr
28+
2729
let getExn: t<'a> => 'a = value =>
2830
switch value->toOption {
2931
| Some(x) => x
@@ -38,12 +40,14 @@ let map = (value, f) =>
3840
| None => Obj.magic(value)
3941
}
4042

41-
let mapWithDefault = (value, default, f) =>
43+
let mapOr = (value, default, f) =>
4244
switch value->toOption {
4345
| Some(x) => f(x)
4446
| None => default
4547
}
4648

49+
let mapWithDefault = mapOr
50+
4751
let flatMap = (value, f) =>
4852
switch value->toOption {
4953
| Some(x) => f(x)

src/Core__Nullable.resi

+13-7
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,25 @@ let asNullable = optString->Nullable.fromOption // Nullable.t<string>
8787
let fromOption: option<'a> => t<'a>
8888

8989
/**
90-
`getWithDefault(value, default)` returns `value` if not `null` or `undefined`,
90+
`getOr(value, default)` returns `value` if not `null` or `undefined`,
9191
otherwise return `default`.
9292

9393
## Examples
9494

9595
```rescript
96-
Nullable.getWithDefault(Nullable.null, "Banana") // Banana
97-
Nullable.getWithDefault(Nulalble.make("Apple"), "Banana") // Apple
96+
Nullable.getOr(Nullable.null, "Banana") // Banana
97+
Nullable.getOr(Nulalble.make("Apple"), "Banana") // Apple
9898

9999
let greet = (firstName: option<string>) =>
100-
"Greetings " ++ firstName->Nullable.getWithDefault("Anonymous")
100+
"Greetings " ++ firstName->Nullable.getOr("Anonymous")
101101

102102
Nullable.make("Jane")->greet // "Greetings Jane"
103103
Nullable.null->greet // "Greetings Anonymous"
104104
```
105105
*/
106+
let getOr: (t<'a>, 'a) => 'a
107+
108+
@deprecated("Use getOr instead")
106109
let getWithDefault: (t<'a>, 'a) => 'a
107110

108111
/**
@@ -149,19 +152,22 @@ Nullable.map(undefined, x => x * x) // undefined
149152
let map: (t<'a>, 'a => 'b) => t<'b>
150153

151154
/**
152-
`mapWithDefault(value, default, f)` returns `f(value)` if `value` is not `null`
155+
`mapOr(value, default, f)` returns `f(value)` if `value` is not `null`
153156
or `undefined`, otherwise returns `default`.
154157

155158
## Examples
156159

157160
```rescript
158161
let someValue = Nullable.make(3)
159-
someValue->Nullable.mapWithDefault(0, x => x + 5) // 8
162+
someValue->Nullable.mapOr(0, x => x + 5) // 8
160163

161164
let noneValue = Nullable.null
162-
noneValue->Nullable.mapWithDefault(0, x => x + 5) // 0
165+
noneValue->Nullable.mapOr(0, x => x + 5) // 0
163166
```
164167
*/
168+
let mapOr: (t<'a>, 'b, 'a => 'b) => 'b
169+
170+
@deprecated("Use mapOr instead")
165171
let mapWithDefault: (t<'a>, 'b, 'a => 'b) => 'b
166172

167173
/**

src/Core__Option.mjs

+8-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function getExn(x) {
2929
};
3030
}
3131

32-
function mapWithDefault(opt, $$default, f) {
32+
function mapOr(opt, $$default, f) {
3333
var f$1 = Curry.__1(f);
3434
if (opt !== undefined) {
3535
return f$1(Caml_option.valFromOption(opt));
@@ -54,7 +54,7 @@ function flatMap(opt, f) {
5454

5555
}
5656

57-
function getWithDefault(opt, $$default) {
57+
function getOr(opt, $$default) {
5858
if (opt !== undefined) {
5959
return Caml_option.valFromOption(opt);
6060
} else {
@@ -104,13 +104,19 @@ function compare(a, b, cmp) {
104104
}
105105
}
106106

107+
var mapWithDefault = mapOr;
108+
109+
var getWithDefault = getOr;
110+
107111
export {
108112
filter ,
109113
forEach ,
110114
getExn ,
115+
mapOr ,
111116
mapWithDefault ,
112117
map ,
113118
flatMap ,
119+
getOr ,
114120
getWithDefault ,
115121
orElse ,
116122
isSome ,

src/Core__Option.res

+7-3
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ let getExn = x =>
4646

4747
external getUnsafe: option<'a> => 'a = "%identity"
4848

49-
let mapWithDefaultU = (opt, default, f) =>
49+
let mapOrU = (opt, default, f) =>
5050
switch opt {
5151
| Some(x) => f(. x)
5252
| None => default
5353
}
5454

55-
let mapWithDefault = (opt, default, f) => mapWithDefaultU(opt, default, (. x) => f(x))
55+
let mapOr = (opt, default, f) => mapOrU(opt, default, (. x) => f(x))
56+
57+
let mapWithDefault = mapOr
5658

5759
let mapU = (opt, f) =>
5860
switch opt {
@@ -70,12 +72,14 @@ let flatMapU = (opt, f) =>
7072

7173
let flatMap = (opt, f) => flatMapU(opt, (. x) => f(x))
7274

73-
let getWithDefault = (opt, default) =>
75+
let getOr = (opt, default) =>
7476
switch opt {
7577
| Some(x) => x
7678
| None => default
7779
}
7880

81+
let getWithDefault = getOr
82+
7983
let orElse = (opt, other) =>
8084
switch opt {
8185
| Some(_) as some => some

src/Core__Option.resi

+13-7
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,21 @@ Option.getUnsafe(None: option<int>) // Returns `undefined`, which is not a valid
9696
external getUnsafe: option<'a> => 'a = "%identity"
9797

9898
/**
99-
`mapWithDefault(opt, default, f)` returns `f(value)` if `opt` is `Some(value)`, otherwise `default`.
99+
`mapOr(opt, default, f)` returns `f(value)` if `opt` is `Some(value)`, otherwise `default`.
100100

101101
## Examples
102102

103103
```rescript
104104
let someValue = Some(3)
105-
someValue->Option.mapWithDefault(0, x => x + 5) // 8
105+
someValue->Option.mapOr(0, x => x + 5) // 8
106106

107107
let noneValue = None
108-
noneValue->Option.mapWithDefault(0, x => x + 5) // 0
108+
noneValue->Option.mapOr(0, x => x + 5) // 0
109109
```
110110
*/
111+
let mapOr: (option<'a>, 'b, 'a => 'b) => 'b
112+
113+
@deprecated("Use mapOr instead")
111114
let mapWithDefault: (option<'a>, 'b, 'a => 'b) => 'b
112115

113116
/**
@@ -143,21 +146,24 @@ Option.flatMap(None, addIfAboveOne) // None
143146
let flatMap: (option<'a>, 'a => option<'b>) => option<'b>
144147

145148
/**
146-
`getWithDefault(opt, default)` returns `value` if `opt` is `Some(value)`, otherwise `default`.
149+
`getOr(opt, default)` returns `value` if `opt` is `Some(value)`, otherwise `default`.
147150

148151
## Examples
149152

150153
```rescript
151-
Option.getWithDefault(None, "Banana") // Banana
152-
Option.getWithDefault(Some("Apple"), "Banana") // Apple
154+
Option.getOr(None, "Banana") // Banana
155+
Option.getOr(Some("Apple"), "Banana") // Apple
153156

154157
let greet = (firstName: option<string>) =>
155-
"Greetings " ++ firstName->Option.getWithDefault("Anonymous")
158+
"Greetings " ++ firstName->Option.getOr("Anonymous")
156159

157160
Some("Jane")->greet // "Greetings Jane"
158161
None->greet // "Greetings Anonymous"
159162
```
160163
*/
164+
let getOr: (option<'a>, 'a) => 'a
165+
166+
@deprecated("Use getOr instead")
161167
let getWithDefault: (option<'a>, 'a) => 'a
162168

163169
/**

0 commit comments

Comments
 (0)