Skip to content

Update Js.String[2].match_ return type #559

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

Merged
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ node_modules/
.next/
index_data/*.json

# Generated via test examples script
_tempFile.cmi
_tempFile.cmj
_tempFile.cmt

# these docs are checked in, but we consider them frozen.
pages/docs/manual/v8.0.0/
pages/docs/manual/v9.0.0/
Expand Down
19 changes: 19 additions & 0 deletions compilers/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion compilers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dependencies": {
"rescript-820": "npm:[email protected]",
"rescript-902": "npm:[email protected]",
"rescript-912": "npm:[email protected]"
"rescript-912": "npm:[email protected]",
"rescript-1000": "npm:[email protected]"
}
}
2 changes: 1 addition & 1 deletion pages/docs/manual/latest/api/belt/range.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ equivalent to `Belt.Array.(forEach(range(start, finish), action))`
```res example
Belt.Range.forEach(0, 4, (i) => Js.log(i))

/**
/*
* prints:
* 0
* 1
Expand Down
11 changes: 6 additions & 5 deletions pages/docs/manual/latest/api/js/string-2.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -315,19 +315,20 @@ Js.String2.localeCompare("CAT", "cat") > 0.0
## match

```res sig
let match_: (t, Js_re.t) => option<array<t>>
let match_: (t, Js_re.t) => option<array<option<t>>>
```

`match(str, regexp)` matches a `string` against the given `regexp`. If there is no match, it returns `None`. For regular expressions without the g modifier, if there is a match, the return value is `Some(array)` where the array contains:
- The entire matched string
- Any capture groups if the regexp had parentheses
For regular expressions with the g modifier, a matched expression returns `Some(array)` with all the matched substrings and no capture groups. See [`String.match`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match) on MDN.

For regular expressions with the g modifier, a matched expression returns `Some(array)` with all the matched substrings and no capture groups. Javscript String.prototype.match can return `undefined` for optional capture groups that are not found, thus the element of the returned array is typed `option<t>`. See [`String.match`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match) on MDN.

```res example
Js.String2.match_("The better bats", %re("/b[aeiou]t/")) == Some(["bet"])
Js.String2.match_("The better bats", %re("/b[aeiou]t/g")) == Some(["bet", "bat"])
Js.String2.match_("The better bats", %re("/b[aeiou]t/")) == Some([Some("bet")])
Js.String2.match_("The better bats", %re("/b[aeiou]t/g")) == Some([Some("bet"), Some("bat")])
Js.String2.match_("Today is 2018-04-05.", %re("/(\d+)-(\d+)-(\d+)/")) ==
Some(["2018-04-05", "2018", "04", "05"])
Some([Some("2018-04-05"), Some("2018"), Some("04"), Some("05")])
Js.String2.match_("The large container.", %re("/b[aeiou]g/")) == None
```

Expand Down
11 changes: 6 additions & 5 deletions pages/docs/manual/latest/api/js/string.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -315,19 +315,20 @@ Js.String.localeCompare("cat", "CAT") > 0.0
## match

```res sig
let match_: (Js_re.t, t) => option<array<t>>
let match_: (Js_re.t, t) => option<array<option<t>>>
```

`match(regexp, str)` matches a `string` against the given `regexp`. If there is no match, it returns `None`. For regular expressions without the g modifier, if there is a match, the return value is `Some(array)` where the array contains:
- The entire matched string
- Any capture groups if the regexp had parentheses
For regular expressions with the g modifier, a matched expression returns `Some(array)` with all the matched substrings and no capture groups. See [`String.match`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match) on MDN.

For regular expressions with the g modifier, a matched expression returns `Some(array)` with all the matched substrings and no capture groups. Javscript String.prototype.match can return `undefined` for optional capture groups that are not found, thus the element of the returned array is typed `option<t>`. See [`String.match`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match) on MDN.

```res example
Js.String.match_(%re("/b[aeiou]t/"), "The better bats") == Some(["bet"])
Js.String.match_(%re("/b[aeiou]t/g"), "The better bats") == Some(["bet", "bat"])
Js.String.match_(%re("/b[aeiou]t/"), "The better bats") == Some([Some("bet")])
Js.String.match_(%re("/b[aeiou]t/g"), "The better bats") == Some([Some("bet"), Some("bat")])
Js.String.match_(%re("/(\d+)-(\d+)-(\d+)/"), "Today is 2018-04-05.") ==
Some(["2018-04-05", "2018", "04", "05"])
Some([Some("2018-04-05"), Some("2018"), Some("04"), Some("05")])
Js.String.match_(%re("/b[aeiou]g/"), "The large container.") == None
```

Expand Down
4 changes: 2 additions & 2 deletions scripts/test-examples.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let tempFileNameRegex = /_tempFile\.res/g
// see the package.json on how to define another rescript version
let compilersDir = path.join(__dirname, "..", "compilers")

let bsc = path.join(compilersDir, 'node_modules', 'rescript-912', process.platform, 'bsc.exe')
let bsc = path.join(compilersDir, 'node_modules', 'rescript-1000', process.platform, 'bsc.exe')

const prepareCompilers = () => {
if (fs.existsSync(bsc)) {
Expand Down Expand Up @@ -82,7 +82,7 @@ glob.sync(__dirname + '/../pages/docs/manual/latest/**/*.mdx').forEach((file) =>
try {
// -109 for suppressing `Toplevel expression is expected to have unit type.`
// Most doc snippets do e.g. `Belt.Array.length(["test"])`, which triggers this
child_process.execFileSync(bsc, ['-i', tempFileName, '-w', '-109'], {stdio: 'pipe'})
child_process.execFileSync(bsc, [tempFileName, '-w', '-109'], {stdio: 'pipe'})
} catch (e) {
process.stdout.write(postprocessOutput(file, e))
success = false
Expand Down