Skip to content

Commit b3e7a98

Browse files
authored
Refine Swift Support (mattt#5)
* Add CI workflow * Add test target * Convert instance methods to read-only properties * Add testSingularization * Test custom singularization rule * Rename TTTStringInflector to StringInflector in Swift * Convert default to class property * Finalize TTTStringInflector * Add NS_ASSUME_NONNULL_BEGIN / END annotations * Rename inflector instance methods in Swift * Add documentation comments * Remove linux job from CI workflow * Update copyright * Move source files to Sources/InflectorKit * Update examples in README * Add CI badge to README * Add release workflow
1 parent e28108c commit b3e7a98

File tree

13 files changed

+297
-110
lines changed

13 files changed

+297
-110
lines changed

.github/workflows/ci.yml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
9+
jobs:
10+
macos_big_sur:
11+
runs-on: macos-11.0
12+
13+
strategy:
14+
matrix:
15+
xcode:
16+
- "11.7" # Swift 5.2
17+
- "12.2" # Swift 5.3
18+
19+
name: "macOS Big Sur (Xcode ${{ matrix.xcode }})"
20+
21+
steps:
22+
- name: Checkout
23+
uses: actions/checkout@v1
24+
- uses: actions/cache@v2
25+
with:
26+
path: .build
27+
key: ${{ runner.os }}-spm-xcode-${{ matrix.xcode }}-${{ hashFiles('**/Package.resolved') }}
28+
restore-keys: |
29+
${{ runner.os }}-spm-xcode-${{ matrix.xcode }}-
30+
- name: Build and Test
31+
run: |
32+
swift test
33+
env:
34+
DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app/Contents/Developer
35+
36+
macos_catalina:
37+
runs-on: macos-10.15
38+
39+
strategy:
40+
matrix:
41+
xcode:
42+
- "11.7" # Swift 5.2
43+
- "12" # Swift 5.3
44+
45+
name: "macOS Catalina (Xcode ${{ matrix.xcode }})"
46+
47+
steps:
48+
- name: Checkout
49+
uses: actions/checkout@v1
50+
- uses: actions/cache@v2
51+
with:
52+
path: .build
53+
key: ${{ runner.os }}-spm-xcode-${{ matrix.xcode }}-${{ hashFiles('**/Package.resolved') }}
54+
restore-keys: |
55+
${{ runner.os }}-spm-xcode-${{ matrix.xcode }}-
56+
- name: Build and Test
57+
run: |
58+
swift test
59+
env:
60+
DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app/Contents/Developer

.github/workflows/release.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- "*.*.*"
7+
8+
jobs:
9+
release:
10+
name: Push release to CocoaPods Trunk
11+
12+
runs-on: macos-latest
13+
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v2
17+
18+
- name: Install Cocoapods
19+
run: gem install cocoapods
20+
21+
- name: Deploy to Cocoapods
22+
run: |
23+
set -eo pipefail
24+
pod lib lint --allow-warnings
25+
pod trunk push --allow-warnings
26+
env:
27+
COCOAPODS_TRUNK_TOKEN: ${{ secrets.COCOAPODS_TRUNK_TOKEN }}

InflectorKit.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Pod::Spec.new do |s|
55
s.summary = 'Efficiently Singularize and Pluralize Strings.'
66
s.homepage = 'https://github.com/mattt/InflectorKit'
77
s.social_media_url = 'https://twitter.com/mattt'
8-
s.authors = { 'Mattt Thompson' => 'm@mattt.me' }
8+
s.authors = { 'Mattt' => 'mattt@me.com' }
99
s.source = { :git => 'https://github.com/mattt/InflectorKit.git', :tag => '0.0.1' }
10-
s.source_files = 'InflectorKit'
10+
s.source_files = 'Sources/InflectorKit'
1111
s.requires_arc = true
1212
end

InflectorKit/include/TTTStringInflector.h

Lines changed: 0 additions & 76 deletions
This file was deleted.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2013 Mattt Thompson (http://mattt.me/)
1+
Copyright (c) 2013 - 2020 Mattt (https://mat.tt)
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal

Package.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ let package = Package(
1414
dependencies: [],
1515
targets: [
1616
.target(
17-
name: "InflectorKit",
18-
dependencies: [],
19-
path: "InflectorKit"
17+
name: "InflectorKit"
18+
),
19+
.testTarget(
20+
name: "InflectorKitTests",
21+
dependencies: [
22+
.target(name: "InflectorKit")
23+
]
2024
)
2125
]
2226
)

README.md

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
InflectorKit
2-
============
1+
# InflectorKit
2+
![CI][ci badge]
33

44
**Efficiently Singularize and Pluralize Strings**
55

@@ -15,6 +15,14 @@ InflectorKit ports the string inflection functionality of [Rails ActiveSupport](
1515
for (NSString *singular in @[@"person", @"tomato", @"matrix", @"octopus", @"fish"]) {
1616
NSLog(@"%@: %@", singular, [singular pluralizedString]);
1717
}
18+
```
19+
20+
```swift
21+
import InflectorKit
22+
23+
for singular in ["person", "tomato", "matrix", "octopus", "fish"] {
24+
print(singular.pluralized)
25+
}
1826
```
1927

2028
person: people
@@ -36,20 +44,31 @@ TTTStringInflector *inflector = [TTTStringInflector defaultInflector];
3644
for (NSString *singular in @[@"iPad Mini", @"lol", @"Herokai"]) {
3745
NSLog(@"%@: %@", singular, [singular pluralizedString]);
3846
}
47+
```
48+
49+
```swift
50+
import InflectorKit
51+
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+
}
3960
```
4061

4162
iPad Mini: iPads Mini
4263
lol: lolz
4364
Herokai: Herokai
4465

45-
## Contact
66+
## License
4667

47-
Mattt Thompson
68+
MIT
4869

49-
- http://github.com/mattt
50-
- http://twitter.com/mattt
51-
70+
## Contact
5271

53-
## License
72+
Mattt ([@mattt](https://twitter.com/mattt))
5473

55-
InflectorKit is available under the MIT license. See the LICENSE file for more info.
74+
[ci badge]: https://github.com/mattt/InflectorKit/workflows/CI/badge.svg

InflectorKit/NSString+InflectorKit.m renamed to Sources/InflectorKit/NSString+InflectorKit.m

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// NSString+InflectorKit.m
22
//
3-
// Copyright (c) 2013 Mattt Thompson (http://mattt.me)
3+
// Copyright (c) 2013 - 2020 Mattt (https://mat.tt)
44
//
55
// Permission is hereby granted, free of charge, to any person obtaining a copy
66
// of this software and associated documentation files (the "Software"), to deal
@@ -23,14 +23,18 @@
2323
#import "NSString+InflectorKit.h"
2424
#import "TTTStringInflector.h"
2525

26+
NS_ASSUME_NONNULL_BEGIN
27+
2628
@implementation NSString (InflectorKit)
2729

28-
- (nonnull NSString *)singularizedString {
30+
- (NSString *)singularizedString {
2931
return [[TTTStringInflector defaultInflector] singularize:self];
3032
}
3133

32-
- (nonnull NSString *)pluralizedString {
34+
- (NSString *)pluralizedString {
3335
return [[TTTStringInflector defaultInflector] pluralize:self];
3436
}
3537

3638
@end
39+
40+
NS_ASSUME_NONNULL_END

InflectorKit/TTTStringInflector.m renamed to Sources/InflectorKit/TTTStringInflector.m

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// TTTStringInflector.m
22
//
3-
// Copyright (c) 2013 Mattt Thompson (http://mattt.me)
3+
// Copyright (c) 2013 - 2020 Mattt (https://mat.tt)
44
//
55
// Permission is hereby granted, free of charge, to any person obtaining a copy
66
// of this software and associated documentation files (the "Software"), to deal
@@ -22,6 +22,8 @@
2222

2323
#import "TTTStringInflector.h"
2424

25+
NS_ASSUME_NONNULL_BEGIN
26+
2527
@interface TTTStringInflectionRule : NSObject
2628

2729
+ (instancetype)ruleWithPattern:(NSString *)pattern
@@ -63,7 +65,7 @@ - (id)init {
6365
return self;
6466
}
6567

66-
+ (nonnull instancetype)defaultInflector {
68+
+ (instancetype)defaultInflector {
6769
static id _defaultInflector = nil;
6870
static dispatch_once_t onceToken;
6971
dispatch_once(&onceToken, ^{
@@ -74,7 +76,7 @@ + (nonnull instancetype)defaultInflector {
7476
return _defaultInflector;
7577
}
7678

77-
- (nonnull NSString *)singularize:(nonnull NSString *)string {
79+
- (NSString *)singularize:(NSString *)string {
7880
if ([self.mutableUncountables containsObject:string]) {
7981
return string;
8082
}
@@ -92,7 +94,7 @@ - (nonnull NSString *)singularize:(nonnull NSString *)string {
9294
return mutableString;
9395
}
9496

95-
- (nonnull NSString *)pluralize:(nonnull NSString *)string {
97+
- (NSString *)pluralize:(NSString *)string {
9698
if ([self.mutableUncountables containsObject:string]) {
9799
return string;
98100
}
@@ -110,31 +112,31 @@ - (nonnull NSString *)pluralize:(nonnull NSString *)string {
110112
return mutableString;
111113
}
112114

113-
- (void)addSingularRule:(nonnull NSString *)rule
114-
withReplacement:(nonnull NSString *)replacement
115+
- (void)addSingularRule:(NSString *)rule
116+
withReplacement:(NSString *)replacement
115117
{
116118
[self.mutableUncountables removeObject:rule];
117119

118120
[self.mutableSingularRules insertObject:[TTTStringInflectionRule ruleWithPattern:rule options:NSRegularExpressionAnchorsMatchLines | NSRegularExpressionCaseInsensitive | NSRegularExpressionUseUnicodeWordBoundaries replacement:replacement] atIndex:0];
119121
}
120122

121-
- (void)addPluralRule:(nonnull NSString *)rule
122-
withReplacement:(nonnull NSString *)replacement
123+
- (void)addPluralRule:(NSString *)rule
124+
withReplacement:(NSString *)replacement
123125
{
124126
[self.mutableUncountables removeObject:rule];
125127
[self.mutableUncountables removeObject:replacement];
126128

127129
[self.mutablePluralRules insertObject:[TTTStringInflectionRule ruleWithPattern:rule options:NSRegularExpressionAnchorsMatchLines | NSRegularExpressionCaseInsensitive | NSRegularExpressionUseUnicodeWordBoundaries replacement:replacement] atIndex:0];
128130
}
129131

130-
- (void)addIrregularWithSingular:(nonnull NSString *)singular
131-
plural:(nonnull NSString *)plural
132+
- (void)addIrregularWithSingular:(NSString *)singular
133+
plural:(NSString *)plural
132134
{
133135
[self.mutableIrregularPluralsBySingular setObject:plural forKey:singular];
134136
[self.mutableIrregularPluralsBySingular setObject:[plural capitalizedString] forKey:[singular capitalizedString]];
135137
}
136138

137-
- (void)addUncountable:(nonnull NSString *)word {
139+
- (void)addUncountable:(NSString *)word {
138140
[self.mutableUncountables addObject:word];
139141
}
140142

@@ -268,3 +270,5 @@ - (void)addPluralizationRulesForEnUSLocale {
268270
}
269271

270272
@end
273+
274+
NS_ASSUME_NONNULL_END
File renamed without changes.

0 commit comments

Comments
 (0)