Skip to content

Commit

Permalink
Merge pull request #136 from SwiftGen/feature/public-filters
Browse files Browse the repository at this point in the history
Internal: make filters public
  • Loading branch information
djbe committed Feb 7, 2021
2 parents dc1d1a4 + 21d6716 commit 910a9e7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 43 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions Sources/StencilSwiftKit/Filters+Numbers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
66 changes: 33 additions & 33 deletions Sources/StencilSwiftKit/Filters+Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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? {
Expand Down Expand Up @@ -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
Expand All @@ -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())
}
Expand All @@ -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)

Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/StencilSwiftKit/Filters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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<T>(
public static func parseEnum<T>(
from arguments: [Any?],
at index: Int = 0,
default: T
Expand Down

0 comments on commit 910a9e7

Please sign in to comment.