Skip to content

Commit a34df2a

Browse files
committed
Object.get documentation and tests
1 parent 7c2f372 commit a34df2a

File tree

5 files changed

+207
-4
lines changed

5 files changed

+207
-4
lines changed

src/Core__Object.res

+15-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,21 @@
1111
@variadic @val external assignMany: ({..}, array<{..}>) => {..} = "Object.assign"
1212
@val external copy: (@as(json`{}`) _, {..}) => {..} = "Object.assign"
1313

14-
@get_index external get: ({..}, string) => option<'a> = ""
14+
/**
15+
`get` gets the value of a property by name. Returns `None` if the property does not exist or has the value `undefined`. Otherwise returns `Some`, including if the value is `null`.
16+
17+
## Examples
18+
19+
```rescript
20+
{"a": 1}->Object.get("a") // Some(1)
21+
{"a": 1}->Object.get("b") // None
22+
{"a": undefined}->Object.get("a") // None
23+
{"a": null}->Object.get("a") // Some(null)
24+
{"a": 1}->Object.get("toString")->Option.isSome // true
25+
```
26+
*/
27+
@get_index
28+
external get: ({..}, string) => option<'a> = ""
1529
@get_index external getSymbol: ({..}, Core__Symbol.t) => option<'a> = ""
1630
@get_index external getSymbolUnsafe: ({..}, Core__Symbol.t) => 'a = ""
1731

test/ObjectTests.mjs

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Test from "./Test.mjs";
4+
import * as Curry from "rescript/lib/es6/curry.js";
5+
import * as Caml_obj from "rescript/lib/es6/caml_obj.js";
6+
import * as Core__Option from "../src/Core__Option.mjs";
7+
8+
var eq = Caml_obj.equal;
9+
10+
function runGetTest(i) {
11+
Test.run([
12+
[
13+
"ObjectTests.res",
14+
15,
15+
22,
16+
46
17+
],
18+
"Object.get: " + i.title + ""
19+
], Curry._1(i.get, Curry._1(i.source, undefined)), eq, i.expected);
20+
}
21+
22+
runGetTest({
23+
title: "prop exists, return Some",
24+
source: (function (param) {
25+
return {
26+
a: 1
27+
};
28+
}),
29+
get: (function (__x) {
30+
return __x["a"];
31+
}),
32+
expected: 1
33+
});
34+
35+
runGetTest({
36+
title: "prop NOT exist, return None",
37+
source: (function (param) {
38+
return {
39+
a: 1
40+
};
41+
}),
42+
get: (function (i) {
43+
return i["banana"];
44+
}),
45+
expected: undefined
46+
});
47+
48+
runGetTest({
49+
title: "prop like toString, return Some",
50+
source: (function (param) {
51+
return {
52+
a: 1
53+
};
54+
}),
55+
get: (function (i) {
56+
return Core__Option.isSome(i["toString"]);
57+
}),
58+
expected: true
59+
});
60+
61+
runGetTest({
62+
title: "prop exist but explicitly undefined, return None",
63+
source: (function (param) {
64+
return {
65+
a: undefined
66+
};
67+
}),
68+
get: (function (i) {
69+
return i["a"];
70+
}),
71+
expected: undefined
72+
});
73+
74+
runGetTest({
75+
title: "prop exist but explicitly null, return None",
76+
source: (function (param) {
77+
return {
78+
a: null
79+
};
80+
}),
81+
get: (function (i) {
82+
return i["a"];
83+
}),
84+
expected: null
85+
});
86+
87+
runGetTest({
88+
title: "prop exists and is an array, can get it",
89+
source: (function (param) {
90+
return {
91+
a: [
92+
1,
93+
2,
94+
3
95+
]
96+
};
97+
}),
98+
get: (function (i) {
99+
return Core__Option.getWithDefault(Core__Option.map(i["a"], (function (i) {
100+
return i.concat([
101+
4,
102+
5
103+
]);
104+
})), []);
105+
}),
106+
expected: [
107+
1,
108+
2,
109+
3,
110+
4,
111+
5
112+
]
113+
});
114+
115+
export {
116+
eq ,
117+
runGetTest ,
118+
}
119+
/* Not a pure module */

