TagKit makes it easy to use tags and slugified strings in Swift and SwiftUI.
You can make any type implement Taggable and Slugifiable to manage tags and slugs. You can use TagList to list tags and TagToggleList to list tags that can be toggled on and off.
TagKit can be installed with the Swift Package Manager:
https://github.com/danielsaidi/TagKit.git
The two main areas of TagKit is to make it easy to manage tags and to slugify strings.
Tagging items make it possible to categorize, group, filter and search among items, based on their tags. TagKit has a Tagged protocol for types with immutable tags and a Taggable protocol for types with mutable tags.
struct MyModel: Tagged {
var tags: [String] {
return ["Tag 1", "Tag 2", "Tag 3"]
}
}
let value = MyModel()
value.tags() // ["Tag 1", "Tag 2", "Tag 3"]
value.slugifiedTags() // ["tag-1", "tag-2", "tag-3"]
value.hasTag("Tag 1") // true
value.hasTag("Tag 4") // false
All Tagged types are extended with properties and functions like hasTags, hasTag(_:), slugifiedTags, etc. Taggable types are extended with mutable functions like addTag(_:), removeTag(_:), toggleTag(_:), etc.
TagKit has tag-related views like TagList, TagToggleList and TagTextField. You can apply a tagFlow(_:) modifier to control the flow of tags in a list, and tagCapsule(style:) to convert a view into a tag capsule.
Slugifying means to remove unwanted characters and replacing whitespaces to get a unique identifier that can be used in e.g. URLs. TagKit has a Slugifiable protocol that makes it easy to slugify type values.
let custom = SlugConfiguration(
separator: "+",
allowedCharacters: .init(charactersIn: "hewo")
)
"Hello, world!".slugified() // "hello-world"
"Hello, world!".slugified(with: custom) // "he+wo"
A Slugifiable type must provide a slugValue after which you can use slugified(with:) to create a slugified representation of the type. You can use a custom SlugConfiguration or the standard one.
The online documentation has more information, articles, code examples, etc.
You can become a sponsor to help me dedicate more time on my various open-source tools. Every contribution, no matter the size, makes a real difference in keeping these tools free and actively developed.
Feel free to reach out if you have questions or if you want to contribute in any way:
- Website: danielsaidi.com
- E-mail: daniel.saidi@gmail.com
- Bluesky: @danielsaidi@bsky.social
- Mastodon: @danielsaidi@mastodon.social
TagKit is available under the MIT license. See the LICENSE file for more info.
