Skip to content

Commit 6cc1573

Browse files
committed
Merge branch 'main' into doc-test-js-outuput
2 parents 4a1beeb + 0cc3d8e commit 6cc1573

14 files changed

+135
-13
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
## Next version
44

55
- Added `assert_eq` function. https://github.com/rescript-association/rescript-core/pull/210
6+
- Add `RegExp.setLastIndex` function. https://github.com/rescript-association/rescript-core/pull/219
7+
- Add `Nullable.isNullable` function. https://github.com/rescript-association/rescript-core/pull/227
8+
- Remove some deps to Belt, Pervasives and Js. https://github.com/rescript-association/rescript-core/pull/226/commits
69

710
## 1.3.0
811

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ Open it so it's available in the global scope.
7070
}
7171
```
7272

73+
## ESM and CJS
74+
To use this library with CommonJS modules you should set the file extension in your project's `rescript.json` to `.res.js`, `.js`, `.res.cjs`, or `.cjs`. This will work with older versions of Node and most web projects using a bundler.
75+
76+
To use this library with EcmaScript modules you should set the file extension in your project's `rescript.json` to `.res.mjs`, or `.mjs`.
77+
This will work with newer versions of Node and web projects using a modern bundler like Vite.
78+
7379
## What it looks like
7480

7581
All relevant standard library modules are designed to be available directly in

src/Core__Array.mjs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

3-
import * as Js_math from "rescript/lib/es6/js_math.js";
43
import * as Caml_option from "rescript/lib/es6/caml_option.js";
54

65
function make(length, x) {
@@ -113,10 +112,14 @@ function swapUnsafe(xs, i, j) {
113112
xs[j] = tmp;
114113
}
115114

115+
function random_int(min, max) {
116+
return (Math.floor(Math.random() * (max - min | 0)) | 0) + min | 0;
117+
}
118+
116119
function shuffle(xs) {
117120
var len = xs.length;
118121
for(var i = 0; i < len; ++i){
119-
swapUnsafe(xs, i, Js_math.random_int(i, len));
122+
swapUnsafe(xs, i, random_int(i, len));
120123
}
121124
}
122125

src/Core__Array.res

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,20 @@ let swapUnsafe = (xs, i, j) => {
203203
setUnsafe(xs, j, tmp)
204204
}
205205

206+
module M = {
207+
@val external floor: float => float = "Math.floor"
208+
@val external random: unit => float = "Math.random"
209+
external fromFloat: float => int = "%intoffloat"
210+
external toFloat: int => float = "%identity"
211+
212+
let random_int: (int, int) => int = (min, max) =>
213+
floor(random() *. toFloat(max - min))->fromFloat + min
214+
}
215+
206216
let shuffle = xs => {
207217
let len = length(xs)
208218
for i in 0 to len - 1 {
209-
swapUnsafe(xs, i, Js.Math.random_int(i, len)) /* [i,len) */
219+
swapUnsafe(xs, i, M.random_int(i, len)) /* [i,len) */
210220
}
211221
}
212222

src/Core__Int.mjs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

33
import * as Core__Array from "./Core__Array.mjs";
4-
import * as PervasivesU from "rescript/lib/es6/pervasivesU.js";
54

65
function equal(a, b) {
76
return a === b;
@@ -26,6 +25,14 @@ function fromString(x, radix) {
2625
}
2726
}
2827

28+
function abs(x) {
29+
if (x >= 0) {
30+
return x;
31+
} else {
32+
return -x | 0;
33+
}
34+
}
35+
2936
function range(start, end, optionsOpt) {
3037
var options = optionsOpt !== undefined ? optionsOpt : ({});
3138
var isInverted = start > end;
@@ -51,7 +58,7 @@ function range(start, end, optionsOpt) {
5158
} else {
5259
var range$1 = isInverted ? start - end | 0 : end - start | 0;
5360
var range$2 = options.inclusive === true ? range$1 + 1 | 0 : range$1;
54-
length = Math.ceil(range$2 / PervasivesU.abs(step)) | 0;
61+
length = Math.ceil(range$2 / abs(step)) | 0;
5562
}
5663
return Core__Array.fromInitializer(length, (function (i) {
5764
return start + Math.imul(i, step) | 0;

src/Core__Int.res

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ external mod: (int, int) => int = "%modint"
4848

4949
type rangeOptions = {step?: int, inclusive?: bool}
5050

51+
let abs = x =>
52+
if x >= 0 {
53+
x
54+
} else {
55+
-x
56+
}
57+
5158
let range = (start, end, ~options: rangeOptions={}) => {
5259
let isInverted = start > end
5360

src/Core__List.mjs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

3-
import * as Belt_Array from "rescript/lib/es6/belt_Array.js";
43
import * as Caml_option from "rescript/lib/es6/caml_option.js";
54
import * as Core__Array from "./Core__Array.mjs";
65

@@ -794,7 +793,12 @@ function reduceReverse(l, acc, f) {
794793
if (len < 1000) {
795794
return reduceReverseUnsafe(l, acc, f);
796795
} else {
797-
return Belt_Array.reduceReverseU(toArray(l), acc, f);
796+
var a = toArray(l);
797+
var r = acc;
798+
for(var i = a.length - 1 | 0; i >= 0; --i){
799+
r = f(r, a[i]);
800+
}
801+
return r;
798802
}
799803
}
800804

@@ -888,7 +892,14 @@ function reduceReverse2(l1, l2, acc, f) {
888892
if (len < 1000) {
889893
return reduceReverse2Unsafe(l1, l2, acc, f);
890894
} else {
891-
return Belt_Array.reduceReverse2U(toArray(l1), toArray(l2), acc, f);
895+
var a = toArray(l1);
896+
var b = toArray(l2);
897+
var r = acc;
898+
var len$1 = a.length < b.length ? a.length : b.length;
899+
for(var i = len$1 - 1 | 0; i >= 0; --i){
900+
r = f(r, a[i], b[i]);
901+
}
902+
return r;
892903
}
893904
}
894905

src/Core__List.res

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,26 @@
6363

6464
type t<'a> = list<'a>
6565

66-
// TODO: This module should be inlined eventually, if we end up removing Belt
67-
// from the compiler.
6866
module A = {
69-
let makeUninitializedUnsafe = Belt_Array.makeUninitializedUnsafe
70-
let reduceReverseU = Belt_Array.reduceReverseU
71-
let reduceReverse2U = Belt_Array.reduceReverse2U
67+
@new external makeUninitializedUnsafe: int => array<'a> = "Array"
68+
external min: ('a, 'a) => 'a = "%bs_min"
69+
70+
let reduceReverseU = (a, x, f) => {
71+
let r = ref(x)
72+
for i in Core__Array.length(a) - 1 downto 0 {
73+
r.contents = f(r.contents, Core__Array.getUnsafe(a, i))
74+
}
75+
r.contents
76+
}
77+
78+
let reduceReverse2U = (a, b, x, f) => {
79+
let r = ref(x)
80+
let len = min(Core__Array.length(a), Core__Array.length(b))
81+
for i in len - 1 downto 0 {
82+
r.contents = f(r.contents, Core__Array.getUnsafe(a, i), Core__Array.getUnsafe(b, i))
83+
}
84+
r.contents
85+
}
7286
}
7387

7488
external mutableCell: ('a, t<'a>) => t<'a> = "#makemutablelist"

src/Core__Nullable.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ external null: t<'a> = "#null"
77

88
external undefined: t<'a> = "#undefined"
99

10+
external isNullable: t<'a> => bool = "#is_nullable"
11+
1012
external make: 'a => t<'a> = "%identity"
1113

1214
external toOption: t<'a> => option<'a> = "#nullable_to_opt"

src/Core__Nullable.resi

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,27 @@ Console.log(undefined) // Logs `undefined` to the console.
3838
*/
3939
external undefined: t<'a> = "#undefined"
4040

41+
/**
42+
`isNullable(a)` returns `true` if `a` is null or undefined, `false` otherwise.
43+
44+
## Examples
45+
46+
```rescript
47+
let myStr = "Hello"
48+
let asNullable = myStr->Nullable.make
49+
50+
// Can't do the below because we're now forced to check for nullability
51+
// myStr == asNullable
52+
53+
// Check if asNullable is not null or undefined
54+
switch asNullable->Nullable.isNullable {
55+
| true => assert(false)
56+
| false => assert(true)
57+
}
58+
```
59+
*/
60+
external isNullable: t<'a> => bool = "#is_nullable"
61+
4162
/**
4263
Creates a new nullable value from the provided value.
4364
This means the compiler will enforce null checks for the new value.

src/Core__RegExp.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module Result = {
1515
@return(nullable) @send external exec: (t, string) => option<Result.t> = "exec"
1616

1717
@get external lastIndex: t => int = "lastIndex"
18+
@set external setLastIndex: (t, int) => unit = "lastIndex"
1819
@get external ignoreCase: t => bool = "ignoreCase"
1920
@get external global: t => bool = "global"
2021
@get external multiline: t => bool = "multiline"

src/Core__RegExp.resi

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,26 @@ Console.log(regexp->RegExp.lastIndex) // Logs `4` to the console
170170
@get
171171
external lastIndex: t => int = "lastIndex"
172172

173+
/**
174+
`setLastIndex(regexp, index)` set the index the next match will start from.
175+
176+
See [`RegExp.lastIndex`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex) on MDN.
177+
178+
## Examples
179+
```rescript
180+
// Match the first word in a sentence
181+
let regexp = RegExp.fromString("\\w+")
182+
let someStr = "Many words here."
183+
184+
regexp->RegExp.setLastIndex(4)
185+
regexp->RegExp.exec(someStr)->ignore
186+
187+
Console.log(regexp->RegExp.lastIndex) // Logs `10` to the console
188+
```
189+
*/
190+
@set
191+
external setLastIndex: (t, int) => unit = "lastIndex"
192+
173193
/**
174194
`ignoreCase(regexp)` returns whether the ignore case (`i`) flag is set on this `RegExp`.
175195

src/Core__String.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ let compare = (a: string, b: string) =>
1313

1414
@get external length: string => int = "length"
1515
@get_index external get: (string, int) => option<string> = ""
16+
@get_index external getUnsafe: (string, int) => string = ""
1617
@send external charAt: (string, int) => string = "charAt"
1718

1819
@send external charCodeAt: (string, int) => float = "charCodeAt"

src/Core__String.resi

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,22 @@ String.get(`JS`, 4) == None
150150
@get_index
151151
external get: (string, int) => option<string> = ""
152152

153+
/**
154+
`getUnsafe(str, index)` returns an `string` at the given `index` number.
155+
156+
This is _unsafe_, meaning it will return `undefined` value if `index` does not exist in `string`.
157+
158+
Use `String.getUnsafe` only when you are sure the `index` exists.
159+
## Examples
160+
161+
```rescript
162+
String.getUnsafe("ReScript", 0) == "R"
163+
String.getUnsafe("Hello", 4) == "o"
164+
```
165+
*/
166+
@get_index
167+
external getUnsafe: (string, int) => string = ""
168+
153169
/**
154170
`charAt(str, index)` gets the character at `index` within string `str`. If
155171
`index` is negative or greater than the length of `str`, it returns the empty

0 commit comments

Comments
 (0)