summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJuan Carlos <juancarlospaco@gmail.com>2021-05-05 02:46:42 -0300
committerGitHub <noreply@github.com>2021-05-05 07:46:42 +0200
commit5762b1d75c55900268e1abd0e83aaaa9fd43e9fe (patch)
tree42a45de3edd99b83df9dd20255751778b5c8aa30
parent94c4c01d9c2a8f78d63533c147afd42f05b21627 (diff)
downloadNim-5762b1d75c55900268e1abd0e83aaaa9fd43e9fe.tar.gz
Add copyWithin for JavaScript (#17937)
* Add jscore.copyWithin for seq and array
* Shallow copy mention docs
-rw-r--r--changelog.md2
-rw-r--r--lib/js/jscore.nim13
2 files changed, 15 insertions, 0 deletions
diff --git a/changelog.md b/changelog.md
index 21f3cb9b5..0c95295cd 100644
--- a/changelog.md
+++ b/changelog.md
@@ -295,6 +295,8 @@
 - Added `genasts.genAst` that avoids the problems inherent with `quote do` and can
   be used as a replacement.
 
+- Added `copyWithin` [for `seq` and `array` for JavaScript targets](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin).
+
 
 ## Language changes
 
diff --git a/lib/js/jscore.nim b/lib/js/jscore.nim
index 693e50799..61c188431 100644
--- a/lib/js/jscore.nim
+++ b/lib/js/jscore.nim
@@ -110,3 +110,16 @@ proc parse*(l: JsonLib, s: cstring): JsRoot {.importcpp.}
 since (1, 5):
   func debugger*() {.importjs: "debugger@".}
     ## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger
+
+  func copyWithin*[T](self: openArray[T]; target: int): seq[T] {.importjs: "#.copyWithin(#)".}
+  func copyWithin*[T](self: openArray[T]; target, start: int): seq[T] {.importjs: "#.copyWithin(#, #)".}
+  func copyWithin*[T](self: openArray[T]; target, start, ends: int): seq[T] {.importjs: "#.copyWithin(#, #, #)".} =
+    ## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin
+    ## `copyWithin` uses shallow copy.
+    runnableExamples:
+      assert ['a', 'b', 'c', 'd', 'e'].copyWithin(0, 3, 4) == @['d', 'b', 'c', 'd', 'e']
+      assert ['a', 'b', 'c', 'd', 'e'].copyWithin(1, 3) == @['a', 'd', 'e', 'd', 'e']
+      assert [1, 2, 3, 4, 5].copyWithin(-2) == @[1, 2, 3, 1, 2]
+      assert [1, 2, 3, 4, 5].copyWithin(0, 3) == @[4, 5, 3, 4, 5]
+      assert [1, 2, 3, 4, 5].copyWithin(0, 3, 4) == @[4, 2, 3, 4, 5]
+      assert [1, 2, 3, 4, 5].copyWithin(-2, -3, -1) == @[1, 2, 3, 3, 4]