feat: Initial commit

This commit is contained in:
2025-02-19 17:01:08 -05:00
commit e0fb6129ad
2362 changed files with 325107 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
import Foundation
extension String {
var numberOfWords: Int {
let characterSet = CharacterSet.whitespacesAndNewlines.union(.punctuationCharacters)
let components = self.components(separatedBy: characterSet)
return components.filter { !$0.isEmpty }.count
}
// This is a sloppy implementation but sadly `NSAttributedString(data:options:documentAttributes:)`
// is not available in CoreFoundation, and as such can't run on Linux (blocking CI builds).
var withoutHtmlTags: String {
return replacingOccurrences(of: "(?m)<pre><span></span><code>[\\s\\S]+?</code></pre>", with: "", options: .regularExpression, range: nil)
.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
.trimmingCharacters(in: .whitespacesAndNewlines)
}
/// See https://jinja2docs.readthedocs.io/en/stable/templates.html#truncate
func truncate(length: Int = 255, killWords: Bool = false, end: String = "...", leeway: Int = 5) -> String {
if count <= length + leeway {
return self
}
if killWords {
return prefix(length - end.count) + end
}
return prefix(length - end.count).split(separator: " ").dropLast().joined(separator: " ") + end
}
}