|
1 | 1 | # InflectorKit
|
2 | 2 | ![CI][ci badge]
|
3 | 3 |
|
4 |
| -**Efficiently Singularize and Pluralize Strings** |
5 |
| - |
6 |
| -InflectorKit ports the string inflection functionality of [Rails ActiveSupport](https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/inflections.rb) to Foundation. |
| 4 | +InflectorKit is a port of the string inflection functionality from |
| 5 | +[Rails ActiveSupport](https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/inflections.rb) for Swift and Objective-C. |
7 | 6 |
|
8 | 7 | > InflectorKit joins [FormatterKit](https://github.com/mattt/FormatterKit) & [TransformerKit](https://github.com/mattt/TransformerKit) in providing well-designed APIs for manipulating user-facing content.
|
9 | 8 |
|
10 | 9 | ## Usage
|
11 | 10 |
|
12 |
| -```objective-c |
13 |
| -#import "NSString+InflectorKit.h" |
14 |
| - |
15 |
| -for (NSString *singular in @[@"person", @"tomato", @"matrix", @"octopus", @"fish"]) { |
16 |
| - NSLog(@"%@: %@", singular, [singular pluralizedString]); |
17 |
| -} |
18 |
| -``` |
| 11 | +### Swift |
19 | 12 |
|
20 | 13 | ```swift
|
21 | 14 | import InflectorKit
|
22 | 15 |
|
23 | 16 | for singular in ["person", "tomato", "matrix", "octopus", "fish"] {
|
24 |
| - print(singular.pluralized) |
| 17 | + print("\(singular) → \(singular.pluralized)") |
25 | 18 | }
|
26 |
| -``` |
| 19 | +/* |
| 20 | +Prints: |
| 21 | +person → people |
| 22 | +tomato → tomatoes |
| 23 | +matrix → matrices |
| 24 | +octopus → octopi |
| 25 | +fish → fish |
| 26 | +*/ |
| 27 | + |
| 28 | +// You can also add pluralization rules, |
| 29 | +// including irregular and uncountable words: |
27 | 30 |
|
28 |
| - person: people |
29 |
| - tomato: tomatoes |
30 |
| - matrix: matrices |
31 |
| - octopus: octopi |
32 |
| - fish: fish |
| 31 | +let inflector = StringInflector.default |
| 32 | +inflector.addPluralRule(#"^i(Pod|Pad)( Mini)?$"#, replacement: #"i$1s$2"#) |
| 33 | +inflector.addIrregular(singular: "lol", plural: "lolz") |
| 34 | +inflector.addUncountable("Herokai") |
| 35 | + |
| 36 | +for singular in ["iPad Mini", "lol", "Herokai"] { |
| 37 | + print("\(singular) → \(singular.pluralized)") |
| 38 | +} |
| 39 | +/* |
| 40 | +Prints: |
| 41 | +iPad Mini → iPads Mini |
| 42 | +lol → lolz |
| 43 | +Herokai → Herokai |
| 44 | +*/ |
| 45 | +``` |
33 | 46 |
|
34 |
| -You can also add pluralization rules, including irregular and uncountable words: |
| 47 | +### Objective-C |
35 | 48 |
|
36 | 49 | ```objective-c
|
37 |
| -#import "TTTStringInflector.h" |
| 50 | +#import "NSString+InflectorKit.h" |
| 51 | + |
| 52 | +for (NSString *singular in @[@"person", @"tomato", @"matrix", @"octopus", @"fish"]) { |
| 53 | + NSLog(@"%@ → %@", singular, [singular pluralizedString]); |
| 54 | +} |
| 55 | + |
| 56 | +/* |
| 57 | +Prints: |
| 58 | +person → people |
| 59 | +tomato → tomatoes |
| 60 | +matrix → matrices |
| 61 | +octopus → octopi |
| 62 | +fish → fish |
| 63 | +*/ |
| 64 | + |
| 65 | +// You can also add pluralization rules, |
| 66 | +// including irregular and uncountable words: |
38 | 67 |
|
39 | 68 | TTTStringInflector *inflector = [TTTStringInflector defaultInflector];
|
40 | 69 | [inflector addPluralRule:@"^i(Pod|Pad)( Mini)?$" withReplacement:@"i$1s$2"];
|
41 | 70 | [inflector addIrregularWithSingular:@"lol" plural:@"lolz"];
|
42 | 71 | [inflector addUncountable:@"Herokai"];
|
43 | 72 |
|
44 | 73 | for (NSString *singular in @[@"iPad Mini", @"lol", @"Herokai"]) {
|
45 |
| - NSLog(@"%@: %@", singular, [singular pluralizedString]); |
| 74 | + NSLog(@"%@ → %@", singular, [singular pluralizedString]); |
46 | 75 | }
|
47 |
| -``` |
48 |
| -
|
49 |
| -```swift |
50 |
| -import InflectorKit |
51 | 76 |
|
52 |
| -let inflector = StringInflector.default |
53 |
| -inflector.addPluralRule(#"^i(Pod|Pad)( Mini)?$"#, replacement: #"i$1s$2"#) |
54 |
| -inflector.addIrregular(singular: "lol", plural: "lolz") |
55 |
| -inflector.addUncountable("Herokai") |
56 |
| -
|
57 |
| -for singular in ["iPad Mini", "lol", "Herokai"] { |
58 |
| - print(singular.pluralized) |
59 |
| -} |
| 77 | +/* |
| 78 | +Prints: |
| 79 | +iPad Mini → iPads Mini |
| 80 | +lol → lolz |
| 81 | +Herokai → Herokai |
| 82 | +*/ |
60 | 83 | ```
|
61 | 84 |
|
62 |
| - iPad Mini: iPads Mini |
63 |
| - lol: lolz |
64 |
| - Herokai: Herokai |
65 |
| - |
66 | 85 | ## License
|
67 | 86 |
|
68 | 87 | MIT
|
|
0 commit comments