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

Rescript core #22

Merged
merged 5 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 7 additions & 1 deletion CHANGELOG.MD → CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.4.0

### Changed

* Upgraded to ReScript v11.1 and using `@rescript/core`

## 0.3.0

### Breaking changes
Expand All @@ -19,4 +25,4 @@
* `Decode.childElements` decoder
* `Decode.requireSome` helper function
* `Decode.ok` decoder
* `Decode.error` decoder
* `Decode.error` decoder
6 changes: 5 additions & 1 deletion bsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
},
"suffix": ".bs.js",
"bs-dependencies": [
"rescript-webapi"
"rescript-webapi",
"@rescript/core"
],
"bsc-flags": [
"-open RescriptCore"
],
"warnings": {
"error": "+101"
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
},
"dependencies": {
"rescript": "^11.1.2",
"rescript-webapi": "^0.9.1"
"rescript-webapi": "^0.9.1",
"@rescript/core": "1.5.2"
}
}
23 changes: 11 additions & 12 deletions src/Xml_Decode.res
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
open Belt
open Webapi

type decoder<'a> = Xml_Element.t => 'a
Expand Down Expand Up @@ -65,8 +64,8 @@ let requireNamespace = (element: Xml_Element.t, namespace: option<string>) => {
raise(
DecodeError(
"namespace '" ++
(namespace->Belt.Option.getWithDefault("") ++
("' expected, got '" ++ (element->namespaceURI->Belt.Option.getWithDefault("") ++ "'"))),
(namespace->Option.getOr("") ++
("' expected, got '" ++ (element->namespaceURI->Option.getOr("") ++ "'"))),
),
)
}
Expand Down Expand Up @@ -126,9 +125,9 @@ let select = (~namespace as targetNamespace=?, targetName, element) =>
let children = (selector: Xml_Element.t => bool, decoder: decoder<'a>, element: Xml_Element.t) => {
Xml_Element.childNodes(element)
->Xml_NodeList.asArrayLike
->Belt.Array.keepMap(Xml_Node.asElement)
->Belt.Array.keep(selector)
->Belt.Array.map(decoder(_))
->Array.filterMap(Xml_Node.asElement)
->Array.filter(selector)
->Array.map(decoder(_))
}

let map = (decoder: decoder<'a>, f: 'a => 'b, elem) => decoder(elem)->f
Expand Down Expand Up @@ -156,7 +155,7 @@ let oneOf = (decoders: list<decoder<'a>>, elem: Xml_Element.t) => {

let i = ref(0)
while result.contents->Option.isNone && i.contents < arr->Array.length {
let d = arr->Js.Array.unsafe_get(i.contents)
let d = arr->Array.getUnsafe(i.contents)
let res = try Some(d(elem)) catch {
| DecodeError(_) => None
}
Expand All @@ -170,8 +169,8 @@ let oneOf = (decoders: list<decoder<'a>>, elem: Xml_Element.t) => {
}

let float = str => {
let f = str->Js.Float.fromString
if f->Js.Float.isFinite {
let f = str->Float.fromString->Option.getUnsafe
if f->Float.isFinite {
f
} else {
raise(DecodeError("float expected"))
Expand All @@ -184,8 +183,8 @@ let int = str =>
}

let date = str => {
let d = str->Js.Date.fromString
if d->Js.Date.getTime->Js.Float.isNaN {
let d = str->Date.fromString
if d->Date.getTime->Float.isNaN {
raise(DecodeError("date expected"))
} else {
d
Expand All @@ -198,4 +197,4 @@ let bool = str =>
}

let childElements = elem =>
elem->Xml_Element.childNodes->Xml_NodeList.asArrayLike->Belt.Array.keepMap(Xml_Node.asElement)
elem->Xml_Element.childNodes->Xml_NodeList.asArrayLike->Array.filterMap(Xml_Node.asElement)
2 changes: 1 addition & 1 deletion src/Xml_Decode.resi
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ let oneOf: (list<decoder<'a>>, Xml_Element.t) => 'a

let float: string => float
let int: string => int
let date: string => Js.Date.t
let date: string => Date.t
let bool: string => bool

let childElements: decoder<array<Xml_Element.t>>
6 changes: 2 additions & 4 deletions src/Xml_DomParser.res
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
open Belt

type t

@send
external parseFromString: (t, string, string) => Xml_Document.t = "parseFromString"

let parse = (self, text, type_): Result.t<Xml_Element.t, string> => {
let parse = (self, text, type_): result<Xml_Element.t, string> => {
let doc = self->parseFromString(text, type_)

switch doc->Xml_Document.querySelector("parsererror") {
| Some(errorElement) => Error(errorElement->Xml_Element.textContent)
| None =>
let nodes = doc->Xml_Document.childNodes->Xml_NodeList.asArrayLike
switch nodes->Array.keepMap(Xml_Node.asElement)->Array.get(0) {
switch nodes->Array.filterMap(Xml_Node.asElement)->Array.get(0) {
| Some(root) => Ok(root)
| None => Error("root element missing")
}
Expand Down
6 changes: 3 additions & 3 deletions src/Xml_DomParser.resi
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ type t
@send
external parseFromString: (t, string, string) => Xml_Document.t = "parseFromString"

let parse: (t, string, string) => Belt.Result.t<Xml_Element.t, string>
let parse: (t, string, string) => result<Xml_Element.t, string>

let parseXml: (t, string) => Belt.Result.t<Xml_Element.t, string>
let parseXml: (t, string) => result<Xml_Element.t, string>

let parseHtml: (t, string) => Belt.Result.t<Xml_Element.t, string>
let parseHtml: (t, string) => result<Xml_Element.t, string>

// constructor, avoid external
let make: unit => t
1 change: 0 additions & 1 deletion src/Xml_NodeList.res
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
open Webapi
open Belt

type t = Dom.NodeList.t

Expand Down
Loading
Loading