Skip to content

Commit

Permalink
Merge branch 'deploy/2.8.0' into productive
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeehut committed Feb 11, 2019
2 parents bbbcebc + c41b167 commit f736ec0
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 170 deletions.
84 changes: 0 additions & 84 deletions .projlint.yml

This file was deleted.

20 changes: 19 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,28 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
### Addedge
- None.
### Changed
- None.
### Deprecated
- None.
### Removed
- None.
### Fixed
- None.
### Security
- None.


## [2.8.0]
### Added
- New `NSRange(_:in:)` initializer for converting from `Range<String.Index>`
- New `sum` computed property on `Sequence` types like `Array`
- New `average` computed property on `Collection` types with `Int` or `Double` elements like `[Int]`
- New `fullRange` and `fullNSRange` computed properties on `String`
### Changed
- Made some APIs available in wider contexts (like `sample` in `RandomAccessCollection` instead of `Array`)
### Deprecated
- None.
### Removed
Expand Down
68 changes: 46 additions & 22 deletions Frameworks/HandySwift/Extensions/ArrayExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,9 @@
import Foundation

extension Array {
/// A tuple representing a combination.
public typealias Combination<T> = (left: Element, right: T)

/// Returns a random element from the `Array`.
///
/// - Returns: A random element from the array or `nil` if empty.
public var sample: Element? {
guard let randomIndex = Int(randomBelow: count) else { return nil }
return self[randomIndex]
}

/// Returns a given number of random elements from the `Array`.
///
/// - Parameters:
/// - size: The number of random elements wanted.
/// - Returns: An array with the given number of random elements or `nil` if empty.
public func sample(size: Int) -> [Element]? {
guard !isEmpty else { return nil }

var sampleElements: [Element] = []
size.times { sampleElements.append(sample!) }

return sampleElements
}

/// Combines each element with each element of a given array.
///
/// Also known as: Cartesian product.
Expand Down Expand Up @@ -111,6 +90,51 @@ extension Array {
}
}

extension RandomAccessCollection where Index == Int {
/// Returns a random element from the `Array`.
///
/// - Returns: A random element from the array or `nil` if empty.
public var sample: Element? {
guard let randomIndex = Int(randomBelow: count) else { return nil }
return self[randomIndex]
}

/// Returns a given number of random elements from the `Array`.
///
/// - Parameters:
/// - size: The number of random elements wanted.
/// - Returns: An array with the given number of random elements or `nil` if empty.
public func sample(size: Int) -> [Element]? {
guard !isEmpty else { return nil }

var sampleElements: [Element] = []
size.times { sampleElements.append(sample!) }

return sampleElements
}
}

extension Sequence where Element: Numeric {
/// Returns the sum of all elements.
public func sum() -> Element {
return reduce(0, +)
}
}

extension Collection where Element == Int {
/// Returns the average of all elements as a Double value.
public func average() -> Double {
return reduce(0) { $0 + Double($1) } / Double(count)
}
}

extension Collection where Element == Double {
/// Returns the average of all elements as a Double value.
public func average() -> Double {
return reduce(0, +) / Double(count)
}
}

extension Array where Element: Comparable {
/// Sorts the collection in place by the order specified in the closure.
///
Expand Down
15 changes: 15 additions & 0 deletions Frameworks/HandySwift/Extensions/NSRangeExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Created by Cihat Gündüz on 11.02.19.
// Copyright © 2019 Flinesoft. All rights reserved.
//

import Foundation

extension NSRange {
/// Initializes an NSRange from a Swift String.Range when the String is provided.
public init(_ range: Range<String.Index>, in string: String) {
self.init()
self.location = string.utf16.distance(from: string.startIndex, to: range.lowerBound)
self.length = string.utf16.distance(from: range.lowerBound, to: range.upperBound)
}
}
10 changes: 10 additions & 0 deletions Frameworks/HandySwift/Extensions/StringExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ extension String {
return isEmpty ? nil : self[index(startIndex, offsetBy: Int(randomBelow: count)!)]
}

/// Returns the range containing the full String.
public var fullRange: Range<Index> {
return startIndex ..< endIndex
}

/// Returns the range as NSRange type for the full String.
public var fullNSRange: NSRange {
return NSRange(fullRange, in: self)
}

/// Create new instance with random numeric/alphabetic/alphanumeric String of given length.
///
/// - Parameters:
Expand Down
2 changes: 2 additions & 0 deletions Frameworks/HandySwift/Structs/SortedArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ public struct SortedArray<Element: Comparable> {
public func index(where predicate: (Element) -> Bool) -> Int? {
// cover trivial cases
guard !array.isEmpty else { return nil }
// swiftlint:disable all
if let first = array.first, predicate(first) { return array.startIndex }
if let last = array.last, !predicate(last) { return nil }
// swiftlint:enable all

// binary search for first matching element
var foundMatch = false
Expand Down
2 changes: 1 addition & 1 deletion Frameworks/SupportingFiles/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>2.7.1
<string>2.8.0
</string>
<key>CFBundleSignature</key>
<string>????</string>
Expand Down
2 changes: 1 addition & 1 deletion HandySwift.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "HandySwift"
s.version = "2.7.1
s.version = "2.8.0
"
s.summary = "Handy Swift features that didn't make it into the Swift standard library"

Expand Down
Loading

0 comments on commit f736ec0

Please sign in to comment.