Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
104 lines (88 loc) · 3.1 KB

File metadata and controls

104 lines (88 loc) · 3.1 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/// A wrapper around [the JavaScript `Array`
/// class](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)
/// that exposes its properties in a type-safe and Swifty way.
public class JSArray: JSBridgedClass {
public static let constructor = JSObject.global.Array.function
static func isArray(_ object: JSObject) -> Bool {
constructor!.isArray!(object).boolean!
}
public let jsObject: JSObject
public required convenience init?(from value: JSValue) {
guard let object = value.object else { return nil }
self.init(object)
}
/// Construct a `JSArray` from Array `JSObject`.
/// Return `nil` if the object is not an Array.
///
/// - Parameter object: A `JSObject` expected to be a JavaScript Array
public convenience init?(_ jsObject: JSObject) {
guard Self.isArray(jsObject) else { return nil }
self.init(unsafelyWrapping: jsObject)
}
public required init(unsafelyWrapping jsObject: JSObject) {
self.jsObject = jsObject
}
}
extension JSArray: RandomAccessCollection {
public typealias Element = JSValue
public func makeIterator() -> Iterator {
Iterator(jsObject: jsObject)
}
/// Iterator type for `JSArray`, conforming to `IteratorProtocol` from the standard library, which allows
/// easy iteration over elements of `JSArray` instances.
public class Iterator: IteratorProtocol {
private let jsObject: JSObject
private var index = 0
init(jsObject: JSObject) {
self.jsObject = jsObject
}
public func next() -> Element? {
let currentIndex = index
guard currentIndex < Int(jsObject.length.number!) else {
return nil
}
index += 1
guard jsObject.hasOwnProperty!(currentIndex).boolean! else {
return next()
}
let value = jsObject[currentIndex]
return value
}
}
public subscript(position: Int) -> JSValue {
jsObject[position]
}
public var startIndex: Int { 0 }
public var endIndex: Int { length }
/// The number of elements in that array including empty hole.
/// Note that `length` respects JavaScript's `Array.prototype.length`
///
/// e.g.
/// ```javascript
/// const array = [1, , 3];
/// ```
/// ```swift
/// let array: JSArray = ...
/// array.length // 3
/// array.count // 2
/// ```
public var length: Int {
Int(jsObject.length.number!)
}
/// The number of elements in that array **not** including empty hole.
/// Note that `count` syncs with the number that `Iterator` can iterate.
/// See also: `JSArray.length`
public var count: Int {
getObjectValuesLength(jsObject)
}
}
private let alwaysTrue = JSClosure { _ in .boolean(true) }
private func getObjectValuesLength(_ object: JSObject) -> Int {
let values = object.filter!(alwaysTrue).object!
return Int(values.length.number!)
}
public extension JSValue {
var array: JSArray? {
object.flatMap(JSArray.init)
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.