summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorJuan Carlos <juancarlospaco@gmail.com>2021-03-21 06:35:55 -0300
committerGitHub <noreply@github.com>2021-03-21 10:35:55 +0100
commitfb38d906a284a088f052e6fc842fb7f2df26a486 (patch)
tree2129b5e8f3f30fb05cebc3f782bee13ec14cca74 /lib
parent05743bc9f72f2e3cbf4b17f5974a811637f03241 (diff)
downloadNim-fb38d906a284a088f052e6fc842fb7f2df26a486.tar.gz
Improve jsre (#17365)
* Add dollar for regex

* Add dollar for regex

* Peer review feedbacks

* Peer review feedbacks

* Update lib/js/jsre.nim

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>

* Update lib/js/jsre.nim

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>

* Update lib/js/jsre.nim

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>

* Pear review

* Beer review

* Beer review

Co-authored-by: Timothee Cour <timothee.cour2@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/js/jsre.nim77
1 files changed, 47 insertions, 30 deletions
diff --git a/lib/js/jsre.nim b/lib/js/jsre.nim
index 7be7221bc..7d51db646 100644
--- a/lib/js/jsre.nim
+++ b/lib/js/jsre.nim
@@ -1,46 +1,63 @@
 ## Regular Expressions for the JavaScript target.
 ## * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
-
-runnableExamples:
-  let jsregex: RegExp = newRegExp(r"\s+", r"i")
-  jsregex.compile(r"\w+", r"i")
-  doAssert jsregex.test(r"nim javascript")
-  doAssert jsregex.exec(r"nim javascript") == @["nim".cstring]
-  doAssert jsregex.toString() == r"/\w+/i"
-  jsregex.compile(r"[0-9]", r"i")
-  doAssert jsregex.test(r"0123456789abcd")
-
-
 when not defined(js):
   {.error: "This module only works on the JavaScript platform".}
 
-type RegExp* {.importjs.} = object    ## Regular Expressions for JavaScript target.
-  flags* {.importjs.}: cstring        ## cstring that contains the flags of the RegExp object.
-  dotAll* {.importjs.}: bool          ## Whether `.` matches newlines or not.
-  global* {.importjs.}: bool          ## Whether to test against all possible matches in a string, or only against the first.
-  ignoreCase* {.importjs.}: bool      ## Whether to ignore case while attempting a match in a string.
-  multiline* {.importjs.}: bool       ## Whether to search in strings across multiple lines.
-  source* {.importjs.}: cstring       ## The text of the pattern.
-  sticky* {.importjs.}: bool          ## Whether the search is sticky.
-  unicode* {.importjs.}: bool         ## Whether Unicode features are enabled.
-  lastIndex* {.importjs.}: cint       ## Index at which to start the next match (read/write property).
-  input* {.importjs.}: cstring        ## Read-only and modified on successful match.
-  lastMatch* {.importjs.}: cstring    ## Ditto.
-  lastParen* {.importjs.}: cstring    ## Ditto.
-  leftContext* {.importjs.}: cstring  ## Ditto.
-  rightContext* {.importjs.}: cstring ## Ditto.
+type RegExp* = ref object of JsRoot
+  ## Regular Expressions for JavaScript target.
+  ## See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
+  flags*: cstring        ## cstring that contains the flags of the RegExp object.
+  dotAll*: bool          ## Whether `.` matches newlines or not.
+  global*: bool          ## Whether to test against all possible matches in a string, or only against the first.
+  ignoreCase*: bool      ## Whether to ignore case while attempting a match in a string.
+  multiline*: bool       ## Whether to search in strings across multiple lines.
+  source*: cstring       ## The text of the pattern.
+  sticky*: bool          ## Whether the search is sticky.
+  unicode*: bool         ## Whether Unicode features are enabled.
+  lastIndex*: cint       ## Index at which to start the next match (read/write property).
+  input*: cstring        ## Read-only and modified on successful match.
+  lastMatch*: cstring    ## Ditto.
+  lastParen*: cstring    ## Ditto.
+  leftContext*: cstring  ## Ditto.
+  rightContext*: cstring ## Ditto.
+
 
 func newRegExp*(pattern: cstring; flags: cstring): RegExp {.importjs: "new RegExp(@)".}
   ## Creates a new RegExp object.
 
+func newRegExp*(pattern: cstring): RegExp {.importjs: "new RegExp(@)".}
+
 func compile*(self: RegExp; pattern: cstring; flags: cstring) {.importjs: "#.compile(@)".}
   ## Recompiles a regular expression during execution of a script.
 
 func exec*(self: RegExp; pattern: cstring): seq[cstring] {.importjs: "#.exec(#)".}
   ## Executes a search for a match in its string parameter.
 
-func test*(self: RegExp; pattern: cstring): bool {.importjs: "#.test(#)".}
-  ## Tests for a match in its string parameter.
-
-func toString*(self: RegExp): cstring {.importjs: "#.toString()".}
+func toCstring*(self: RegExp): cstring {.importjs: "#.toString()".}
   ## Returns a string representing the RegExp object.
+
+func `$`*(self: RegExp): string = $toCstring(self)
+
+func test*(self: RegExp; pattern: cstring): bool {.importjs: "#.test(#)", deprecated: "Use contains instead".}
+
+func toString*(self: RegExp): cstring {.importjs: "#.toString()", deprecated: "Use toCstring instead".}
+
+func contains*(pattern: cstring; self: RegExp): bool =
+  ## Tests for a substring match in its string parameter.
+  runnableExamples:
+    let jsregex: RegExp = newRegExp(r"bc$", r"i")
+    assert jsregex in r"abc"
+    assert jsregex notin r"abcd"
+    assert "xabc".contains jsregex
+  asm "`result` = `self`.test(`pattern`);"
+
+
+runnableExamples:
+  let jsregex: RegExp = newRegExp(r"\s+", r"i")
+  jsregex.compile(r"\w+", r"i")
+  assert "nim javascript".contains jsregex
+  assert jsregex.exec(r"nim javascript") == @["nim".cstring]
+  assert jsregex.toCstring() == r"/\w+/i"
+  jsregex.compile(r"[0-9]", r"i")
+  assert "0123456789abcd".contains jsregex
+  assert $jsregex == "/[0-9]/i"