|  | 
|  | 1 | +// | 
|  | 2 | +//  Array+Utils.swift | 
|  | 3 | +//  DigitalAssortmentPlanner | 
|  | 4 | +// | 
|  | 5 | +//  Created by Philipp Homann on 26.05.20. | 
|  | 6 | +//  Copyright © 2020 Endava All rights reserved. | 
|  | 7 | +// | 
|  | 8 | +// Permission is hereby granted, free of charge, to any person obtaining a copy | 
|  | 9 | +// of this software and associated documentation files (the "Software"), to deal | 
|  | 10 | +// in the Software without restriction, including without limitation the rights | 
|  | 11 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | 
|  | 12 | +// copies of the Software, and to permit persons to whom the Software is | 
|  | 13 | +// furnished to do so, subject to the following conditions: | 
|  | 14 | +// | 
|  | 15 | +// The above copyright notice and this permission notice shall be included in | 
|  | 16 | +// all copies or substantial portions of the Software. | 
|  | 17 | +// | 
|  | 18 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
|  | 19 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 
|  | 20 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 
|  | 21 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 
|  | 22 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 
|  | 23 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 
|  | 24 | +// THE SOFTWARE. | 
|  | 25 | + | 
|  | 26 | + | 
|  | 27 | +import Foundation | 
|  | 28 | + | 
|  | 29 | + | 
|  | 30 | + | 
|  | 31 | +public extension Array where Element : Hashable { | 
|  | 32 | +     | 
|  | 33 | +    /// returns an unique array without dublicated items | 
|  | 34 | +    var unique: [Element] { | 
|  | 35 | +        return Array(Set(self)) | 
|  | 36 | +    } | 
|  | 37 | +} | 
|  | 38 | + | 
|  | 39 | + | 
|  | 40 | +/// appends an item only if it is not contained | 
|  | 41 | +public extension RangeReplaceableCollection where Element: Equatable { | 
|  | 42 | +    @discardableResult | 
|  | 43 | +    mutating func appendIfNotContains(_ element: Element) -> (appended: Bool, memberAfterAppend: Element) { | 
|  | 44 | +        if let index = firstIndex(of: element) { | 
|  | 45 | +            return (false, self[index]) | 
|  | 46 | +        } else { | 
|  | 47 | +            append(element) | 
|  | 48 | +            return (true, element) | 
|  | 49 | +        } | 
|  | 50 | +    } | 
|  | 51 | +} | 
|  | 52 | + | 
|  | 53 | + | 
|  | 54 | +public extension Collection { | 
|  | 55 | +    subscript(safe index: Index) -> Iterator.Element? { | 
|  | 56 | +        guard indices.contains(index) else { return nil } | 
|  | 57 | +        return self[index] | 
|  | 58 | +    } | 
|  | 59 | +} | 
0 commit comments