Skip to content

Commit

Permalink
Merge pull request #20 from orchetect/dev
Browse files Browse the repository at this point in the history
Added Dictionary map methods
  • Loading branch information
orchetect authored Nov 25, 2021
2 parents 8b9bd6c + ec958be commit b7329eb
Show file tree
Hide file tree
Showing 20 changed files with 306 additions and 187 deletions.
28 changes: 14 additions & 14 deletions Sources/OTCore/Abstractions/Collection Set-Like Methods.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ extension Collection where Self: RangeReplaceableCollection,
func insert(_ element: Element,
position: CollectionPosition = .default) {

if !self.contains(element) {
if !contains(element) {
switch position {
case .start:
self.insert(element, at: startIndex)
insert(element, at: startIndex)
case .end, .default:
self.append(element)
append(element)
}

}
Expand All @@ -52,23 +52,23 @@ extension Collection where Self: RangeReplaceableCollection,
@inlinable public mutating func update(with newMember: Element,
position: CollectionPosition = .default) {

if let index = self.firstIndex(of: newMember) {
if let index = firstIndex(of: newMember) {
switch position {
case .default:
self[index] = newMember
case .start:
self.remove(at: index)
self.insert(newMember, at: startIndex)
remove(at: index)
insert(newMember, at: startIndex)
case .end:
self.remove(at: index)
self.append(newMember)
remove(at: index)
append(newMember)
}
} else {
switch position {
case .start:
self.insert(newMember, at: startIndex)
insert(newMember, at: startIndex)
case .end, .default:
self.append(newMember)
append(newMember)
}
}

Expand All @@ -78,7 +78,7 @@ extension Collection where Self: RangeReplaceableCollection,
/// Similar behavior to a Set, but removes all instances of the element.
@inlinable public mutating func removeAll(_ member: Element) {

self.removeAll(where: { $0 == member })
removeAll(where: { $0 == member })

}

Expand All @@ -101,8 +101,8 @@ extension Collection where Self: RangeReplaceableCollection,
@inlinable public mutating func formUnion<S>(_ other: S) where Element == S.Element, S : Sequence {

other.forEach {
if !self.contains($0) {
self.append($0)
if !contains($0) {
append($0)
}
}

Expand All @@ -125,7 +125,7 @@ extension Collection where Self: RangeReplaceableCollection,
@inlinable public mutating func formUnion<S>(updating other: S) where Element == S.Element, S : Sequence {

other.forEach {
self.update(with: $0)
update(with: $0)
}

}
Expand Down
2 changes: 1 addition & 1 deletion Sources/OTCore/Abstractions/String Title Case.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extension String {
public var titleCased: String {

var words =
self.localizedCapitalized
localizedCapitalized
.split(separator: " ")
.map({ String($0) })

Expand Down
6 changes: 3 additions & 3 deletions Sources/OTCore/Abstractions/String Wrapped.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ extension String {
/// Same as `self.wrapped(with: .parentheses)`
@inlinable public var parens: Self {

self.wrapped(with: .parentheses)
wrapped(with: .parentheses)

}

Expand All @@ -66,7 +66,7 @@ extension String {
/// Same as `self.wrapped(with: .singleQuotes)`
@inlinable public var singleQuoted: Self {

self.wrapped(with: .singleQuotes)
wrapped(with: .singleQuotes)

}

Expand All @@ -75,7 +75,7 @@ extension String {
/// Same as `self.wrapped(with: .quotes)`
@inlinable public var quoted: Self {

self.wrapped(with: .quotes)
wrapped(with: .quotes)

}

Expand Down
2 changes: 1 addition & 1 deletion Sources/OTCore/Extensions/AppKit/URL and AppKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension URL {

guard isFileURL, fileExists else { return nil }

return NSWorkspace.shared.icon(forFile: self.path)
return NSWorkspace.shared.icon(forFile: path)

}

Expand Down
4 changes: 2 additions & 2 deletions Sources/OTCore/Extensions/CoreGraphics/CGPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ extension CGPoint {
/// Returns the distance between two coordinate points.
@inlinable public func distance(to other: CGPoint) -> CGFloat {

hypot(other.x - self.x, other.y - self.y)
hypot(other.x - x, other.y - y)

}

Expand All @@ -65,7 +65,7 @@ extension CGPoint {
/// To calculate the where cardinal North is the origin (0°), use `cardinalAngle(to:)` instead.
@inlinable public func angle(to other: CGPoint) -> CGFloat {

let calc = atan2(other.y - self.y, other.x - self.x).radiansToDegrees
let calc = atan2(other.y - y, other.x - x).radiansToDegrees

if calc < 0 {
return calc + 360.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ extension FloatingPoint where Self : FloatingPointPowerComputable {
/// If `decimalPlaces` <= 0, then `trunc(self)` is returned.
public mutating func truncate(decimalPlaces: Int) {

self = self.truncated(decimalPlaces: decimalPlaces)
self = truncated(decimalPlaces: decimalPlaces)

}

/// **OTCore:**
/// Truncates decimal places to `decimalPlaces` number of decimal places.
///
/// If `decimalPlaces` <= 0, then trunc(self) is returned.
/// If `decimalPlaces` <= 0, then `trunc(self)` is returned.
public func truncated(decimalPlaces: Int) -> Self {

if decimalPlaces < 1 {
Expand Down
16 changes: 8 additions & 8 deletions Sources/OTCore/Extensions/Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extension Data {
/// Returns nil if Data is not the correct length.
public func toInt8() -> Int8? {

guard self.count == 1 else { return nil }
guard count == 1 else { return nil }

var int = UInt8()
withUnsafeMutablePointer(to: &int) {
Expand Down Expand Up @@ -131,8 +131,8 @@ extension Data {
/// Returns nil if Data is not the correct length.
public func toUInt8() -> UInt8? {

guard self.count == 1 else { return nil }
return self.first
guard count == 1 else { return nil }
return first

}

Expand Down Expand Up @@ -243,7 +243,7 @@ extension Data {
/// Returns nil if Data is != 4 bytes.
public func toFloat32(from endianness: NumberEndianness = .platformDefault) -> Float32? {

guard self.count == 4 else { return nil }
guard count == 4 else { return nil }

// define conversions

Expand Down Expand Up @@ -359,7 +359,7 @@ extension Data {
/// Returns nil if Data is != 8 bytes.
public func toDouble(from endianness: NumberEndianness = .platformDefault) -> Double? {

guard self.count == 8 else { return nil }
guard count == 8 else { return nil }

// define conversions

Expand Down Expand Up @@ -432,8 +432,8 @@ extension FixedWidthInteger {

switch endianness {
case .platformDefault: int = self
case .littleEndian: int = self.littleEndian
case .bigEndian: int = self.bigEndian
case .littleEndian: int = littleEndian
case .bigEndian: int = bigEndian
}

withUnsafeBytes(of: &int) { rawBuffer in
Expand Down Expand Up @@ -510,7 +510,7 @@ extension String {
/// Returns a Data representation of a String, defaulting to utf8 encoding.
public func toData(using encoding: String.Encoding = .utf8) -> Data? {

self.data(using: encoding)
data(using: encoding)

}

Expand Down
4 changes: 2 additions & 2 deletions Sources/OTCore/Extensions/Foundation/Decimal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ extension Decimal {
public mutating func round(_ rule: NSDecimalNumber.RoundingMode = .plain,
decimalPlaces: Int) {

self = self.rounded(rule, decimalPlaces: decimalPlaces)
self = rounded(rule, decimalPlaces: decimalPlaces)

}

Expand All @@ -226,7 +226,7 @@ extension Decimal {
/// Replaces this value by truncating it to `decimalPlaces` number of decimal places.
public mutating func truncate(decimalPlaces: Int) {

self = self.truncated(decimalPlaces: decimalPlaces)
self = truncated(decimalPlaces: decimalPlaces)

}

Expand Down
10 changes: 5 additions & 5 deletions Sources/OTCore/Extensions/Foundation/NSAttributedString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ extension NSAttributedString {
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = alignment

guard let copy = self.mutableCopy() as? NSMutableAttributedString
guard let copy = mutableCopy() as? NSMutableAttributedString
else {
print("Could not create mutable NSAttributedString copy.")
return self
}

copy.addAttributes([ .paragraphStyle : paragraph ],
range: NSRange(location: 0, length: self.length))
copy.addAttributes([.paragraphStyle : paragraph],
range: NSRange(location: 0, length: length))

return copy

Expand All @@ -44,8 +44,8 @@ extension NSMutableAttributedString {
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = alignment

self.addAttributes([ .paragraphStyle : paragraph ],
range: NSRange(location: 0, length: self.length))
addAttributes([.paragraphStyle : paragraph],
range: NSRange(location: 0, length: length))

}

Expand Down
22 changes: 11 additions & 11 deletions Sources/OTCore/Extensions/Foundation/String and CharacterSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ extension StringProtocol {

// iterate over characters

var currentGroupingStartIndex: Self.Index? = self.indices.first
var currentGroupingStartIndex: Self.Index? = indices.first
var lastCharSetIndex: Int? = nil

for idx in self.indices {
for idx in indices {

// helper

Expand All @@ -53,13 +53,13 @@ extension StringProtocol {
let firstMatchingCharSetIndex = characterSets.firstIndex(where: { $0.contains(scalar) })

if lastCharSetIndex != firstMatchingCharSetIndex
&& idx != self.indices.first {
&& idx != indices.first {

// grouping separator here

// close off previous grouping and append to result array

closeGrouping(closingIdx: self.index(before: idx))
closeGrouping(closingIdx: index(before: idx))

// start new grouping

Expand All @@ -69,8 +69,8 @@ extension StringProtocol {

// close off if we've reached the end of the string

if idx == self.indices.last {
if idx == self.indices.first {
if idx == indices.last {
if idx == indices.first {
lastCharSetIndex = firstMatchingCharSetIndex
}

Expand Down Expand Up @@ -103,7 +103,7 @@ extension StringProtocol {
///
public func only(_ characterSet: CharacterSet) -> String {

self.map { characterSet.contains(UnicodeScalar("\($0)")!) ? "\($0)" : "" }
map { characterSet.contains(UnicodeScalar("\($0)")!) ? "\($0)" : "" }
.joined()

}
Expand All @@ -112,23 +112,23 @@ extension StringProtocol {
/// Returns a string preserving only characters from the passed string and removing all other characters.
public func only(characters: String) -> String {

self.only(CharacterSet(charactersIn: characters))
only(CharacterSet(charactersIn: characters))

}

/// **OTCore:**
/// Returns a string containing only alphanumeric characters and removing all other characters.
public var onlyAlphanumerics: String {

self.only(.alphanumerics)
only(.alphanumerics)

}

/// **OTCore:**
/// Returns a string removing all characters from the passed CharacterSet.
public func removing(_ characterSet: CharacterSet) -> String {

self.components(separatedBy: characterSet)
components(separatedBy: characterSet)
.joined()

}
Expand All @@ -137,7 +137,7 @@ extension StringProtocol {
/// Returns a string removing all characters from the passed string.
public func removing(characters: String) -> String {

self.components(separatedBy: CharacterSet(charactersIn: characters))
components(separatedBy: CharacterSet(charactersIn: characters))
.joined()

}
Expand Down
Loading

0 comments on commit b7329eb

Please sign in to comment.