|
8 | 8 |
|
9 | 9 | import Foundation |
10 | 10 |
|
11 | | -/** |
12 | | - This protocol describe types that can be tagged with one or |
13 | | - many tags, which can then be used to group, search etc. |
14 | | - |
15 | | - Types that implement this protocol get access to a bunch of |
16 | | - additional functionality, e.g. ``hasTags``, ``addTag(_:)``. |
17 | | - |
18 | | - Note that the ``tags`` property is a raw list of tags, that |
19 | | - should contain slugified tags. However, since this protocol |
20 | | - can be implemented by any type, you can use ``slugifiedTags`` |
21 | | - to ensure that you get a list of slugified tags. |
22 | | - */ |
| 11 | +/// This protocol describe types that can be tagged with one |
| 12 | +/// or many tags, which can be used to group, search etc. |
| 13 | +/// |
| 14 | +/// Types that implement this protocol get access to a bunch |
| 15 | +/// of additional features, e.g. ``hasTags``, ``addTag(_:)``, |
| 16 | +/// ``toggleTag(_:)``, etc. |
| 17 | +/// |
| 18 | +/// Note that the ``tags`` property is a raw string list. If |
| 19 | +/// ypu want a list of slugified tags, use ``slugifiedTags``. |
23 | 20 | public protocol Taggable { |
24 | 21 |
|
25 | | - /** |
26 | | - All tags that have been applied to the item. |
27 | | - */ |
| 22 | + /// All tags that have been applied to the item. |
28 | 23 | var tags: [String] { get set } |
29 | 24 | } |
30 | 25 |
|
31 | 26 | public extension Taggable { |
32 | 27 |
|
33 | | - /** |
34 | | - Whether or not the item has any tags. |
35 | | - */ |
| 28 | + /// Whether or not the item has any tags. |
36 | 29 | var hasTags: Bool { |
37 | 30 | !tags.isEmpty |
38 | 31 | } |
39 | 32 |
|
40 | | - /** |
41 | | - Get a list of slugified ``tags``. |
42 | | - */ |
| 33 | + /// Get a list of slugified ``tags``. |
43 | 34 | var slugifiedTags: [String] { |
44 | 35 | slugifiedTags() |
45 | 36 | } |
46 | 37 |
|
47 | | - /** |
48 | | - Add a tag to the taggable type. |
49 | | - */ |
| 38 | + /// Add a tag to the taggable type. |
50 | 39 | mutating func addTag(_ tag: String) { |
51 | 40 | if hasTag(tag) { return } |
52 | 41 | tags.append(tag) |
53 | 42 | } |
54 | 43 |
|
55 | | - /** |
56 | | - Whether or not the item has a certain tag. |
57 | | - */ |
| 44 | + /// Whether or not the item has a certain tag. |
58 | 45 | func hasTag(_ tag: String) -> Bool { |
59 | 46 | if tag.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { return true } |
60 | 47 | return slugifiedTags.contains(tag.slugified()) |
61 | 48 | } |
62 | 49 |
|
63 | | - /** |
64 | | - Remove a tag from the taggable type. |
65 | | - */ |
| 50 | + /// Remove a tag from the taggable type. |
66 | 51 | mutating func removeTag(_ tag: String) { |
67 | 52 | guard hasTag(tag) else { return } |
68 | 53 | tags = tags.filter { $0 != tag } |
69 | 54 | } |
70 | 55 |
|
71 | | - /** |
72 | | - Get a list of slugified ``tags`` for a certain config. |
73 | | - |
74 | | - - Parameters: |
75 | | - - config: The slug configuration to use, by default ``SlugConfiguration/standard``. |
76 | | - */ |
| 56 | + /// Get a list of slugified ``tags``. |
| 57 | + /// |
| 58 | + /// - Parameters: |
| 59 | + /// - config: The slug configuration to use, by default ``SlugConfiguration/standard``. |
77 | 60 | func slugifiedTags( |
78 | 61 | configuration: SlugConfiguration = .standard |
79 | 62 | ) -> [String] { |
80 | 63 | tags.map { $0.slugified(configuration: configuration) } |
81 | 64 | } |
82 | 65 |
|
83 | | - /** |
84 | | - Toggle a tag on the taggable type. |
85 | | - */ |
| 66 | + /// Toggle a tag on the taggable type. |
86 | 67 | mutating func toggleTag(_ tag: String) { |
87 | 68 | if hasTag(tag) { |
88 | 69 | removeTag(tag) |
|
0 commit comments