Skip to content

Commit 19365a1

Browse files
authored
Implement RandomAccessCollection on JSArrayRef (#30)
* Implement `RandomAccessCollection` on `JSArrayRef` * Remove unused `Index` typealias
1 parent 18adf42 commit 19365a1

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
dist
22
node_modules
33
.DS_Store
4-
/.build
4+
.build
55
/Packages
66
/*.xcodeproj
77
xcuserdata/

IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ Array_Iterator: do {
100100
try expectEqual(Array(array), expectedProp_4)
101101
}
102102

103+
Array_RandomAccessCollection: do {
104+
let globalObject1 = getJSValue(this: .global, name: "globalObject1")
105+
let globalObject1Ref = try expectObject(globalObject1)
106+
let prop_4 = getJSValue(this: globalObject1Ref, name: "prop_4")
107+
let array = try expectArray(prop_4)
108+
let expectedProp_4: [JSValue] = [
109+
.number(3), .number(4), .string("str_elm_1"), .number(5),
110+
]
111+
try expectEqual([array[0], array[1], array[2], array[3]], expectedProp_4)
112+
}
113+
103114
Value_Decoder: do {
104115
struct GlobalObject1: Codable {
105116
struct Prop1: Codable {

Sources/JavaScriptKit/JSArrayRef.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class JSArrayRef {
1414
}
1515
}
1616

17-
extension JSArrayRef: Sequence {
17+
extension JSArrayRef: RandomAccessCollection {
1818
public typealias Element = JSValue
1919

2020
public func makeIterator() -> Iterator {
@@ -37,4 +37,12 @@ extension JSArrayRef: Sequence {
3737
return value.isNull ? nil : value
3838
}
3939
}
40+
41+
public subscript(position: Int) -> JSValue {
42+
ref.get(position)
43+
}
44+
45+
public var startIndex: Int { 0 }
46+
47+
public var endIndex: Int { ref.length.number.map(Int.init) ?? 0 }
4048
}

0 commit comments

Comments
 (0)