js module also uses runnableExamples (#16229)

This commit is contained in:
flywind 2020-12-02 23:24:59 +08:00 • committed by GitHub
commit 148d614dff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 40 deletions

View file

@ -12,26 +12,25 @@
## and ``JsAssoc`` together with the provided macros allows for smoother ## and ``JsAssoc`` together with the provided macros allows for smoother
## interfacing with JavaScript, allowing for example quick and easy imports of ## interfacing with JavaScript, allowing for example quick and easy imports of
## JavaScript variables: ## JavaScript variables:
##
## .. code-block:: nim runnableExamples:
## # Here, we are using jQuery for just a few calls and do not want to wrap the
## # Here, we are using jQuery for just a few calls and do not want to wrap the # whole library:
## # whole library:
## # import the document object and the console
## # import the document object and the console var document {.importc, nodecl.}: JsObject
## var document {.importc, nodecl.}: JsObject var console {.importc, nodecl.}: JsObject
## var console {.importc, nodecl.}: JsObject # import the "$" function
## # import the "$" function proc jq(selector: JsObject): JsObject {.importcpp: "$$(#)".}
## proc jq(selector: JsObject): JsObject {.importcpp: "$$(#)".}
## # Use jQuery to make the following code run, after the document is ready.
## # Use jQuery to make the following code run, after the document is ready. # This uses an experimental ``.()`` operator for ``JsObject``, to emit
## # This uses an experimental ``.()`` operator for ``JsObject``, to emit # JavaScript calls, when no corresponding proc exists for ``JsObject``.
## # JavaScript calls, when no corresponding proc exists for ``JsObject``. proc main =
## proc main = jq(document).ready(proc() =
## jq(document).ready(proc() = console.log("Hello JavaScript!")
## console.log("Hello JavaScript!") )
## )
##
when not defined(js) and not defined(nimdoc) and not defined(nimsuggest): when not defined(js) and not defined(nimdoc) and not defined(nimsuggest):
{.fatal: "Module jsFFI is designed to be used with the JavaScript backend.".} {.fatal: "Module jsFFI is designed to be used with the JavaScript backend.".}
@ -221,14 +220,10 @@ proc `==`*(x, y: JsRoot): bool {.importcpp: "(# === #)".}
macro `.`*(obj: JsObject, field: untyped): JsObject = macro `.`*(obj: JsObject, field: untyped): JsObject =
## Experimental dot accessor (get) for type JsObject. ## Experimental dot accessor (get) for type JsObject.
## Returns the value of a property of name `field` from a JsObject `x`. ## Returns the value of a property of name `field` from a JsObject `x`.
## runnableExamples:
## Example: let obj = newJsObject()
## obj.a = 20
## .. code-block:: nim assert obj.a.to(int) == 20
##
## let obj = newJsObject()
## obj.a = 20
## console.log(obj.a) # puts 20 onto the console.
if validJsName($field): if validJsName($field):
let importString = "#." & $field let importString = "#." & $field
result = quote do: result = quote do:

View file

@ -1,17 +1,14 @@
## Regular Expressions for the JavaScript target. ## Regular Expressions for the JavaScript target.
## * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions ## * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
##
## Examples runnableExamples:
## ======== let jsregex: RegExp = newRegExp(r"\s+", r"i")
## jsregex.compile(r"\w+", r"i")
## .. code-block::nim doAssert jsregex.test(r"nim javascript")
## let jsregex: RegExp = newRegExp(r"\s+", r"i") doAssert jsregex.exec(r"nim javascript") == @["nim".cstring]
## jsregex.compile(r"\w+", r"i") doAssert jsregex.toString() == r"/\w+/i"
## doAssert jsregex.test(r"nim javascript") jsregex.compile(r"[0-9]", r"i")
## doAssert jsregex.exec(r"nim javascript") == @["nim".cstring] doAssert jsregex.test(r"0123456789abcd")
## doAssert jsregex.toString() == r"/\w+/i"
## jsregex.compile(r"[0-9]", r"i")
## doAssert jsregex.test(r"0123456789abcd")
when not defined(js) and not defined(Nimdoc): when not defined(js) and not defined(Nimdoc):