https://github.com/mattt/inflectorkit
Efficiently Singularize and Pluralize Strings
https://github.com/mattt/inflectorkit
Last synced: 9 months ago
JSON representation
Efficiently Singularize and Pluralize Strings
- Host: GitHub
- URL: https://github.com/mattt/inflectorkit
- Owner: mattt
- License: mit
- Created: 2013-03-14T18:37:53.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2020-12-04T12:33:50.000Z (about 5 years ago)
- Last Synced: 2024-10-30T08:14:41.000Z (about 1 year ago)
- Language: Objective-C
- Size: 33.2 KB
- Stars: 481
- Watchers: 11
- Forks: 33
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# InflectorKit
![CI][ci badge]
InflectorKit is a port of the string inflection functionality from
[Rails ActiveSupport](https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/inflections.rb) for Swift and Objective-C.
> InflectorKit joins [FormatterKit](https://github.com/mattt/FormatterKit) & [TransformerKit](https://github.com/mattt/TransformerKit) in providing well-designed APIs for manipulating user-facing content.
## Usage
### Swift
```swift
import InflectorKit
for singular in ["person", "tomato", "matrix", "octopus", "fish"] {
print("\(singular) → \(singular.pluralized)")
}
/*
Prints:
person → people
tomato → tomatoes
matrix → matrices
octopus → octopi
fish → fish
*/
// You can also add pluralization rules,
// including irregular and uncountable words:
let inflector = StringInflector.default
inflector.addPluralRule(#"^i(Pod|Pad)( Mini)?$"#, replacement: #"i$1s$2"#)
inflector.addIrregular(singular: "lol", plural: "lolz")
inflector.addUncountable("Herokai")
for singular in ["iPad Mini", "lol", "Herokai"] {
print("\(singular) → \(singular.pluralized)")
}
/*
Prints:
iPad Mini → iPads Mini
lol → lolz
Herokai → Herokai
*/
```
### Objective-C
```objective-c
#import "NSString+InflectorKit.h"
for (NSString *singular in @[@"person", @"tomato", @"matrix", @"octopus", @"fish"]) {
NSLog(@"%@ → %@", singular, [singular pluralizedString]);
}
/*
Prints:
person → people
tomato → tomatoes
matrix → matrices
octopus → octopi
fish → fish
*/
// You can also add pluralization rules,
// including irregular and uncountable words:
TTTStringInflector *inflector = [TTTStringInflector defaultInflector];
[inflector addPluralRule:@"^i(Pod|Pad)( Mini)?$" withReplacement:@"i$1s$2"];
[inflector addIrregularWithSingular:@"lol" plural:@"lolz"];
[inflector addUncountable:@"Herokai"];
for (NSString *singular in @[@"iPad Mini", @"lol", @"Herokai"]) {
NSLog(@"%@ → %@", singular, [singular pluralizedString]);
}
/*
Prints:
iPad Mini → iPads Mini
lol → lolz
Herokai → Herokai
*/
```
## Installation
### Swift Package Manager
Add the InflectorKit package to your target dependencies in `Package.swift`:
```swift
import PackageDescription
let package = Package(
name: "YourProject",
dependencies: [
.package(
url: "https://github.com/mattt/InflectorKit",
from: "1.0.0"
),
]
)
```
Then run the `swift build` command to build your project.
### CocoaPods
You can install `InflectorKit` via CocoaPods,
by adding the following line to your `Podfile`:
```ruby
pod 'InflectorKit', '~> 1.0.0'
```
Run the `pod install` command to download the library
and integrate it into your Xcode project.
## License
MIT
## Contact
Mattt ([@mattt](https://twitter.com/mattt))
[ci badge]: https://github.com/mattt/InflectorKit/workflows/CI/badge.svg