Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implement RandomAccessCollection on JSArrayRef
  • Loading branch information
MaxDesiatov committed Aug 4, 2020
commit a6fa93e0caf7292390c61939c3e6197ec2c8aefe
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
dist
node_modules
.DS_Store
/.build
.build
/Packages
/*.xcodeproj
xcuserdata/
11 changes: 11 additions & 0 deletions IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ Array_Iterator: do {
try expectEqual(Array(array), expectedProp_4)
}

Array_RandomAccessCollection: do {
let globalObject1 = getJSValue(this: .global, name: "globalObject1")
let globalObject1Ref = try expectObject(globalObject1)
let prop_4 = getJSValue(this: globalObject1Ref, name: "prop_4")
let array = try expectArray(prop_4)
let expectedProp_4: [JSValue] = [
.number(3), .number(4), .string("str_elm_1"), .number(5),
]
try expectEqual([array[0], array[1], array[2], array[3]], expectedProp_4)
}

Value_Decoder: do {
struct GlobalObject1: Codable {
struct Prop1: Codable {
Expand Down
11 changes: 10 additions & 1 deletion Sources/JavaScriptKit/JSArrayRef.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ public class JSArrayRef {
}
}

extension JSArrayRef: Sequence {
extension JSArrayRef: RandomAccessCollection {
public typealias Element = JSValue
public typealias Index = Int

public func makeIterator() -> Iterator {
Iterator(ref: ref)
Expand All @@ -37,4 +38,12 @@ extension JSArrayRef: Sequence {
return value.isNull ? nil : value
}
}

public subscript(position: Int) -> JSValue {
ref.get(position)
}

public var startIndex: Int { 0 }

public var endIndex: Int { ref.length.number.map(Int.init) ?? 0 }
}