Skip to content
This repository was archived by the owner on Mar 21, 2022. It is now read-only.

Commit 3b20322

Browse files
committed
Add header documentation
1 parent d85eedb commit 3b20322

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

Sources/PostalCodeValidator/PostalCodeValidator.swift

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
// This file was automatically generated and should not be edited.
22
import Foundation
33

4+
/// A validator for postal codes.
45
public final class PostalCodeValidator {
6+
/**
7+
* A set of available region codes.
8+
*
9+
* Each element of the returned set is an two-letter
10+
* ISO 3166-2 region code
11+
* (for example, "US" for the United States of America).
12+
*/
513
public class var isoRegionCodes: Set<String> {
614
return Set(patternsByRegion.keys)
715
}
@@ -257,10 +265,22 @@ public final class PostalCodeValidator {
257265
"ZW": #"\d{5}"#,
258266
]
259267

268+
/// The locale of the validator.
260269
public private(set) var locale: Locale
261270
private var regularExpression: NSRegularExpression
262271

263-
public init?(locale: Locale = .autoupdatingCurrent) {
272+
/**
273+
* Creates a postal code validator for the region of the provided locale.
274+
*
275+
* If the locale doesn't specify a valid region,
276+
* or the region isn't supported,
277+
* this initializer returns `nil`.
278+
*
279+
* - Parameters:
280+
* - locale: The locale whose `regionCode` property is used to determine
281+
* the appropriate postal code validation rules.
282+
*/
283+
public init?(locale: Locale = .current) {
264284
self.locale = locale
265285
guard let regionCode = locale.regionCode,
266286
let pattern = PostalCodeValidator.patternsByRegion[regionCode],
@@ -270,6 +290,13 @@ public final class PostalCodeValidator {
270290
self.regularExpression = regex
271291
}
272292

293+
/**
294+
* Returns whether a postal code is valid for the configured region.
295+
*
296+
* - Parameters:
297+
* - postalCode: The postal code.
298+
* - Returns: `true` if valid, otherwise `false`.
299+
*/
273300
public func validate(postalCode: String) -> Bool {
274301
return regularExpression.rangeOfFirstMatch(in: postalCode, options: [], range: NSRange(postalCode.startIndex..<postalCode.endIndex, in: postalCode)).location != NSNotFound
275302
}

Sources/PostalCodeValidator/PostalCodeValidator.swift.gyb

+28-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ sys.setdefaultencoding('utf8')
1212
}%
1313
% with open(os.getcwd() + "/../../Resources/postalData.json") as file:
1414
%{ data = json.load(file) }%
15+
/// A validator for postal codes.
1516
public final class PostalCodeValidator {
17+
/**
18+
* A set of available region codes.
19+
*
20+
* Each element of the returned set is an two-letter
21+
* ISO 3166-2 region code
22+
* (for example, "US" for the United States of America).
23+
*/
1624
public class var isoRegionCodes: Set<String> {
1725
return Set(patternsByRegion.keys)
1826
}
@@ -32,10 +40,22 @@ public final class PostalCodeValidator {
3240
% end
3341
]
3442

43+
/// The locale of the validator.
3544
public private(set) var locale: Locale
3645
private var regularExpression: NSRegularExpression
3746

38-
public init?(locale: Locale = .autoupdatingCurrent) {
47+
/**
48+
* Creates a postal code validator for the region of the provided locale.
49+
*
50+
* If the locale doesn't specify a valid region,
51+
* or the region isn't supported,
52+
* this initializer returns `nil`.
53+
*
54+
* - Parameters:
55+
* - locale: The locale whose `regionCode` property is used to determine
56+
* the appropriate postal code validation rules.
57+
*/
58+
public init?(locale: Locale = .current) {
3959
self.locale = locale
4060
guard let regionCode = locale.regionCode,
4161
let pattern = PostalCodeValidator.patternsByRegion[regionCode],
@@ -45,6 +65,13 @@ public final class PostalCodeValidator {
4565
self.regularExpression = regex
4666
}
4767

68+
/**
69+
* Returns whether a postal code is valid for the configured region.
70+
*
71+
* - Parameters:
72+
* - postalCode: The postal code.
73+
* - Returns: `true` if valid, otherwise `false`.
74+
*/
4875
public func validate(postalCode: String) -> Bool {
4976
return regularExpression.rangeOfFirstMatch(in: postalCode, options: [], range: NSRange(postalCode.startIndex..<postalCode.endIndex, in: postalCode)).location != NSNotFound
5077
}

0 commit comments

Comments
 (0)