Skip to content

Commit

Permalink
Rename to Percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 21, 2020
1 parent 3f646da commit 76480b7
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1100"
LastUpgradeVersion = "1130"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -14,9 +14,9 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Percent"
BuildableName = "Percent"
BlueprintName = "Percent"
BlueprintIdentifier = "Percentage"
BuildableName = "Percentage"
BlueprintName = "Percentage"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
Expand All @@ -28,9 +28,9 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "PercentTests"
BuildableName = "PercentTests"
BlueprintName = "PercentTests"
BlueprintIdentifier = "PercentageTests"
BuildableName = "PercentageTests"
BlueprintName = "PercentageTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
Expand All @@ -40,17 +40,15 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO"
testExecutionOrdering = "random">
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "PercentTests"
BuildableName = "PercentTests"
BlueprintName = "PercentTests"
BlueprintIdentifier = "PercentageTests"
BuildableName = "PercentageTests"
BlueprintName = "PercentageTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
Expand All @@ -76,9 +74,9 @@
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Percent"
BuildableName = "Percent"
BlueprintName = "Percent"
BlueprintIdentifier = "Percentage"
BuildableName = "Percentage"
BlueprintName = "Percentage"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
Expand Down
12 changes: 6 additions & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
import PackageDescription

let package = Package(
name: "Percent",
name: "Percentage",
products: [
.library(
name: "Percent",
name: "Percentage",
targets: [
"Percent"
"Percentage"
]
),
],
targets: [
.target(
name: "Percent"
name: "Percentage"
),
.testTarget(
name: "PercentTests",
name: "PercentageTests",
dependencies: [
"Percent"
"Percentage"
]
)
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import CoreGraphics

/**
```
import Percent
import Percentage
10% + 5.5%
//=> 15.5%
Expand All @@ -20,10 +20,10 @@ import Percent
50%.of(200)
//=> 100
Percent(50)
Percentage(50)
//=> 50%
Percent(fraction: 0.5)
Percentage(fraction: 0.5)
//=> 50%
50%.fraction
Expand All @@ -36,9 +36,9 @@ print("\(1%)")
//=> "1%"
```
*/
public struct Percent: Hashable, Codable {
public struct Percentage: Hashable, Codable {
/**
The raw percent number.
The raw percentage number.
```
10%.rawValue
Expand All @@ -48,7 +48,7 @@ public struct Percent: Hashable, Codable {
public let rawValue: Double

/**
Get the percent as a fraction.
Get the percentage as a fraction.
```
50%.fraction
Expand All @@ -58,48 +58,48 @@ public struct Percent: Hashable, Codable {
public var fraction: Double { rawValue / 100 }

/**
Create a `Percent` from a `Double`.
Create a `Percentage` from a `Double`.
```
Percent(50.5)
Percentage(50.5)
//=> 50.5%
```
*/
public init(_ percent: Double) {
self.rawValue = percent
public init(_ percentage: Double) {
self.rawValue = percentage
}

/**
Create a `Percent` from a `CGFloat`.
Create a `Percentage` from a `CGFloat`.
```
let cgFloat: CGFloat = 50.5
Percent(cgFloat)
Percentage(cgFloat)
//=> 50.5%
```
*/
public init(_ percent: CGFloat) {
self.rawValue = Double(percent)
public init(_ percentage: CGFloat) {
self.rawValue = Double(percentage)
}

/**
Create a `Percent` from an `Int`.
Create a `Percentage` from an `Int`.
```
let int = 50
Percent(int)
Percentage(int)
//=> 50%
```
*/
public init(_ percent: Int) {
self.rawValue = Double(percent)
public init(_ percentage: Int) {
self.rawValue = Double(percentage)
}

/**
Create a `Percent` from a fraction.
Create a `Percentage` from a fraction.
```
Percent(fraction: 0.5)
Percentage(fraction: 0.5)
//=> "50%"
```
*/
Expand All @@ -108,7 +108,7 @@ public struct Percent: Hashable, Codable {
}

/**
Returns how much the percent of the given value is.
Returns how much the percentage of the given value is.
```
50%.of(200)
Expand All @@ -118,19 +118,19 @@ public struct Percent: Hashable, Codable {
public func of(_ value: Double) -> Double { value * rawValue / 100 }
}

extension Percent: RawRepresentable {
extension Percentage: RawRepresentable {
public init(rawValue: Double) {
self.rawValue = rawValue
}
}

extension Percent: Comparable {
extension Percentage: Comparable {
public static func < (lhs: Self, rhs: Self) -> Bool {
lhs.rawValue < rhs.rawValue
}
}

extension Percent: CustomStringConvertible {
extension Percentage: CustomStringConvertible {
internal static var formatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .percent
Expand All @@ -145,22 +145,22 @@ extension Percent: CustomStringConvertible {
// swiftlint:disable static_operator
prefix operator -

public prefix func - (percent: Percent) -> Percent {
return Percent(-percent.rawValue)
public prefix func - (percentage: Percentage) -> Percentage {
return Percentage(-percentage.rawValue)
}

postfix operator %

public postfix func % (value: Double) -> Percent {
return Percent(value)
public postfix func % (value: Double) -> Percentage {
return Percentage(value)
}

public postfix func % (value: Int) -> Percent {
return Percent(Double(value))
public postfix func % (value: Int) -> Percentage {
return Percentage(Double(value))
}
// swiftlint:enable static_operator

extension Percent {
extension Percentage {
public static func + (lhs: Self, rhs: Self) -> Self {
Self(lhs.rawValue + rhs.rawValue)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import XCTest
@testable import Percent
@testable import Percentage

final class PercentTests: XCTestCase {
func testPercent() {
final class PercentageTests: XCTestCase {
func testPercentage() {
XCTAssertEqual(10%, 10%)
XCTAssertEqual(-10% / 2, -5%)
XCTAssertEqual(1.1%.rawValue, 1.1)
XCTAssertEqual(10% + 5.5%, 15.5%)
XCTAssertEqual((40% + 93%) * 3, 399%)
XCTAssertEqual(50%.of(200), 100)
XCTAssertEqual(Percent(50.5), 50.5%)
XCTAssertEqual(Percent(rawValue: 50.5), 50.5%)
XCTAssertEqual(Percentage(50.5), 50.5%)
XCTAssertEqual(Percentage(rawValue: 50.5), 50.5%)

let int = 50
XCTAssertEqual(Percent(int), 50%)
XCTAssertEqual(Percentage(int), 50%)

let cgFloat: CGFloat = 50.5
XCTAssertEqual(Percent(cgFloat), 50.5%)
XCTAssertEqual(Percentage(cgFloat), 50.5%)

XCTAssertEqual(Percent(fraction: 0.5), 50%)
XCTAssertEqual(Percentage(fraction: 0.5), 50%)
XCTAssertEqual(50%.fraction, 0.5)

XCTAssertTrue(30% > 25%)
Expand Down Expand Up @@ -76,7 +76,7 @@ final class PercentTests: XCTestCase {

func testCodable() {
struct Foo: Codable {
let alpha: Percent
let alpha: Percentage
}

let foo = Foo(alpha: 1%)
Expand All @@ -91,26 +91,26 @@ final class PercentTests: XCTestCase {
formatter.numberStyle = .percent

formatter.locale = Locale(identifier: "ru")
Percent.formatter = formatter
Percentage.formatter = formatter
XCTAssertEqual("\(50%)", formatter.string(for: 50%.fraction))
XCTAssertEqual("\(1%)", formatter.string(for: 1%.fraction))

formatter.locale = Locale(identifier: "tr")
Percent.formatter = formatter
Percentage.formatter = formatter
XCTAssertEqual("\(50%)", formatter.string(for: 50%.fraction))
XCTAssertEqual("\(1%)", formatter.string(for: 1%.fraction))

formatter.locale = Locale(identifier: "eu")
Percent.formatter = formatter
Percentage.formatter = formatter
XCTAssertEqual("\(50%)", formatter.string(for: 50%.fraction))
XCTAssertEqual("\(1%)", formatter.string(for: 1%.fraction))

formatter.locale = Locale(identifier: "ar")
Percent.formatter = formatter
Percentage.formatter = formatter
XCTAssertEqual("\(50%)", formatter.string(for: 50%.fraction))
XCTAssertEqual("\(1%)", formatter.string(for: 1%.fraction))

formatter.locale = Locale.current
Percent.formatter = formatter
Percentage.formatter = formatter
}
}
16 changes: 8 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Percent [![Build Status](https://travis-ci.com/sindresorhus/Percent.svg?branch=master)](https://travis-ci.com/sindresorhus/Percent)
# Percentage [![Build Status](https://travis-ci.com/sindresorhus/Percentage.svg?branch=master)](https://travis-ci.com/sindresorhus/Percentage)

> A percentage type for Swift
Expand All @@ -15,18 +15,18 @@ Makes percentages more readable and type-safe, for example, for APIs that curren
SwiftPM:

```swift
.package(url: "https://github.com/sindresorhus/Percent", from: "0.2.0")
.package(url: "https://github.com/sindresorhus/Percentage", from: "0.2.0")
```

Or just copy-paste it.


## Usage

See the [source](Sources/Percent/Percent.swift) for docs.
See the [source](Sources/Percentage/Percentage.swift) for docs.

```swift
import Percent
import Percentage

10% + 5.5%
//=> 15.5%
Expand All @@ -43,10 +43,10 @@ import Percent
50%.of(200)
//=> 100

Percent(50)
Percentage(50)
//=> 50%

Percent(fraction: 0.5)
Percentage(fraction: 0.5)
//=> 50%

50%.fraction
Expand All @@ -63,11 +63,11 @@ The type conforms to `Hashable`, `Codable`, `RawRepresentable`, `Comparable`, an

### Codable

The percent value is encoded as a single value:
The percentage value is encoded as a single value:

```swift
struct Foo: Codable {
let alpha: Percent
let alpha: Percentage
}

let foo = Foo(alpha: 1%)
Expand Down

0 comments on commit 76480b7

Please sign in to comment.