From 21d6716aafe35941d8f459debfc8d6e6d17f87b6 Mon Sep 17 00:00:00 2001 From: David Jennes Date: Sun, 7 Feb 2021 21:50:42 +0100 Subject: [PATCH] Make filters public --- CHANGELOG.md | 3 + Sources/StencilSwiftKit/Filters+Numbers.swift | 8 +-- Sources/StencilSwiftKit/Filters+Strings.swift | 66 +++++++++---------- Sources/StencilSwiftKit/Filters.swift | 12 ++-- 4 files changed, 46 insertions(+), 43 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9c33135..4f01b0ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,9 @@ * Switched the whole project over to use Swift Package Manager, restructuring some of the internals in the process. [David Jennes](https://github.com/djbe) [#130](https://github.com/SwiftGen/StencilSwiftKit/pull/130) +* Made the filter implementations public, so they can be used in other libraries. + [David Jennes](https://github.com/djbe) + [#136](https://github.com/SwiftGen/StencilSwiftKit/pull/136) ## 2.7.2 diff --git a/Sources/StencilSwiftKit/Filters+Numbers.swift b/Sources/StencilSwiftKit/Filters+Numbers.swift index 3d23c41a..a7a9be18 100644 --- a/Sources/StencilSwiftKit/Filters+Numbers.swift +++ b/Sources/StencilSwiftKit/Filters+Numbers.swift @@ -7,19 +7,19 @@ import Foundation import Stencil -extension Filters { +public extension Filters { enum Numbers { - static func hexToInt(_ value: Any?) throws -> Any? { + public static func hexToInt(_ value: Any?) throws -> Any? { guard let value = value as? String else { throw Filters.Error.invalidInputType } return Int(value, radix: 16) } - static func int255toFloat(_ value: Any?) throws -> Any? { + public static func int255toFloat(_ value: Any?) throws -> Any? { guard let value = value as? Int else { throw Filters.Error.invalidInputType } return Float(value) / Float(255.0) } - static func percent(_ value: Any?) throws -> Any? { + public static func percent(_ value: Any?) throws -> Any? { guard let value = value as? Float else { throw Filters.Error.invalidInputType } let percent = Int(value * 100.0) diff --git a/Sources/StencilSwiftKit/Filters+Strings.swift b/Sources/StencilSwiftKit/Filters+Strings.swift index 377252e8..067232ab 100644 --- a/Sources/StencilSwiftKit/Filters+Strings.swift +++ b/Sources/StencilSwiftKit/Filters+Strings.swift @@ -10,22 +10,22 @@ import Stencil // MARK: - Strings Filters -extension Filters { +public extension Filters { enum Strings { } } -enum RemoveNewlinesModes: String { +public enum RemoveNewlinesModes: String { case all, leading } -enum SwiftIdentifierModes: String { +public enum SwiftIdentifierModes: String { case normal, pretty } // MARK: - String Filters: Boolean filters -extension Filters.Strings { +public extension Filters.Strings { /// Checks if the given string contains given substring /// /// - Parameters: @@ -71,7 +71,7 @@ extension Filters.Strings { // MARK: - String Filters: Lettercase filters -extension Filters.Strings { +public extension Filters.Strings { /// Lowers the first letter of the string /// e.g. "People picker" gives "people picker", "Sports Stats" gives "sports Stats" static func lowerFirstLetter(_ value: Any?) throws -> Any? { @@ -179,10 +179,10 @@ extension Filters.Strings { } return result } +} - // MARK: Private - - private static func containsAnyLowercasedChar(_ string: String) throws -> Bool { +private extension Filters.Strings { + static func containsAnyLowercasedChar(_ string: String) throws -> Bool { let lowercaseCharRegex = try NSRegularExpression(pattern: "[a-z]", options: .dotMatchesLineSeparators) let fullRange = NSRange(location: 0, length: string.unicodeScalars.count) return lowercaseCharRegex.firstMatch(in: string, options: .reportCompletion, range: fullRange) != nil @@ -196,7 +196,7 @@ extension Filters.Strings { /// - arguments: the arguments to the function; expecting zero /// - Returns: the string with first letter being uppercased /// - Throws: FilterError.invalidInputType if the value parameter isn't a string - private static func upperFirstLetter(_ value: String) -> String { + static func upperFirstLetter(_ value: String) -> String { guard let first = value.unicodeScalars.first else { return value } return String(first).uppercased() + String(value.unicodeScalars.dropFirst()) } @@ -205,7 +205,7 @@ extension Filters.Strings { /// /// - Parameter string: The string to snake_case /// - Returns: The string snake cased from either snake_cased or camelCased string. - private static func snakecase(_ string: String) throws -> String { + static func snakecase(_ string: String) throws -> String { let longUpper = try NSRegularExpression(pattern: "([A-Z\\d]+)([A-Z][a-z])", options: .dotMatchesLineSeparators) let camelCased = try NSRegularExpression(pattern: "([a-z\\d])([A-Z])", options: .dotMatchesLineSeparators) @@ -228,26 +228,7 @@ extension Filters.Strings { // MARK: - String Filters: Mutation filters -extension Filters.Strings { - fileprivate static let reservedKeywords = [ - "associatedtype", "class", "deinit", "enum", "extension", - "fileprivate", "func", "import", "init", "inout", "internal", - "let", "open", "operator", "private", "protocol", "public", - "static", "struct", "subscript", "typealias", "var", "break", - "case", "continue", "default", "defer", "do", "else", - "fallthrough", "for", "guard", "if", "in", "repeat", "return", - "switch", "where", "while", "as", "Any", "catch", "false", "is", - "nil", "rethrows", "super", "self", "Self", "throw", "throws", - "true", "try", "_", "#available", "#colorLiteral", "#column", - "#else", "#elseif", "#endif", "#file", "#fileLiteral", - "#function", "#if", "#imageLiteral", "#line", "#selector", - "#sourceLocation", "associativity", "convenience", "dynamic", - "didSet", "final", "get", "infix", "indirect", "lazy", "left", - "mutating", "none", "nonmutating", "optional", "override", - "postfix", "precedence", "prefix", "Protocol", "required", - "right", "set", "Type", "unowned", "weak", "willSet" - ] - +public extension Filters.Strings { static func escapeReservedKeywords(value: Any?) throws -> Any? { let string = try Filters.parseString(from: value) return escapeReservedKeywords(in: string) @@ -350,10 +331,29 @@ extension Filters.Strings { .trimmingCharacters(in: .whitespaces) } } +} - // MARK: Private +private extension Filters.Strings { + static let reservedKeywords = [ + "associatedtype", "class", "deinit", "enum", "extension", + "fileprivate", "func", "import", "init", "inout", "internal", + "let", "open", "operator", "private", "protocol", "public", + "static", "struct", "subscript", "typealias", "var", "break", + "case", "continue", "default", "defer", "do", "else", + "fallthrough", "for", "guard", "if", "in", "repeat", "return", + "switch", "where", "while", "as", "Any", "catch", "false", "is", + "nil", "rethrows", "super", "self", "Self", "throw", "throws", + "true", "try", "_", "#available", "#colorLiteral", "#column", + "#else", "#elseif", "#endif", "#file", "#fileLiteral", + "#function", "#if", "#imageLiteral", "#line", "#selector", + "#sourceLocation", "associativity", "convenience", "dynamic", + "didSet", "final", "get", "infix", "indirect", "lazy", "left", + "mutating", "none", "nonmutating", "optional", "override", + "postfix", "precedence", "prefix", "Protocol", "required", + "right", "set", "Type", "unowned", "weak", "willSet" + ] - private static func removeLeadingWhitespaces(from string: String) -> String { + static func removeLeadingWhitespaces(from string: String) -> String { let chars = string.unicodeScalars.drop { CharacterSet.whitespaces.contains($0) } return String(chars) } @@ -362,7 +362,7 @@ extension Filters.Strings { /// /// - Parameter in: the string to possibly escape /// - Returns: if the string is a reserved keyword, the escaped string, otherwise the original one - private static func escapeReservedKeywords(in string: String) -> String { + static func escapeReservedKeywords(in string: String) -> String { guard reservedKeywords.contains(string) else { return string } diff --git a/Sources/StencilSwiftKit/Filters.swift b/Sources/StencilSwiftKit/Filters.swift index 82900227..1f55a5fa 100644 --- a/Sources/StencilSwiftKit/Filters.swift +++ b/Sources/StencilSwiftKit/Filters.swift @@ -7,10 +7,10 @@ import Foundation import Stencil -enum Filters { +public enum Filters { typealias BooleanWithArguments = (Any?, [Any?]) throws -> Bool - enum Error: Swift.Error { + public enum Error: Swift.Error { case invalidInputType case invalidOption(option: String) } @@ -21,7 +21,7 @@ enum Filters { /// - Parameters: /// - value: an input value, may be nil /// - Throws: Filters.Error.invalidInputType - static func parseString(from value: Any?) throws -> String { + public static func parseString(from value: Any?) throws -> String { if let losslessString = value as? LosslessStringConvertible { return String(describing: losslessString) } @@ -44,7 +44,7 @@ enum Filters { /// - arguments: an array of argument values, may be empty /// - index: the index in the arguments array /// - Throws: Filters.Error.invalidInputType - static func parseStringArgument(from arguments: [Any?], at index: Int = 0) throws -> String { + public static func parseStringArgument(from arguments: [Any?], at index: Int = 0) throws -> String { guard index < arguments.count else { throw Error.invalidInputType } @@ -68,7 +68,7 @@ enum Filters { /// If false, returns nil on missing args. /// - Throws: Filters.Error.invalidInputType // swiftlint:disable discouraged_optional_boolean - static func parseBool(from arguments: [Any?], at index: Int = 0, required: Bool = true) throws -> Bool? { + public static func parseBool(from arguments: [Any?], at index: Int = 0, required: Bool = true) throws -> Bool? { guard index < arguments.count, let boolArg = arguments[index] as? String else { if required { throw Error.invalidInputType @@ -95,7 +95,7 @@ enum Filters { /// - index: the index in the arguments array /// - default: The default value should no argument be provided /// - Throws: Filters.Error.invalidInputType - static func parseEnum( + public static func parseEnum( from arguments: [Any?], at index: Int = 0, default: T