-
Notifications
You must be signed in to change notification settings - Fork 731
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bring back all the pluralization (#1879)
* Revert "Remove inflection option, pluralizer and dependency" * Move from InflectorKit fork to origin with 1.0.0 minimum * Update to comply with InflectorKit 1.0.0 deprecations * Enable code generation options to accept additional inflection rules * Update PluralizerTest function names to match #1849 * Shuffle parameter documentation order to match parameter input order
- Loading branch information
1 parent
3e768ed
commit a7d11c8
Showing
9 changed files
with
217 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
Apollo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import Foundation | ||
import InflectorKit | ||
|
||
/// The types of inflection rules that can be used to customize pluralization. | ||
public enum InflectionRule { | ||
|
||
/// A pluralization rule that allows taking a singular word and pluralizing it. | ||
/// - singularRegex: A regular expression representing the single version of the word | ||
/// - replacementRegex: A regular expression representing how to replace the singular version. | ||
case pluralization(singularRegex: String, replacementRegex: String) | ||
|
||
/// A singularization rule that allows taking a plural word and singularizing it. | ||
/// - pluralRegex: A regular expression represeinting the plural version of the word | ||
/// - replacementRegex: A regular expression representing how to replace the singular version | ||
case singularization(pluralRegex: String, replacementRegex: String) | ||
|
||
/// A definition of an irregular pluralization rule not easily captured by regex - for example "person" and "people". | ||
/// - singular: The singular version of the word | ||
/// - plural: The plural version of the word. | ||
case irregular(singular: String, plural: String) | ||
|
||
/// A definition of a word that should never be pluralized or de-pluralized because it's the same no matter what the count - for example, "fish". | ||
/// - word: The word that should never be adjusted. | ||
case uncountable(word: String) | ||
} | ||
|
||
struct Pluralizer { | ||
|
||
private let inflector: StringInflector | ||
|
||
init(rules: [InflectionRule] = []) { | ||
let inflector = StringInflector.default | ||
for rule in rules { | ||
switch rule { | ||
case .pluralization(let pluralRegex, let replacementRegex): | ||
inflector.addPluralRule(pluralRegex, replacement: replacementRegex) | ||
case .singularization(let singularRegex, let replacementRegex): | ||
inflector.addSingularRule(singularRegex, replacement: replacementRegex) | ||
case .irregular(let singular, let plural): | ||
inflector.addIrregular(singular: singular, plural: plural) | ||
case .uncountable(let word): | ||
inflector.addUncountable(word) | ||
} | ||
} | ||
|
||
self.inflector = inflector | ||
} | ||
|
||
func singularize(_ string: String) -> String { | ||
self.inflector.singularize(string) | ||
} | ||
|
||
func pluralize(_ string: String) -> String { | ||
self.inflector.pluralize(string) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.