Skip to content

Commit ebb5fda

Browse files
committed
perf: Introduce a helper function to pluralize time left from epoch
1 parent a5f7e9a commit ebb5fda

File tree

1 file changed

+11
-7
lines changed
  • FileBrowserClient/FileBrowserClient/Utilities

1 file changed

+11
-7
lines changed

FileBrowserClient/FileBrowserClient/Utilities/Utils.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ func timeStampToString(from timestamp: TimeInterval?) -> String {
299299
return formatter.string(from: date)
300300
}
301301

302+
func pluralize(_ count: Int, _ identifier: String) -> String {
303+
return "\(count) \(identifier)\(count == 1 ? "" : "s")"
304+
}
305+
302306
func timeLeftString(until timestamp: TimeInterval?, asString: Bool = true) -> String {
303307
guard let timestamp = timestamp else {
304308
return "Unknown"
@@ -328,26 +332,26 @@ func timeLeftString(until timestamp: TimeInterval?, asString: Bool = true) -> St
328332
if asString {
329333
var components: [String] = []
330334
if years > 0 {
331-
components.append("\(years) year\(years == 1 ? "" : "s")")
335+
components.append(pluralize(years, "year"))
332336
}
333337
if months > 0 {
334-
components.append("\(months) month\(months == 1 ? "" : "s")")
338+
components.append(pluralize(months, "month"))
335339
}
336340
if weeks > 0 {
337-
components.append("\(weeks) week\(weeks == 1 ? "" : "s")")
341+
components.append(pluralize(weeks, "week"))
338342
}
339343
if days > 0 {
340-
components.append("\(days) day\(days == 1 ? "" : "s")")
344+
components.append(pluralize(days, "day"))
341345
}
342346
if hours > 0 {
343-
components.append("\(hours) hour\(hours == 1 ? "" : "s")")
347+
components.append(pluralize(hours, "hour"))
344348
}
345349
if minutes > 0 {
346-
components.append("\(minutes) minute\(minutes == 1 ? "" : "s")")
350+
components.append(pluralize(minutes, "minute"))
347351
}
348352
if components.isEmpty {
349353
// If seconds is the only option
350-
components.append("\(seconds) second\(seconds == 1 ? "" : "s")")
354+
components.append(pluralize(seconds, "second"))
351355
}
352356
return components.joined(separator: ", ")
353357
} else {

0 commit comments

Comments
 (0)