mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
Enhance Inflections setup APIs
This commit is contained in:
1 parent
c04f3c7faa
commit
e201dd21ac
4 files changed
+33
-34
No files matched your search
+2
-3
@@ -352,9 +352,8 @@ It comes with new features, but also a few breaking changes. The [GRDB 4 Migrati
|
||||
+ init()
|
||||
+ mutating func plural(_ pattern: String, options: NSRegularExpression.Options = [.caseInsensitive], _ template: String)
|
||||
+ mutating func singular(_ pattern: String, options: NSRegularExpression.Options = [.caseInsensitive], _ template: String)
|
||||
+ mutating func uncountable(_ words: [String])
|
||||
+ mutating func uncountable(_ word: String)
|
||||
+ mutating func irregular(_ singular: String, _ plural: String)
|
||||
+ mutating func uncountableWords(_ words: [String])
|
||||
+ mutating func irregularSuffix(_ singular: String, _ plural: String)
|
||||
+ func pluralize(_ string: String) -> String
|
||||
+ func singularize(_ string: String) -> String
|
||||
+}
|
||||
|
||||
@@ -106,7 +106,7 @@ extension Inflections {
|
||||
inflections.singular("(database)s$", "$1")
|
||||
inflections.singular("(canvas)(es)?$", "$1")
|
||||
|
||||
inflections.uncountable([
|
||||
inflections.uncountableWords([
|
||||
"advice",
|
||||
"corps",
|
||||
"dice",
|
||||
@@ -123,15 +123,15 @@ extension Inflections {
|
||||
"species",
|
||||
])
|
||||
|
||||
inflections.irregular("child", "children")
|
||||
inflections.irregular("foot", "feet")
|
||||
inflections.irregular("leaf", "leaves")
|
||||
inflections.irregular("man", "men")
|
||||
inflections.irregular("move", "moves")
|
||||
inflections.irregular("person", "people")
|
||||
inflections.irregular("sex", "sexes")
|
||||
inflections.irregular("specimen", "specimens")
|
||||
inflections.irregular("zombie", "zombies")
|
||||
inflections.irregularSuffix("child", "children")
|
||||
inflections.irregularSuffix("foot", "feet")
|
||||
inflections.irregularSuffix("leaf", "leaves")
|
||||
inflections.irregularSuffix("man", "men")
|
||||
inflections.irregularSuffix("move", "moves")
|
||||
inflections.irregularSuffix("person", "people")
|
||||
inflections.irregularSuffix("sex", "sexes")
|
||||
inflections.irregularSuffix("specimen", "specimens")
|
||||
inflections.irregularSuffix("zombie", "zombies")
|
||||
|
||||
return inflections
|
||||
}()
|
||||
|
||||
@@ -84,39 +84,27 @@ public struct Inflections {
|
||||
///
|
||||
/// var inflections = Inflections()
|
||||
/// inflections.plural("$", "s")
|
||||
/// inflections.uncountable(["foo"])
|
||||
/// inflections.uncountableWords(["foo"])
|
||||
/// inflections.pluralize("foo") // "foo"
|
||||
/// inflections.pluralize("bar") // "bars"
|
||||
public mutating func uncountable(_ words: [String]) {
|
||||
public mutating func uncountableWords(_ words: [String]) {
|
||||
for word in words {
|
||||
uncountable(word)
|
||||
uncountableWord(word)
|
||||
}
|
||||
}
|
||||
|
||||
/// Appends an uncountable word.
|
||||
///
|
||||
/// var inflections = Inflections()
|
||||
/// inflections.plural("$", "s")
|
||||
/// inflections.uncountable("foo")
|
||||
/// inflections.pluralize("foo") // "foo"
|
||||
/// inflections.pluralize("bar") // "bars"
|
||||
public mutating func uncountable(_ word: String) {
|
||||
let escWord = NSRegularExpression.escapedPattern(for: word)
|
||||
uncountablesRegularExpressions[word] = try! NSRegularExpression(pattern: "\\b\(escWord)\\Z", options: [.caseInsensitive])
|
||||
}
|
||||
|
||||
/// Appends an irregular singular/plural pair.
|
||||
///
|
||||
/// var inflections = Inflections()
|
||||
/// inflections.plural("$", "s")
|
||||
/// inflections.irregular("man", "men")
|
||||
/// inflections.irregularSuffix("man", "men")
|
||||
/// inflections.pluralize("man") // "men"
|
||||
/// inflections.singularizes("women") // "woman"
|
||||
///
|
||||
/// - parameters:
|
||||
/// - singular: The singular form.
|
||||
/// - plural: The plural form.
|
||||
public mutating func irregular(_ singular: String, _ plural: String) {
|
||||
public mutating func irregularSuffix(_ singular: String, _ plural: String) {
|
||||
let s0 = singular.first!
|
||||
let srest = singular.dropFirst()
|
||||
|
||||
@@ -158,6 +146,18 @@ public struct Inflections {
|
||||
|
||||
// MARK: - Utils
|
||||
|
||||
/// Appends an uncountable word.
|
||||
///
|
||||
/// var inflections = Inflections()
|
||||
/// inflections.plural("$", "s")
|
||||
/// inflections.uncountableWord("foo")
|
||||
/// inflections.pluralize("foo") // "foo"
|
||||
/// inflections.pluralize("bar") // "bars"
|
||||
private mutating func uncountableWord(_ word: String) {
|
||||
let escWord = NSRegularExpression.escapedPattern(for: word)
|
||||
uncountablesRegularExpressions[word] = try! NSRegularExpression(pattern: "\\b\(escWord)\\Z", options: [.caseInsensitive])
|
||||
}
|
||||
|
||||
private func isUncountable(_ string: String) -> Bool {
|
||||
let range = NSRange(location: 0, length: string.utf16.count)
|
||||
for (_, reg) in uncountablesRegularExpressions {
|
||||
|
||||
@@ -109,7 +109,7 @@ class InflectionsTests: GRDBTestCase {
|
||||
|
||||
func testUncountabilityOfASCIIWord() {
|
||||
let word = "HTTP"
|
||||
Inflections.default.uncountable(word)
|
||||
Inflections.default.uncountableWords([word])
|
||||
XCTAssertEqual(word.pluralized, word)
|
||||
XCTAssertEqual(word.singularized, word)
|
||||
XCTAssertEqual(word.pluralized, word.singularized)
|
||||
@@ -117,7 +117,7 @@ class InflectionsTests: GRDBTestCase {
|
||||
|
||||
func testUncountabilityOfNonASCIIWord() {
|
||||
let word = "猫"
|
||||
Inflections.default.uncountable(word)
|
||||
Inflections.default.uncountableWords([word])
|
||||
XCTAssertEqual(word.pluralized, word)
|
||||
XCTAssertEqual(word.singularized, word)
|
||||
XCTAssertEqual(word.pluralized, word.singularized)
|
||||
@@ -151,7 +151,7 @@ class InflectionsTests: GRDBTestCase {
|
||||
let uncountableWord = "ors"
|
||||
let countableWord = "sponsor"
|
||||
|
||||
Inflections.default.uncountable(uncountableWord)
|
||||
Inflections.default.uncountableWords([uncountableWord])
|
||||
|
||||
XCTAssertEqual(uncountableWord.singularized, uncountableWord)
|
||||
XCTAssertEqual(uncountableWord.pluralized, uncountableWord)
|
||||
@@ -234,7 +234,7 @@ class InflectionsTests: GRDBTestCase {
|
||||
#if !SWIFT_PACKAGE
|
||||
func testIrregularityBetweenSingularAndPlural() {
|
||||
for (singular, plural) in inflectionTestCases.testCases["Irregularities"]! {
|
||||
Inflections.default.irregular(singular, plural)
|
||||
Inflections.default.irregularSuffix(singular, plural)
|
||||
XCTAssertEqual(plural.singularized, singular)
|
||||
XCTAssertEqual(singular.pluralized, plural)
|
||||
XCTAssertEqual(singular.singularized, singular)
|
||||
|
||||
Reference in new issue
Block a user