test/ObjectTests.res

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
open RescriptCore
2+
3+
let eq = (a, b) => a == b
4+
5+
// ===== get =====
6+
7+
type getTestData<'obj, 'res, 'expected> = {
8+
title: string,
9+
source: unit => 'obj,
10+
get: 'obj => 'res,
11+
expected: 'expected,
12+
}
13+
14+
let runGetTest = i =>
15+
Test.run(__POS_OF__(`Object.get: ${i.title}`), i.source()->i.get, eq, i.expected)
16+
17+
{
18+
title: "prop exists, return Some",
19+
source: () => {"a": 1},
20+
get: Object.get(_, "a"),
21+
expected: Some(1),
22+
}->runGetTest
23+
24+
{
25+
title: "prop NOT exist, return None",
26+
source: () => {"a": 1},
27+
get: i => i->Object.get("banana"),
28+
expected: None,
29+
}->runGetTest
30+
31+
{
32+
title: "prop like toString, return Some",
33+
source: () => {"a": 1},
34+
get: i => i->Object.get("toString")->Option.isSome,
35+
expected: true,
36+
}->runGetTest
37+
38+
{
39+
title: "prop exist but explicitly undefined, return None",
40+
source: () => {"a": undefined},
41+
get: i => i->Object.get("a"),
42+
expected: None,
43+
}->runGetTest
44+
45+
{
46+
title: "prop exist but explicitly null, return None",
47+
source: () => {"a": null},
48+
get: i => i->Object.get("a"),
49+
expected: Some(null),
50+
}->runGetTest
51+
52+
{
53+
title: "prop exists and is an array, can get it",
54+
source: () => {"a": [1, 2, 3]},
55+
get: i => i->Object.get("a")->Option.map(i => i->Array.concat([4, 5]))->Option.getWithDefault([]),
56+
expected: [1, 2, 3, 4, 5],
57+
}->runGetTest
58+
59+
// This throws an exception
60+
// {
61+
// title: "prop exists but casted wrong on get",
62+
// source: () => {"a": 34},
63+
// get: i => i->Object.get("a")->Option.map(i => i->Array.concat([4, 5]))->Option.getWithDefault([]),
64+
// expected: [],
65+
// }->runGetTest

test/TestSuite.mjs

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as IntTests from "./IntTests.mjs";
44
import * as TestTests from "./TestTests.mjs";
55
import * as ArrayTests from "./ArrayTests.mjs";
66
import * as ErrorTests from "./ErrorTests.mjs";
7+
import * as ObjectTests from "./ObjectTests.mjs";
78
import * as PromiseTest from "./PromiseTest.mjs";
89

910
var bign = TestTests.bign;
@@ -26,10 +27,12 @@ var Concurrently = PromiseTest.Concurrently;
2627

2728
var panicTest = ErrorTests.panicTest;
2829

29-
var eq = IntTests.eq;
30-
3130
var $$catch = IntTests.$$catch;
3231

32+
var eq = ObjectTests.eq;
33+
34+
var runGetTest = ObjectTests.runGetTest;
35+
3336
export {
3437
bign ,
3538
TestError ,
@@ -41,7 +44,8 @@ export {
4144
Catching ,
4245
Concurrently ,
4346
panicTest ,
44-
eq ,
4547
$$catch ,
48+
eq ,
49+
runGetTest ,
4650
}
4751
/* IntTests Not a pure module */

test/TestSuite.res

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ include PromiseTest
33
include ErrorTests
44
include ArrayTests
55
include IntTests
6+
include ObjectTests

0 commit comments

Comments
 (0)