mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
46 lines
1.1 KiB
Swift
46 lines
1.1 KiB
Swift
//
|
|
// Array+SwiftLint.swift
|
|
// SwiftLint
|
|
//
|
|
// Created by Scott Hoyt on 1/11/16.
|
|
// Copyright © 2016 Realm. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
extension Array where Element: NSTextCheckingResult {
|
|
func ranges() -> [NSRange] {
|
|
return map { $0.range }
|
|
}
|
|
}
|
|
|
|
extension Array where Element: Equatable {
|
|
var unique: [Element] {
|
|
var uniqueValues = [Element]()
|
|
for item in self where !uniqueValues.contains(item) {
|
|
uniqueValues.append(item)
|
|
}
|
|
return uniqueValues
|
|
}
|
|
}
|
|
|
|
extension Array {
|
|
static func array(of obj: Any?) -> [Element]? {
|
|
if let array = obj as? [Element] {
|
|
return array
|
|
} else if let obj = obj as? Element {
|
|
return [obj]
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func group<U: Hashable>(by transform: (Element) -> U) -> [U: [Element]] {
|
|
return reduce([:]) { dictionary, element in
|
|
var dictionary = dictionary
|
|
let key = transform(element)
|
|
dictionary[key] = (dictionary[key] ?? []) + [element]
|
|
return dictionary
|
|
}
|
|
}
|
|
}
|