You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.5 KiB

6 years ago
  1. // Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
  2. //
  3. // You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
  4. // copy, modify, and distribute this software in source code or binary form for use
  5. // in connection with the web services and APIs provided by Facebook.
  6. //
  7. // As with any software that integrates with the Facebook platform, your use of
  8. // this software is subject to the Facebook Developer Principles and Policies
  9. // [http://developers.facebook.com/policy/]. This copyright notice shall be
  10. // included in all copies or substantial portions of the software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. import Foundation
  19. extension AppInvite {
  20. /**
  21. A promo code for an App Invite promotion. This can be between 0 and 10 characters long, and can contain alphanumeric
  22. and spaces only.
  23. If you attempt to create a Promo Code with an invalid string literal, you will receive a runtime warning, and your
  24. code will be truncated.
  25. */
  26. public struct PromoCode {
  27. internal let rawValue: String
  28. /**
  29. Attempt to create a promo code from a string.
  30. - parameter string: The string to initialize from.
  31. */
  32. public init?(string: String) {
  33. let truncated = PromoCode.truncate(string: string)
  34. if string != truncated {
  35. return nil
  36. }
  37. rawValue = string
  38. }
  39. }
  40. }
  41. extension AppInvite.PromoCode: Hashable {
  42. /// The hash of this promo code.
  43. public var hashValue: Int {
  44. return rawValue.hashValue
  45. }
  46. /**
  47. Compare two `PromoCode`s for equality.
  48. - parameter lhs: The first promo code to compare.
  49. - parameter rhs: The second promo code to compare.
  50. - returns: Whether or not the promo codes are equal.
  51. */
  52. public static func == (lhs: AppInvite.PromoCode, rhs: AppInvite.PromoCode) -> Bool {
  53. return lhs.rawValue == rhs.rawValue
  54. }
  55. }
  56. extension AppInvite.PromoCode: ExpressibleByStringLiteral {
  57. /**
  58. Create a PromoCode from a string literal.
  59. - parameter value: The string literal to intiialize from.
  60. */
  61. public init(stringLiteral value: String) {
  62. let truncated = AppInvite.PromoCode.truncate(string: value)
  63. if truncated != value {
  64. print("Warning: Attempted to create a PromoCode from \"\(value)\" which contained invalid characters, or was too long.")
  65. }
  66. rawValue = truncated
  67. }
  68. /**
  69. Create a PromoCode from a unicode scalar literal.
  70. - parameter value: The string literal to intiialize from.
  71. */
  72. public init(unicodeScalarLiteral value: String) {
  73. self.init(stringLiteral: value)
  74. }
  75. /**
  76. Create a PromoCode from an extended grapheme cluster literal.
  77. - parameter value: The string literal to initialize from.
  78. */
  79. public init(extendedGraphemeClusterLiteral value: String) {
  80. self.init(stringLiteral: value)
  81. }
  82. }
  83. extension AppInvite.PromoCode {
  84. fileprivate static func truncate(string: String) -> String {
  85. let validCharacters = CharacterSet.alphanumerics
  86. let cleaned = string
  87. .unicodeScalars
  88. .filter({
  89. validCharacters.contains(UnicodeScalar(UInt16($0.value))!)
  90. })
  91. .map(Character.init)
  92. return String(cleaned)
  93. }
  94. }