Skip to content

Commit 6ce7e77

Browse files
authored
Rename ref to jsObject on JSDate for consistency with JSError (#50)
I think that `jsObject` name is more representative than plain `ref`, and as Jed correctly points out, it should be public after all for users to access it if necessary. Also, more doc comments are added to `JSDate` with existing ones refined.
1 parent f97dca1 commit 6ce7e77

File tree

3 files changed

+89
-64
lines changed

3 files changed

+89
-64
lines changed
Lines changed: 81 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1+
/** A wrapper around the [JavaScript Date
2+
class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) that
3+
exposes its properties in a type-safe way. This doesn't 100% match the JS API, for example
4+
`getMonth`/`setMonth` etc accessor methods are converted to properties, but the rest of it matches
5+
in the naming. Parts of the JavaScript `Date` API that are not consistent across browsers and JS
6+
implementations are not exposed in a type-safe manner, you should access the underlying `jsObject`
7+
property if you need those.
8+
*/
19
public final class JSDate {
10+
/// The constructor function used to create new `Date` objects.
211
private static let constructor = JSObject.global.Date.function!
3-
public let ref: JSObject
412

13+
/// The underlying JavaScript `Date` object.
14+
public let jsObject: JSObject
15+
16+
/** Creates a new instance of the JavaScript `Date` class with a given amount of milliseconds
17+
that passed since midnight 01 January 1970 UTC.
18+
*/
519
public init(millisecondsSinceEpoch: Double? = nil) {
620
if let milliseconds = millisecondsSinceEpoch {
7-
ref = Self.constructor.new(milliseconds)
21+
jsObject = Self.constructor.new(milliseconds)
822
} else {
9-
ref = Self.constructor.new()
23+
jsObject = Self.constructor.new()
1024
}
1125
}
1226

@@ -22,199 +36,210 @@ public final class JSDate {
2236
seconds: Int = 0,
2337
milliseconds: Int = 0
2438
) {
25-
ref = Self.constructor.new(year, monthIndex, day, hours, minutes, seconds, milliseconds)
39+
jsObject = Self.constructor.new(year, monthIndex, day, hours, minutes, seconds, milliseconds)
2640
}
2741

2842
/// Year of this date in local time zone.
2943
public var fullYear: Int {
3044
get {
31-
Int(ref.getFullYear!().number!)
45+
Int(jsObject.getFullYear!().number!)
3246
}
3347
set {
34-
_ = ref.setFullYear!(newValue)
48+
_ = jsObject.setFullYear!(newValue)
3549
}
3650
}
3751

38-
/// Month of this date in `0–11` range in local time zone
52+
/// Month of this date in `0–11` range in local time zone.
3953
public var month: Int {
4054
get {
41-
Int(ref.getMonth!().number!)
55+
Int(jsObject.getMonth!().number!)
4256
}
4357
set {
44-
_ = ref.setMonth!(newValue)
58+
_ = jsObject.setMonth!(newValue)
4559
}
4660
}
4761

4862
/// The day of the month in `1..31` range in local time zone.
4963
public var date: Int {
5064
get {
51-
Int(ref.getDate!().number!)
65+
Int(jsObject.getDate!().number!)
5266
}
5367
set {
54-
_ = ref.setDate!(newValue)
68+
_ = jsObject.setDate!(newValue)
5569
}
5670
}
5771

5872
/// The day of the week in `0..6` range in local time zone.
5973
public var day: Int {
60-
Int(ref.getDay!().number!)
74+
Int(jsObject.getDay!().number!)
6175
}
6276

6377
/// The amount of hours in this day from `0..23` range in local time zone.
6478
public var hours: Int {
6579
get {
66-
Int(ref.getHours!().number!)
80+
Int(jsObject.getHours!().number!)
6781
}
6882
set {
69-
_ = ref.setHours!(newValue)
83+
_ = jsObject.setHours!(newValue)
7084
}
7185
}
7286

7387
/// The amount of minutes in this hours from `0..59` range in local time zone.
7488
public var minutes: Int {
7589
get {
76-
Int(ref.getMinutes!().number!)
90+
Int(jsObject.getMinutes!().number!)
7791
}
7892
set {
79-
_ = ref.setMinutes!(newValue)
93+
_ = jsObject.setMinutes!(newValue)
8094
}
8195
}
8296

8397
/// The amount of seconds in this minute from `0..59` range in local time zone.
8498
public var seconds: Int {
8599
get {
86-
Int(ref.getSeconds!().number!)
100+
Int(jsObject.getSeconds!().number!)
87101
}
88102
set {
89-
_ = ref.setSeconds!(newValue)
103+
_ = jsObject.setSeconds!(newValue)
90104
}
91105
}
92106

93107
/// The amount of milliseconds in this second `0..999` range in local time zone.
94108
public var milliseconds: Int {
95109
get {
96-
Int(ref.getMilliseconds!().number!)
110+
Int(jsObject.getMilliseconds!().number!)
97111
}
98112
set {
99-
_ = ref.setMilliseconds!(newValue)
113+
_ = jsObject.setMilliseconds!(newValue)
100114
}
101115
}
102116

103-
/// Year of this date in the UTC time zone
117+
/// Year of this date in the UTC time zone.
104118
public var utcFullYear: Int {
105119
get {
106-
Int(ref.getUTCFullYear!().number!)
120+
Int(jsObject.getUTCFullYear!().number!)
107121
}
108122
set {
109-
_ = ref.setUTCFullYear!(newValue)
123+
_ = jsObject.setUTCFullYear!(newValue)
110124
}
111125
}
112126

113-
/// Month of this date in `0–11` range in the UTC time zone
127+
/// Month of this date in `0–11` range in the UTC time zone.
114128
public var utcMonth: Int {
115129
get {
116-
Int(ref.getUTCMonth!().number!)
130+
Int(jsObject.getUTCMonth!().number!)
117131
}
118132
set {
119-
_ = ref.setUTCMonth!(newValue)
133+
_ = jsObject.setUTCMonth!(newValue)
120134
}
121135
}
122136

123-
/// The day of the month in `1..31` range in the UTC time zone
137+
/// The day of the month in `1..31` range in the UTC time zone.
124138
public var utcDate: Int {
125139
get {
126-
Int(ref.getUTCDate!().number!)
140+
Int(jsObject.getUTCDate!().number!)
127141
}
128142
set {
129-
_ = ref.setUTCDate!(newValue)
143+
_ = jsObject.setUTCDate!(newValue)
130144
}
131145
}
132146

133-
/// The day of the week in `0..6` range in the UTC time zone
147+
/// The day of the week in `0..6` range in the UTC time zone.
134148
public var utcDay: Int {
135-
Int(ref.getUTCDay!().number!)
149+
Int(jsObject.getUTCDay!().number!)
136150
}
137151

138-
/// The amount of hours in this day from `0..23` range in the UTC time zone
152+
/// The amount of hours in this day from `0..23` range in the UTC time zone.
139153
public var utcHours: Int {
140154
get {
141-
Int(ref.getUTCHours!().number!)
155+
Int(jsObject.getUTCHours!().number!)
142156
}
143157
set {
144-
_ = ref.setUTCHours!(newValue)
158+
_ = jsObject.setUTCHours!(newValue)
145159
}
146160
}
147161

148-
/// The amount of minutes in this hours from `0..59` range in the UTC time zone
162+
/// The amount of minutes in this hours from `0..59` range in the UTC time zone.
149163
public var utcMinutes: Int {
150164
get {
151-
Int(ref.getUTCMinutes!().number!)
165+
Int(jsObject.getUTCMinutes!().number!)
152166
}
153167
set {
154-
_ = ref.setUTCMinutes!(newValue)
168+
_ = jsObject.setUTCMinutes!(newValue)
155169
}
156170
}
157171

158-
/// The amount of seconds in this minute from `0..59` range in the UTC time zone
172+
/// The amount of seconds in this minute from `0..59` range in the UTC time zone.
159173
public var utcSeconds: Int {
160174
get {
161-
Int(ref.getUTCSeconds!().number!)
175+
Int(jsObject.getUTCSeconds!().number!)
162176
}
163177
set {
164-
_ = ref.setUTCSeconds!(newValue)
178+
_ = jsObject.setUTCSeconds!(newValue)
165179
}
166180
}
167181

168-
/// The amount of milliseconds in this second `0..999` range in the UTC time zone
182+
/// The amount of milliseconds in this second `0..999` range in the UTC time zone.
169183
public var utcMilliseconds: Int {
170184
get {
171-
Int(ref.getUTCMilliseconds!().number!)
185+
Int(jsObject.getUTCMilliseconds!().number!)
172186
}
173187
set {
174-
_ = ref.setUTCMilliseconds!(newValue)
188+
_ = jsObject.setUTCMilliseconds!(newValue)
175189
}
176190
}
177191

178-
/// Offset in minutes between the local time zone and UTC
192+
/// Offset in minutes between the local time zone and UTC.
179193
public var timezoneOffset: Int {
180-
Int(ref.getTimezoneOffset!().number!)
194+
Int(jsObject.getTimezoneOffset!().number!)
181195
}
182196

197+
/// Returns a string conforming to ISO 8601 that contains date and time, e.g.
198+
/// `"2020-09-15T08:56:54.811Z"`.
183199
public func toISOString() -> String {
184-
ref.toISOString!().string!
200+
jsObject.toISOString!().string!
185201
}
186202

203+
/// Returns a string with date parts in a format defined by user's locale, e.g. `"9/15/2020"`.
187204
public func toLocaleDateString() -> String {
188-
ref.toLocaleDateString!().string!
205+
jsObject.toLocaleDateString!().string!
189206
}
190207

208+
/// Returns a string with time parts in a format defined by user's locale, e.g. `"10:04:14"`.
191209
public func toLocaleTimeString() -> String {
192-
ref.toLocaleTimeString!().string!
210+
jsObject.toLocaleTimeString!().string!
193211
}
194212

213+
/** Returns a string formatted according to
214+
[rfc7231](https://tools.ietf.org/html/rfc7231#section-7.1.1.1) and modified according to
215+
[ecma-262](https://www.ecma-international.org/ecma-262/10.0/index.html#sec-date.prototype.toutcstring),
216+
e.g. `Tue, 15 Sep 2020 09:04:40 GMT`.
217+
*/
195218
public func toUTCString() -> String {
196-
ref.toUTCString!().string!
219+
jsObject.toUTCString!().string!
197220
}
198221

199-
/// Number of milliseconds since midnight 01 January 1970 UTC to the present moment ignoring leap
200-
/// seconds
222+
/** Number of milliseconds since midnight 01 January 1970 UTC to the present moment ignoring
223+
leap seconds.
224+
*/
201225
public static func now() -> Double {
202226
constructor.now!().number!
203227
}
204228

205-
/// Number of milliseconds since midnight 01 January 1970 UTC to the given date ignoring leap
206-
/// seconds
229+
/** Number of milliseconds since midnight 01 January 1970 UTC to the given date ignoring leap
230+
seconds.
231+
*/
207232
public func valueOf() -> Double {
208-
ref.valueOf!().number!
233+
jsObject.valueOf!().number!
209234
}
210235
}
211236

212237
extension JSDate: Comparable {
213-
public static func ==(lhs: JSDate, rhs: JSDate) -> Bool {
238+
public static func == (lhs: JSDate, rhs: JSDate) -> Bool {
214239
return lhs.valueOf() == rhs.valueOf()
215240
}
216241

217-
public static func <(lhs: JSDate, rhs: JSDate) -> Bool {
242+
public static func < (lhs: JSDate, rhs: JSDate) -> Bool {
218243
return lhs.valueOf() < rhs.valueOf()
219244
}
220245
}

Sources/JavaScriptKit/BasicObjects/JSError.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_
33
exposes its properties in a type-safe way.
44
*/
55
public final class JSError: Error {
6-
/// The underlying JavaScript `Error` object.
7-
public let jsObject: JSObject
8-
96
/// The constructor function used to create new `Error` objects.
107
private static let constructor = JSObject.global.Error.function!
118

9+
/// The underlying JavaScript `Error` object.
10+
public let jsObject: JSObject
11+
1212
/// Creates a new instance of the JavaScript `Error` class with a given message.
1313
public init(message: String) {
1414
jsObject = Self.constructor.new([message])

Sources/JavaScriptKit/BasicObjects/JSTimer.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
/** This timer type hides [`setInterval`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval)
1+
/** This timer is an abstraction over [`setInterval`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval)
22
/ [`clearInterval`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearInterval) and
33
[`setTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout)
44
/ [`clearTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout)
5-
pairs of calls for you. It intentionally doesn't match the JavaScript API, as a special care is
5+
JavaScript functions. It intentionally doesn't match the JavaScript API, as a special care is
66
needed to hold a reference to the timer closure and to call `JSClosure.release()` on it when the
77
timer is deallocated. As a user, you have to hold a reference to a `JSTimer` instance for it to stay
88
valid. The `JSTimer` API is also intentionally trivial, the timer is started right away, and the
9-
only way to invalidate the timer is to bring the reference count of the `JSTimer` instance to zero,
10-
either by storing the timer in an optional property and assigning `nil` to it or by deallocating the
11-
object that owns it for invalidation.
9+
only way to invalidate the timer is to bring the reference count of the `JSTimer` instance to zero.
10+
For invalidation you should either store the timer in an optional property and assign `nil` to it,
11+
or deallocate the object that owns the timer.
1212
*/
1313
public final class JSTimer {
1414
/// Indicates whether this timer instance calls its callback repeatedly at a given delay.

0 commit comments

Comments
 (0)