Skip to content

Commit 0558d0c

Browse files
committed
refactor: WithDefault -> Or
1 parent a976a8c commit 0558d0c

12 files changed

+109
-45
lines changed

src/Core__Null.mjs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function fromOption(option) {
1111
}
1212
}
1313

14-
function getWithDefault(value, $$default) {
14+
function getOr(value, $$default) {
1515
if (value !== null) {
1616
return value;
1717
} else {
@@ -38,7 +38,7 @@ function map(value, f) {
3838
}
3939
}
4040

41-
function mapWithDefault(value, $$default, f) {
41+
function mapOr(value, $$default, f) {
4242
if (value !== null) {
4343
return Curry._1(f, value);
4444
} else {
@@ -54,11 +54,17 @@ function flatMap(value, f) {
5454
}
5555
}
5656

57+
var getWithDefault = getOr;
58+
59+
var mapWithDefault = mapOr;
60+
5761
export {
5862
fromOption ,
63+
getOr ,
5964
getWithDefault ,
6065
getExn ,
6166
map ,
67+
mapOr ,
6268
mapWithDefault ,
6369
flatMap ,
6470
}

src/Core__Null.res

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ let fromOption: option<'a> => t<'a> = option =>
1414
| None => null
1515
}
1616

17-
let getWithDefault = (value, default) =>
17+
let getOr = (value, default) =>
1818
switch value->toOption {
1919
| Some(x) => x
2020
| None => default
2121
}
2222

23+
let getWithDefault = getOr
24+
2325
let getExn: t<'a> => 'a = value =>
2426
switch value->toOption {
2527
| Some(x) => x
@@ -34,12 +36,14 @@ let map = (value, f) =>
3436
| None => null
3537
}
3638

37-
let mapWithDefault = (value, default, f) =>
39+
let mapOr = (value, default, f) =>
3840
switch value->toOption {
3941
| Some(x) => f(x)
4042
| None => default
4143
}
4244

45+
let mapWithDefault = mapOr
46+
4347
let flatMap = (value, f) =>
4448
switch value->toOption {
4549
| Some(x) => f(x)

src/Core__Null.resi

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,25 @@ Console.log(asNull == null) // Logs `true` to the console.
7373
let fromOption: option<'a> => t<'a>
7474

7575
/**
76-
`getWithDefault(value, default)` returns `value` if not `null`, otherwise return
76+
`getOr(value, default)` returns `value` if not `null`, otherwise return
7777
`default`.
7878

7979
## Examples
8080

8181
```rescript
82-
Null.getWithDefault(null, "Banana") // Banana
83-
Null.getWithDefault(Nulalble.make("Apple"), "Banana") // Apple
82+
Null.getOr(null, "Banana") // Banana
83+
Null.getOr(Nulalble.make("Apple"), "Banana") // Apple
8484

8585
let greet = (firstName: option<string>) =>
86-
"Greetings " ++ firstName->Null.getWithDefault("Anonymous")
86+
"Greetings " ++ firstName->Null.getOr("Anonymous")
8787

8888
Null.make("Jane")->greet // "Greetings Jane"
8989
null->greet // "Greetings Anonymous"
9090
```
9191
*/
92+
let getOr: (t<'a>, 'a) => 'a
93+
94+
@deprecated("Use getOr instead")
9295
let getWithDefault: (t<'a>, 'a) => 'a
9396

9497
/**
@@ -135,19 +138,22 @@ Null.map(null, x => x * x) // null
135138
let map: (t<'a>, 'a => 'b) => t<'b>
136139

137140
/**
138-
`mapWithDefault(value, default, f)` returns `f(value)` if `value` is not `null`,
141+
`mapOr(value, default, f)` returns `f(value)` if `value` is not `null`,
139142
otherwise returns `default`.
140143

141144
## Examples
142145

143146
```rescript
144147
let someValue = Null.make(3)
145-
someValue->Null.mapWithDefault(0, x => x + 5) // 8
148+
someValue->Null.mapOr(0, x => x + 5) // 8
146149

147150
let noneValue = null
148-
noneValue->Null.mapWithDefault(0, x => x + 5) // 0
151+
noneValue->Null.mapOr(0, x => x + 5) // 0
149152
```
150153
*/
154+
let mapOr: (t<'a>, 'b, 'a => 'b) => 'b
155+
156+
@deprecated("Use mapOr instead")
151157
let mapWithDefault: (t<'a>, 'b, 'a => 'b) => 'b
152158

153159
/**

src/Core__Nullable.mjs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function fromOption(option) {
1010

1111
}
1212

13-
function getWithDefault(value, $$default) {
13+
function getOr(value, $$default) {
1414
if (value == null) {
1515
return $$default;
1616
} else {
@@ -37,7 +37,7 @@ function map(value, f) {
3737
}
3838
}
3939

40-
function mapWithDefault(value, $$default, f) {
40+
function mapOr(value, $$default, f) {
4141
if (value == null) {
4242
return $$default;
4343
} else {
@@ -53,11 +53,17 @@ function flatMap(value, f) {
5353
}
5454
}
5555

56+
var getWithDefault = getOr;
57+
58+
var mapWithDefault = mapOr;
59+
5660
export {
5761
fromOption ,
62+
getOr ,
5863
getWithDefault ,
5964
getExn ,
6065
map ,
66+
mapOr ,
6167
mapWithDefault ,
6268
flatMap ,
6369
}

src/Core__Nullable.res

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ let fromOption: option<'a> => t<'a> = option =>
1414
| None => undefined
1515
}
1616

17-
let getWithDefault = (value, default) =>
17+
let getOr = (value, default) =>
1818
switch value->toOption {
1919
| Some(x) => x
2020
| None => default
2121
}
2222

23+
let getWithDefault = getOr
24+
2325
let getExn: t<'a> => 'a = value =>
2426
switch value->toOption {
2527
| Some(x) => x
@@ -34,12 +36,14 @@ let map = (value, f) =>
3436
| None => Obj.magic(value)
3537
}
3638

37-
let mapWithDefault = (value, default, f) =>
39+
let mapOr = (value, default, f) =>
3840
switch value->toOption {
3941
| Some(x) => f(x)
4042
| None => default
4143
}
4244

45+
let mapWithDefault = mapOr
46+
4347
let flatMap = (value, f) =>
4448
switch value->toOption {
4549
| Some(x) => f(x)

src/Core__Nullable.resi

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,25 @@ let asNullable = optString->Nullable.fromOption // Nullable.t<string>
8383
let fromOption: option<'a> => t<'a>
8484

8585
/**
86-
`getWithDefault(value, default)` returns `value` if not `null` or `undefined`,
86+
`getOr(value, default)` returns `value` if not `null` or `undefined`,
8787
otherwise return `default`.
8888

8989
## Examples
9090

9191
```rescript
92-
Nullable.getWithDefault(Nullable.null, "Banana") // Banana
93-
Nullable.getWithDefault(Nulalble.make("Apple"), "Banana") // Apple
92+
Nullable.getOr(Nullable.null, "Banana") // Banana
93+
Nullable.getOr(Nulalble.make("Apple"), "Banana") // Apple
9494

9595
let greet = (firstName: option<string>) =>
96-
"Greetings " ++ firstName->Nullable.getWithDefault("Anonymous")
96+
"Greetings " ++ firstName->Nullable.getOr("Anonymous")
9797

9898
Nullable.make("Jane")->greet // "Greetings Jane"
9999
Nullable.null->greet // "Greetings Anonymous"
100100
```
101101
*/
102+
let getOr: (t<'a>, 'a) => 'a
103+
104+
@deprecated("Use getOr instead")
102105
let getWithDefault: (t<'a>, 'a) => 'a
103106

104107
/**
@@ -145,19 +148,22 @@ Nullable.map(undefined, x => x * x) // undefined
145148
let map: (t<'a>, 'a => 'b) => t<'b>
146149

147150
/**
148-
`mapWithDefault(value, default, f)` returns `f(value)` if `value` is not `null`
151+
`mapOr(value, default, f)` returns `f(value)` if `value` is not `null`
149152
or `undefined`, otherwise returns `default`.
150153

151154
## Examples
152155

153156
```rescript
154157
let someValue = Nullable.make(3)
155-
someValue->Nullable.mapWithDefault(0, x => x + 5) // 8
158+
someValue->Nullable.mapOr(0, x => x + 5) // 8
156159

157160
let noneValue = Nullable.null
158-
noneValue->Nullable.mapWithDefault(0, x => x + 5) // 0
161+
noneValue->Nullable.mapOr(0, x => x + 5) // 0
159162
```
160163
*/
164+
let mapOr: (t<'a>, 'b, 'a => 'b) => 'b
165+
166+
@deprecated("Use mapOr instead")
161167
let mapWithDefault: (t<'a>, 'b, 'a => 'b) => 'b
162168

163169
/**

src/Core__Option.mjs

Lines changed: 8 additions & 2 deletions
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 {
@@ -106,13 +106,19 @@ function cmp(a, b, f) {
106106
}
107107
}
108108

109+
var mapWithDefault = mapOr;
110+
111+
var getWithDefault = getOr;
112+
109113
export {
110114
filter ,
111115
forEach ,
112116
getExn ,
117+
mapOr ,
113118
mapWithDefault ,
114119
map ,
115120
flatMap ,
121+
getOr ,
116122
getWithDefault ,
117123
orElse ,
118124
isSome ,

src/Core__Option.res

Lines changed: 7 additions & 3 deletions
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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,21 @@ Option.getUnsafe(None) // Raises an error
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)