Skip to content

Object.get documentation and tests #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
getSymbol docs and tests
  • Loading branch information
jmagaram committed Mar 20, 2023
commit b4b3fda93acc2a2186feb7a35ffbca62db075ab0
15 changes: 14 additions & 1 deletion src/Core__Object.res
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,20 @@
*/
@get_index
external get: ({..}, string) => option<'a> = ""
@get_index external getSymbol: ({..}, Core__Symbol.t) => option<'a> = ""
/**
`getSymbol` gets the value of a property by symbol. Returns `None` if the property does not exist or has the value `undefined`. Otherwise returns `Some`, including if the value is `null`.

## Examples

```rescript
let fruit = Symbol.make("fruit")
let x = Object.empty()
x->Object.setSymbol(fruit, "banana")
x->Object.getSymbol(fruit) // Some("banana")
```
*/
@get_index
external getSymbol: ({..}, Core__Symbol.t) => option<'a> = ""
@get_index external getSymbolUnsafe: ({..}, Core__Symbol.t) => 'a = ""

@set_index external set: ({..}, string, 'a) => unit = ""
Expand Down
29 changes: 29 additions & 0 deletions test/ObjectTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,37 @@ runGetTest({
]
});

function getSymbolTestWhenExists(param) {
var obj = {};
var fruit = Symbol("fruit");
obj[fruit] = "banana";
var retrieved = obj[fruit];
Test.run([
[
"ObjectTests.res",
75,
15,
63
],
"Object.getSymbol when exists return it as Some"
], retrieved, eq, "banana");
}

getSymbolTestWhenExists(undefined);

Test.run([
[
"ObjectTests.res",
84,
13,
65
],
"Object.getSymbol when not exists return it as None"
], ({})[Symbol("fruit")], eq, undefined);

export {
eq ,
runGetTest ,
getSymbolTestWhenExists ,
}
/* Not a pure module */
23 changes: 23 additions & 0 deletions test/ObjectTests.res
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,26 @@ let runGetTest = i =>
// get: i => i->Object.get("a")->Option.map(i => i->Array.concat([4, 5]))->Option.getWithDefault([]),
// expected: [],
// }->runGetTest

// ===== getSymbol =====

let getSymbolTestWhenExists = () => {
let obj = Object.empty()
let fruit = Symbol.make("fruit")
obj->Object.setSymbol(fruit, "banana")
let retrieved = obj->Object.getSymbol(fruit)
Test.run(
__POS_OF__(`Object.getSymbol when exists return it as Some`),
retrieved,
eq,
Some("banana"),
)
}
getSymbolTestWhenExists()

Test.run(
__POS_OF__(`Object.getSymbol when not exists return it as None`),
Object.empty()->Object.getSymbol(Symbol.make("fruit")),
eq,
None,
)
3 changes: 3 additions & 0 deletions test/TestSuite.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ var eq = ObjectTests.eq;

var runGetTest = ObjectTests.runGetTest;

var getSymbolTestWhenExists = ObjectTests.getSymbolTestWhenExists;

export {
bign ,
TestError ,
Expand All @@ -47,5 +49,6 @@ export {
$$catch ,
eq ,
runGetTest ,
getSymbolTestWhenExists ,
}
/* IntTests Not a pure module */