CoreKit is a lightweight Swift package that bundles a collection of general-purpose utilities, foundation extensions, and small helper types used across projects. It focuses on the everyday building blocks — string and collection helpers, date formatting, cryptography, error handling, and async testing tools — so you don't have to rewrite them in every app.
- Swift 6.3+
- A platform that provides
Foundation,CryptoKit, andCommonCrypto(iOS, macOS, tvOS, watchOS, visionOS)
Add CoreKit to your Package.swift:
dependencies:[.package(url:"https://github.com/thomasalbert1993/CoreKit.git", from:"1.0.0")]Then add it as a dependency of your target:
.target(
name:"MyApp",
dependencies:["CoreKit"])CoreKit depends on BigInt, which is resolved automatically.
To use it in code:
import CoreKitAES encryption — AES-CBC with PKCS7 padding, supporting 128-, 192-, and 256-bit keys.
letkey=Data(/* 16, 24, or 32 bytes */)letiv=AESEncryption.generateRandomIV()!
letciphertext=AESEncryption.encrypt(data: plaintext, key: key, iv: iv)letplaintext=AESEncryption.decrypt(data: ciphertext!, key: key, iv: iv)SHA-256 — hashing for Data and String, returned as a hex string.
"hello".sha256() // "2cf24dba..."
someData.sha256()Base62 — compact, URL-safe encoding for Data, String, and UUID.
UUID().base62Encoded()"hello".base62Encoded()
someData.base62EncodedString()Hex — hexadecimal representation of Data.
someData.hexStringDate.utcString(timeStyle:)— ISO 8601 UTC serialization (none,second, ormicrosecondprecision).String.toDate()— parse ISO 8601 strings back intoDate(second and microsecond precision).MicroSecondISO8601DateFormatter— a formatter that handles microsecond-precise fractional seconds, which the standardISO8601DateFormatterdoes not.Date.components(includingTime:in:),Date.slightlyBefore, andDate.slightlyAfterhelpers.
letstring=Date().utcString(timeStyle:.microsecond)letdate=try string.toDate()SemanticVersion parses, compares, and prints major.minor.patch versions.
leta=SemanticVersion("1.2.0")!
letb=SemanticVersion(major:1, minor:3, patch:0)
a < b // true
b.literal // "1.3.0"- The
?!operator throws a given error when the left-hand value isnil. Stringconforms toError/LocalizedErrorfor quick debug errors.DebuggableErrorprovides consistent, readable error descriptions (including case name and associated values) in both Debug and Release builds.
letinstance=try repository.fetch(id: id)?!MyError.notFound(id: id)enumAPIError:DebuggableError{case internalError(code:Int)}
// print → "APIError.internalError(502)"Guard asynchronous test code against hangs with a bounded timeout.
tryawaitwithTimeout(2.0){await sut.performWork()}letvalue=tryawaitawaitCompletion{ completion in
legacyAPI.load{completion($0)}}A set of convenience extensions on the standard library and Foundation types:
| Type | Highlights |
|---|---|
Array | chunked(into:), distinctValues(), randomElements(count:preserveOrder:), remove(_:), removeFirst(while:), IndexSet subscript |
Collection | emptyAsNil, plus Identifiable helpers: ids, first(id:), firstIndex(id:), contains(id:) |
Dictionary | mapKeys(_:), compactMapKeys(_:), contains(key:) |
Sequence | grouped(by:), sum() |
Set | toggle(_:), difference(from:), remove(id:) |
String | emptyAsNil, trimmed(), removingAccents(), toURL(), uniqueID(withPrefix:), randomUppercasedCharactersAndDigits(length:) |
Double | rounded(toDecimals:), hasFractionalPart |
ClosedRange | intersects(with:), nullable bounds via the RangeBound protocol |
WeakRef<T>— store weak references inside collections.DebugDescription— build readable, consistentdescriptionstrings for your types (automatically includesidforIdentifiableobjects).
finalclassSession{}letref=WeakRef(session) // ref.value becomes nil when session is deallocatedThe package ships with a full test suite built on the Swift Testing framework. Run it with:
swift testCoreKit is available under the MIT license. See the LICENSE file for details.