-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathArray (ext).swift
More file actions
22 lines (21 loc) · 788 Bytes
/
Array (ext).swift
File metadata and controls
22 lines (21 loc) · 788 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
extension Array: ConvertibleToJSValue where Element: ConvertibleToJSValue {
@inlinable public var jsValue: JSValue {
.object(.array(self.map(\.jsValue)))
}
}
extension Array: ConstructibleFromJSValue where Element: ConstructibleFromJSValue {
@inlinable public static func construct(from value: JSValue) -> [Element]? {
guard case .object(let object) = value.storage, object.isArray else {
return nil
}
var elements: [Element] = []
; elements.reserveCapacity(object.buffer.count)
for element: JSValue in object.buffer {
guard let element: Element = .construct(from: element) else {
return nil
}
elements.append(element)
}
return elements
}
}