Skip to content

Extracting t fragment from Locator #372

@domkm

Description

@domkm

What do you think of providing a method of extracting time(s) from a Locator

There is a private extension on Locator which kind of does this, but looks like it only matches start time (my understanding is that end time can be specified).

private extension Locator {
private static let timeFragmentRegex = try! NSRegularExpression(pattern: #"t=(\d+(?:\.\d+)?)"#)
// FIXME: Should probably be in `Locator` itself.
func time(forDuration duration: Double? = nil) -> Double? {
if let progression = locations.progression, let duration = duration {
return progression * duration
} else {
for fragment in locations.fragments {
let range = NSRange(fragment.startIndex ..< fragment.endIndex, in: fragment)
if let match = Self.timeFragmentRegex.firstMatch(in: fragment, range: range) {
let matchRange = match.range(at: 1)
if matchRange.location != NSNotFound, let range = Range(matchRange, in: fragment) {
return Double(fragment[range])
}
}
}
}
return nil
}
}

Would you be open to a Locator.time property/function (() -> (Double?, Double?)?). I'd be open to providing a PR for this if you want.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions