InflectorKit
InflectorKit 是从 Rails ActiveSupport 迁移的字符串屈折功能到 Swift 和 Objective-C。
InflectorKit 与 FormatterKit 和 TransformerKit 并行,为操作面向用户的内容提供精心设计的 API。
使用方法
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
#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
*/
安装
Swift Package Manager
在 Package.swift
文件中将 InflectorKit 包添加到目标依赖中
import PackageDescription
let package = Package(
name: "YourProject",
dependencies: [
.package(
url: "https://github.com/mattt/InflectorKit",
from: "1.0.0"
),
]
)
然后运行 swift build
命令来构建你的项目。
CocoaPods
您可以通过在 Podfile
中添加以下行来使用 CocoaPods 安装 InflectorKit
pod 'InflectorKit', '~> 1.0.0'
运行 pod install
命令以下载库并将其集成到您的 Xcode 项目中。
许可协议
MIT
联系信息
Mattt (@mattt)