Skip to content
This repository was archived by the owner on Jul 2, 2018. It is now read-only.

Commit e137751

Browse files
committed
Merge branch 'release/1.1.0'
2 parents 3a3b7a3 + 13d4281 commit e137751

12 files changed

+209
-108
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 1.1.0
2+
1. [[MNY-16](https://github.com/danthorpe/Money/pull/16)]: Grab bag of minor issues post 1.0 release.
3+
* Cleans up some minor mistakes (spelling etc).
4+
* Adds `NSCoding` conformance to `FXQuote` - so it can be persisted if needed.
5+
* Adds `FXRemoteProviderType.quote(: BaseMoney, completion: Result<(BaseMoney, FXQuote, CounterMoney), FXError> -> Void) -> NSURLSessionDataTask` API. This is the nuts and bolts of the FX provider now. It returns as its result, the base money (i.e. the input), the quote (which includes the rate), and the counter money (i.e. the output). The `fx` method still exists, and it just unwraps the tuple to return the counter money. See the updated README.
6+
2. [[MNY-17](https://github.com/danthorpe/Money/pull/17)]: There was an oversight in the functions in `DecimalNumberType` which accepts `NSDecimalNumberBehaviors` as an argument. These were unnecessary so I’ve removed them. Hence the minor version bump.
17

28
# 1.0.0
39
🎉🐝 Initial release of Money.

Money.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "Money"
3-
s.version = "1.0.0"
3+
s.version = "1.1.0"
44
s.summary = "Swift types for working with Money."
55
s.description = <<-DESC
66

Money/Decimal/Decimal.swift

+11-10
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import ValueCoding
3636
and scale rules for base 10 decimal arithmetic.
3737
*/
3838
public struct _Decimal<Behavior: DecimalNumberBehaviorType>: DecimalNumberType {
39+
3940
public typealias DecimalNumberBehavior = Behavior
4041

4142
/// Access the underlying decimal storage.
@@ -100,8 +101,8 @@ public struct _Decimal<Behavior: DecimalNumberBehaviorType>: DecimalNumberType {
100101
- returns: another instance of this type.
101102
*/
102103
@warn_unused_result
103-
public func subtract(other: _Decimal, withBehaviors behaviors: NSDecimalNumberBehaviors) -> _Decimal {
104-
return _Decimal(storage: storage.subtract(other.storage, withBehaviors: behaviors))
104+
public func subtract(other: _Decimal) -> _Decimal {
105+
return _Decimal(storage: storage.subtract(other.storage, withBehaviors: DecimalNumberBehavior.decimalNumberBehaviors))
105106
}
106107

107108
/**
@@ -112,8 +113,8 @@ public struct _Decimal<Behavior: DecimalNumberBehaviorType>: DecimalNumberType {
112113
- returns: another instance of this type.
113114
*/
114115
@warn_unused_result
115-
public func add(other: _Decimal, withBehaviors behaviors: NSDecimalNumberBehaviors) -> _Decimal {
116-
return _Decimal(storage: storage.add(other.storage, withBehaviors: behaviors))
116+
public func add(other: _Decimal) -> _Decimal {
117+
return _Decimal(storage: storage.add(other.storage, withBehaviors: DecimalNumberBehavior.decimalNumberBehaviors))
117118
}
118119

119120
/**
@@ -124,8 +125,8 @@ public struct _Decimal<Behavior: DecimalNumberBehaviorType>: DecimalNumberType {
124125
- returns: another instance of this type.
125126
*/
126127
@warn_unused_result
127-
public func multiplyBy(other: _Decimal, withBehaviors behaviors: NSDecimalNumberBehaviors) -> _Decimal {
128-
return _Decimal(storage: storage.multiplyBy(other.storage, withBehaviors: behaviors))
128+
public func multiplyBy(other: _Decimal) -> _Decimal {
129+
return _Decimal(storage: storage.multiplyBy(other.storage, withBehaviors: DecimalNumberBehavior.decimalNumberBehaviors))
129130
}
130131

131132
/**
@@ -136,8 +137,8 @@ public struct _Decimal<Behavior: DecimalNumberBehaviorType>: DecimalNumberType {
136137
- returns: another instance of this type.
137138
*/
138139
@warn_unused_result
139-
public func divideBy(other: _Decimal, withBehaviors behaviors: NSDecimalNumberBehaviors) -> _Decimal {
140-
return _Decimal(storage: storage.divideBy(other.storage, withBehaviors: behaviors))
140+
public func divideBy(other: _Decimal) -> _Decimal {
141+
return _Decimal(storage: storage.divideBy(other.storage, withBehaviors: DecimalNumberBehavior.decimalNumberBehaviors))
141142
}
142143

143144
/**
@@ -148,8 +149,8 @@ public struct _Decimal<Behavior: DecimalNumberBehaviorType>: DecimalNumberType {
148149
- returns: another instance of this type.
149150
*/
150151
@warn_unused_result
151-
public func remainder(other: _Decimal, withBehaviors behaviors: NSDecimalNumberBehaviors) -> _Decimal {
152-
return _Decimal(storage: storage.remainder(other.storage, withBehaviors: behaviors))
152+
public func remainder(other: _Decimal) -> _Decimal {
153+
return _Decimal(storage: storage.remainder(other.storage, withBehaviors: DecimalNumberBehavior.decimalNumberBehaviors))
153154
}
154155
}
155156

Money/Decimal/DecimalNumberType.swift

+64-49
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,53 @@
2727

2828
import Foundation
2929

30+
/**
31+
32+
# DecimalNumberBehaviorType
33+
34+
Defines the decimal number behavior, i.e. `NSDecimalNumberBehaviors`
35+
of the type.
36+
37+
*/
3038
public protocol DecimalNumberBehaviorType {
3139

3240
/// Specify the decimal number (i.e. rounding, scale etc) for base 10 calculations
3341
static var decimalNumberBehaviors: NSDecimalNumberBehaviors { get }
3442
}
3543

36-
public struct DecimalNumberBehavior {
44+
/**
3745

38-
private static func behaviorWithRoundingMode(mode: NSRoundingMode) -> NSDecimalNumberBehaviors {
39-
return NSDecimalNumberHandler(roundingMode: mode, scale: 38, raiseOnExactness: false, raiseOnOverflow: true, raiseOnUnderflow: true, raiseOnDivideByZero: true)
40-
}
46+
# DecimalNumberBehavior
47+
48+
This is a name space of types which conform to `DecimalNumberBehaviorType`
49+
with common rounding modes. All have maximum precision, of 38 significant
50+
digits.
51+
*/
52+
public struct DecimalNumberBehavior {
4153

54+
/// Plain rounding mode
4255
public struct Plain: DecimalNumberBehaviorType {
4356
public static let decimalNumberBehaviors = DecimalNumberBehavior.behaviorWithRoundingMode(.RoundPlain)
4457
}
4558

59+
/// Round down mode
4660
public struct RoundDown: DecimalNumberBehaviorType {
4761
public static let decimalNumberBehaviors = DecimalNumberBehavior.behaviorWithRoundingMode(.RoundDown)
4862
}
4963

64+
/// Round up mode
5065
public struct RoundUp: DecimalNumberBehaviorType {
5166
public static let decimalNumberBehaviors = DecimalNumberBehavior.behaviorWithRoundingMode(.RoundUp)
5267
}
5368

69+
/// Bankers rounding mode, see `NSRoundingMode.RoundBankers` for info.
5470
public struct Bankers: DecimalNumberBehaviorType {
5571
public static let decimalNumberBehaviors = DecimalNumberBehavior.behaviorWithRoundingMode(.RoundBankers)
5672
}
73+
74+
private static func behaviorWithRoundingMode(mode: NSRoundingMode, scale: Int16 = 38) -> NSDecimalNumberBehaviors {
75+
return NSDecimalNumberHandler(roundingMode: mode, scale: 38, raiseOnExactness: false, raiseOnOverflow: true, raiseOnUnderflow: true, raiseOnDivideByZero: true)
76+
}
5777
}
5878

5979

@@ -93,82 +113,98 @@ public protocol DecimalNumberType: SignedNumberType, IntegerLiteralConvertible,
93113
Subtract a matching `DecimalNumberType` from the receiver.
94114

95115
- parameter other: another instance of this type.
96-
- parameter behaviors: an optional NSDecimalNumberBehaviors?
97116
- returns: another instance of this type.
98117
*/
99118
@warn_unused_result
100-
func subtract(_: Self, withBehaviors: NSDecimalNumberBehaviors) -> Self
119+
func subtract(_: Self) -> Self
101120

102121
/**
103122
Add a matching `DecimalNumberType` to the receiver.
104123

105124
- parameter other: another instance of this type.
106-
- parameter behaviors: an optional NSDecimalNumberBehaviors?
107125
- returns: another instance of this type.
108126
*/
109127
@warn_unused_result
110-
func add(_: Self, withBehaviors: NSDecimalNumberBehaviors) -> Self
128+
func add(_: Self) -> Self
111129

112130
/**
113131
Multiply a matching `DecimalNumberType` with the receiver.
114132

115133
- parameter other: another instance of this type.
116-
- parameter behaviors: an optional NSDecimalNumberBehaviors?
117134
- returns: another instance of this type.
118135
*/
119136
@warn_unused_result
120-
func multiplyBy(_: Self, withBehaviors: NSDecimalNumberBehaviors) -> Self
137+
func multiplyBy(_: Self) -> Self
121138

122139
/**
123140
Multiply another `DecimalNumberType` with the receiver. The other
124141
`DecimalNumberType` must have the same underlying `DecimalStorageType` as
125142
this `DecimalNumberType`.
126143

127144
- parameter other: another `DecimalNumberType` value of different type.
128-
- parameter behaviors: an optional NSDecimalNumberBehaviors?
129145
- returns: a different `DecimalNumberType` value.
130146
*/
131147
@warn_unused_result
132-
func multiplyBy<Other: DecimalNumberType where Other.DecimalStorageType == DecimalStorageType>(_: Other, withBehaviors: NSDecimalNumberBehaviors) -> Other
148+
func multiplyBy<Other: DecimalNumberType where Other.DecimalStorageType == DecimalStorageType>(_: Other) -> Other
133149

134150
/**
135151
Divide the receiver by a matching `DecimalNumberType`.
136152

137153
- parameter other: another instance of this type.
138-
- parameter behaviors: an optional NSDecimalNumberBehaviors?
139154
- returns: another instance of this type.
140155
*/
141156
@warn_unused_result
142-
func divideBy(_: Self, withBehaviors: NSDecimalNumberBehaviors) -> Self
157+
func divideBy(_: Self) -> Self
143158

144159
/**
145160
Divide the receiver by another `DecimalNumberType`. The other
146161
`DecimalNumberType` must have the same underlying `DecimalStorageType` as
147162
this `DecimalNumberType`.
148163

149164
- parameter other: another `DecimalNumberType` value of different type.
150-
- parameter behaviors: an optional NSDecimalNumberBehaviors?
151165
- returns: another instance of this type.
152166
*/
153167
@warn_unused_result
154-
func divideBy<Other: DecimalNumberType where Other.DecimalStorageType == DecimalStorageType>(_: Other, withBehaviors: NSDecimalNumberBehaviors) -> Other
168+
func divideBy<Other: DecimalNumberType where Other.DecimalStorageType == DecimalStorageType>(_: Other) -> Other
155169

156170
/**
157171
The remainder of dividing another `DecimalNumberType` into the receiver.
158172

159173
- parameter other: another instance of this type.
160-
- parameter behaviors: an optional NSDecimalNumberBehaviors?
161174
- returns: another instance of this type.
162175
*/
163176
@warn_unused_result
164-
func remainder(_: Self, withBehaviors: NSDecimalNumberBehaviors) -> Self
177+
func remainder(_: Self) -> Self
165178
}
166179

180+
public extension DecimalNumberType where DecimalStorageType == NSDecimalNumber {
181+
182+
@warn_unused_result
183+
func multiplyBy<Other: DecimalNumberType where Other.DecimalStorageType == NSDecimalNumber>(other: Other) -> Other {
184+
return Other(storage: storage.multiplyBy(other.storage, withBehaviors: Other.DecimalNumberBehavior.decimalNumberBehaviors) )
185+
}
186+
187+
@warn_unused_result
188+
func divideBy<Other: DecimalNumberType where Other.DecimalStorageType == NSDecimalNumber>(other: Other) -> Other {
189+
return Other(storage: storage.divideBy(other.storage, withBehaviors: Other.DecimalNumberBehavior.decimalNumberBehaviors))
190+
}
191+
}
192+
193+
extension DecimalNumberType where Self.IntegerLiteralType == Int {
194+
195+
/// Get the reciprocal of the receiver.
196+
public var reciprocal: Self {
197+
return Self(integerLiteral: 1).divideBy(self)
198+
}
199+
}
200+
201+
// MARK: - Operators
202+
167203
// MARK: - Subtraction
168204

169205
@warn_unused_result
170206
public func -<T: DecimalNumberType>(lhs: T, rhs: T) -> T {
171-
return lhs.subtract(rhs, withBehaviors: T.DecimalNumberBehavior.decimalNumberBehaviors)
207+
return lhs.subtract(rhs)
172208
}
173209

174210
@warn_unused_result
@@ -191,18 +227,11 @@ public func -<T: DecimalNumberType>(lhs: T.FloatLiteralType, rhs: T) -> T {
191227
return T(floatLiteral: lhs) - rhs
192228
}
193229

194-
// MARK: - Remainder
195-
196-
@warn_unused_result
197-
public func %<T: DecimalNumberType>(lhs: T, rhs: T) -> T {
198-
return lhs.remainder(rhs, withBehaviors: T.DecimalNumberBehavior.decimalNumberBehaviors)
199-
}
200-
201230
// MARK: - Addition
202231

203232
@warn_unused_result
204233
public func +<T: DecimalNumberType>(lhs: T, rhs: T) -> T {
205-
return lhs.add(rhs, withBehaviors: T.DecimalNumberBehavior.decimalNumberBehaviors)
234+
return lhs.add(rhs)
206235
}
207236

208237
@warn_unused_result
@@ -229,7 +258,7 @@ public func +<T: DecimalNumberType>(lhs: T.FloatLiteralType, rhs: T) -> T {
229258

230259
@warn_unused_result
231260
public func *<T: DecimalNumberType>(lhs: T, rhs: T) -> T {
232-
return lhs.multiplyBy(rhs, withBehaviors: T.DecimalNumberBehavior.decimalNumberBehaviors)
261+
return lhs.multiplyBy(rhs)
233262
}
234263

235264
@warn_unused_result
@@ -257,14 +286,14 @@ public func *<T, V where
257286
T: DecimalNumberType,
258287
V: DecimalNumberType,
259288
T.DecimalStorageType == V.DecimalStorageType>(lhs: T, rhs: V) -> V {
260-
return lhs.multiplyBy(rhs, withBehaviors: V.DecimalNumberBehavior.decimalNumberBehaviors)
289+
return lhs.multiplyBy(rhs)
261290
}
262291

263292
// MARK: - Division
264293

265294
@warn_unused_result
266295
public func /<T: DecimalNumberType>(lhs: T, rhs: T) -> T {
267-
return lhs.divideBy(rhs, withBehaviors: T.DecimalNumberBehavior.decimalNumberBehaviors)
296+
return lhs.divideBy(rhs)
268297
}
269298

270299
@warn_unused_result
@@ -282,28 +311,14 @@ public func /<T, V where
282311
T: DecimalNumberType,
283312
V: DecimalNumberType,
284313
T.DecimalStorageType == V.DecimalStorageType>(lhs: T, rhs: V) -> V {
285-
return lhs.divideBy(rhs, withBehaviors: V.DecimalNumberBehavior.decimalNumberBehaviors)
286-
}
287-
288-
extension DecimalNumberType where DecimalStorageType == NSDecimalNumber {
289-
290-
@warn_unused_result
291-
public func multiplyBy<Other: DecimalNumberType where Other.DecimalStorageType == NSDecimalNumber>(other: Other, withBehaviors behaviors: NSDecimalNumberBehaviors) -> Other {
292-
return Other(storage: storage.multiplyBy(other.storage, withBehaviors: behaviors))
293-
}
294-
295-
@warn_unused_result
296-
public func divideBy<Other: DecimalNumberType where Other.DecimalStorageType == NSDecimalNumber>(other: Other, withBehaviors behaviors: NSDecimalNumberBehaviors) -> Other {
297-
return Other(storage: storage.divideBy(other.storage, withBehaviors: behaviors))
298-
}
314+
return lhs.divideBy(rhs)
299315
}
300316

301-
extension DecimalNumberType where Self.IntegerLiteralType == Int {
317+
// MARK: - Remainder
302318

303-
/// Get the reciprocal of the receiver.
304-
public var reciprocal: Self {
305-
return Self(integerLiteral: 1).divideBy(self, withBehaviors: DecimalNumberBehavior.decimalNumberBehaviors)
306-
}
319+
@warn_unused_result
320+
public func %<T: DecimalNumberType>(lhs: T, rhs: T) -> T {
321+
return lhs.remainder(rhs)
307322
}
308323

309324

0 commit comments

Comments
 (0)