<commit_before>= How to bind a JS library for OCaml

==Accessing a JS variable, ex: {{{document}}}:
Write in .ml:

<<code language="ocaml"|
let v = Js.Unsafe.variable "window.document"
>>
and in .mli:

<<code language="ocaml"|
val v : ... Js.t
>>

Be careful the function <<a_api | val Js.Unsafe.variable >> is not typed. Verify the library documentation before writing the type.


==Binding a JS function

Example from the Js module:
<<code language="ocaml"|
let decodeURI (s : js_string t) : js_string t =
  Js.Unsafe.fun_call (Js.Unsafe.variable "decodeURI") [|Js.Unsafe.inject s|]
>>

Have a look at the <<a_api | module Js.Unsafe >> module API.

==Using a JS constructor, ex: {{{F}}}:
Write in .ml:

<<code language="ocaml"|
let f = Js.Unsafe.variable "window.F"
>>
and in .mli:

<<code language="ocaml"|
val f : (... -> ... Js.t) Js.constr
>>

and if you want to use JS overloading, do, for example:
<<code language="ocaml"|
val f_fromInt : (int -> ... Js.t) Js.constr
val f_fromString : (js_string t -> ... Js.t) Js.constr
val f_blah : (#Dom_html.element t -> js_string t -> ... Js.t) Js.constr
>>
<commit_msg>Adding a paragraph in binding doc<commit_after>= How to bind a JS library for OCaml

==Accessing a JS variable, ex: {{{document}}}:
Write in .ml:

<<code language="ocaml"|
let v = Js.Unsafe.variable "window.document"
>>
and in .mli:

<<code language="ocaml"|
val v : ... Js.t
>>

Be careful the function <<a_api | val Js.Unsafe.variable >> is not typed. Verify the library documentation before writing the type.


==Binding a JS function

Example from the Js module:
<<code language="ocaml"|
let decodeURI (s : js_string t) : js_string t =
  Js.Unsafe.fun_call (Js.Unsafe.variable "decodeURI") [|Js.Unsafe.inject s|]
>>

Have a look at the <<a_api | module Js.Unsafe >> module API.

==Using a JS constructor, ex: {{{F}}}:
Write in .ml:

<<code language="ocaml"|
let f = Js.Unsafe.variable "window.F"
>>
and in .mli:

<<code language="ocaml"|
val f : (... -> ... Js.t) Js.constr
>>

and if you want to use JS overloading, do, for example:
<<code language="ocaml"|
val f_fromInt : (int -> ... Js.t) Js.constr
val f_fromString : (js_string t -> ... Js.t) Js.constr
val f_blah : (#Dom_html.element t -> js_string t -> ... Js.t) Js.constr
>>

==Accessing or modifying a JS property to an element

When a property is missing in the OCaml interface of an element (for example
it has been dynamically added by a library), you can access using unsafe
features:

<<code language="ocaml"|
(Js.Unsafe.coerce elt)##blah
>>

If you want to add yourself a new property:
<<code language="ocaml"|
(Js.Unsafe.coerce elt)##blah <- v
>>

Here, {{{v}}} may be a JS value or an OCaml value.

If you want to do that in type safe manner, just define new types for the
extended elements, or wrap the unsafe functions inside a getter and setter.


{'fraction_non_alphanumeric': 0.14869626497533475, 'fraction_numerical': 0.0, 'mean_word_length': 3.7714285714285714, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1437671', 'n_tokens_mistral': 1031, 'n_tokens_neox': 1004, 'n_words': 377}