Skip to content
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

Array.fromOneItem #109

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Next version

### API changes

- `Array.of1` https://github.com/rescript-association/rescript-core/pull/109

## 0.4.0

### API changes
Expand Down
2 changes: 2 additions & 0 deletions src/Core__Array.res
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,5 @@ let findMap = (arr, f) => {
}

@send external at: (array<'a>, int) => option<'a> = "at"

@val external of1: 'a => array<'a> = "Array.of"
21 changes: 21 additions & 0 deletions src/Core__Array.resi
Original file line number Diff line number Diff line change
Expand Up @@ -969,3 +969,24 @@ let findMap: (array<'a>, 'a => option<'b>) => option<'b>
*/
@send
external at: (array<'a>, int) => option<'a> = "at"

/**
`of1` generates a new array from a single value. See [Array.of on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of)

## Examples

```rescript
3->Array.of1 // [3]
undefined->Array.of1 // [undefined]
Some("abc")->Array.of1 // [Some("abc")]
[1,2,3]->Array.of1 // [[1,2,3]]
{"x": 3, "y": 5}->Array.of1 // Some({"x": 3, "y": 5})
```

## Specifications

- [Array.of MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of)
- [Array.of ECMAScript Language Specification](https://tc39.es/ecma262/multipage/indexed-collections.html#sec-array.of)
*/
@val
external of1: 'a => array<'a> = "Array.of"
59 changes: 59 additions & 0 deletions test/ArrayTests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,66 @@ Test.run([

})), eq, undefined);

function of1Test(a, title) {
Test.run([
[
"ArrayTests.res",
103,
22,
38
],
"of1 : " + title + ""
], Array.of(a), Caml_obj.equal, [a]);
Test.run([
[
"ArrayTests.res",
104,
22,
38
],
"of1 : " + title + ""
], Array.of(a).length, eq, 1);
Test.run([
[
"ArrayTests.res",
105,
22,
38
],
"of1 : " + title + ""
], Array.of(a)[0], (function (i, j) {
return i === j;
}), a);
}

var m = Array.of({
x: 3,
y: 5
});

of1Test(3, "Single integer");

of1Test("abc", "Single string");

of1Test(undefined, "undefined");

of1Test(null, "null");

of1Test([
1,
2,
3
], "full array of integers");

of1Test([], "empty array");

of1Test(5, "Some");

of1Test(undefined, "None");

export {
eq ,
of1Test ,
m ,
}
/* Not a pure module */
15 changes: 15 additions & 0 deletions test/ArrayTests.res
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,18 @@ Test.run(
eq,
None,
)

let of1Test = (a, title) => {
Test.run(__POS_OF__(`of1 : ${title}`), Array.of1(a), (i, j) => i == j, [a])
Test.run(__POS_OF__(`of1 : ${title}`), Array.of1(a)->Array.length, eq, 1)
Test.run(__POS_OF__(`of1 : ${title}`), Array.of1(a)->Array.getUnsafe(0), (i, j) => i === j, a)
}
let m = {"x": 3, "y": 5}->Array.of1
3->of1Test("Single integer")
"abc"->of1Test("Single string")
undefined->of1Test("undefined")
null->of1Test("null")
[1, 2, 3]->of1Test("full array of integers")
[]->of1Test("empty array")
Some(5)->of1Test("Some")
None->of1Test("None")
6 changes: 6 additions & 0 deletions test/TestSuite.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ var Concurrently = PromiseTest.Concurrently;

var panicTest = ErrorTests.panicTest;

var of1Test = ArrayTests.of1Test;

var m = ArrayTests.m;

var $$catch = IntTests.$$catch;

var forEachIfOkCallFunction = ResultTests.forEachIfOkCallFunction;
Expand Down Expand Up @@ -61,6 +65,8 @@ export {
Catching ,
Concurrently ,
panicTest ,
of1Test ,
m ,
$$catch ,
forEachIfOkCallFunction ,
forEachIfErrorDoNotCallFunction ,
Expand Down