{"id":25764666,"url":"https://github.com/ipedro/swift-choosable","last_synced_at":"2025-08-10T22:16:12.603Z","repository":{"id":205435625,"uuid":"714232915","full_name":"ipedro/swift-choosable","owner":"ipedro","description":"Choosable","archived":false,"fork":false,"pushed_at":"2023-11-11T04:48:19.000Z","size":29,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-18T03:02:40.698Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://ipedro.github.io/swift-choosable/","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ipedro.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-04T10:00:04.000Z","updated_at":"2023-11-04T13:38:28.000Z","dependencies_parsed_at":"2025-02-26T21:29:57.612Z","dependency_job_id":null,"html_url":"https://github.com/ipedro/swift-choosable","commit_stats":null,"previous_names":["ipedro/swift-choosable"],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/ipedro/swift-choosable","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ipedro%2Fswift-choosable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ipedro%2Fswift-choosable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ipedro%2Fswift-choosable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ipedro%2Fswift-choosable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ipedro","download_url":"https://codeload.github.com/ipedro/swift-choosable/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ipedro%2Fswift-choosable/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269795070,"owners_count":24476939,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-10T02:00:08.965Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-26T21:19:46.879Z","updated_at":"2025-08-10T22:16:12.576Z","avatar_url":"https://github.com/ipedro.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Choosable\n[![License](https://img.shields.io/github/license/ipedro/swift-choosable)](https://github.com/ipedro/swift-choosable/blob/main/LICENSE) \n![Latest Version](https://img.shields.io/github/v/tag/ipedro/swift-choosable?label=Swift%20Package\u0026sort=semver)\n![Swift 5.7+](https://img.shields.io/badge/Swift-5.7+-orange.svg)\n\n![iOS 10.0+](https://img.shields.io/badge/iOS-10.0+-lightblue.svg)\n![macOS 10.14+](https://img.shields.io/badge/macOS-10.14+-lightblue.svg)\n![tvOS 10.0+](https://img.shields.io/badge/tvOS-10.0+-lightblue.svg)\n![watchOS 3.0+](https://img.shields.io/badge/watchOS-3.0+-lightblue.svg)\n\n`Choosable` is a Swift protocol designed to empower objects with context-aware capabilities, allowing them to respond dynamically to conditions. By adopting `Choosable`, any Swift type can elegantly choose between itself and an alternative based on the truth value of given conditions.\n\nThis flexibility opens the door to cleaner, more readable conditional logic throughout your Swift projects—from feature flagging to adaptive UI elements, `Choosable` can streamline the way you handle binary decisions.\n\n## Features\n\n- 🔄 **Toggle Behavior**: Seamlessly switch between values or states.\n- 🔍 **Context-Aware**: Make decisions based on runtime conditions.\n- ✅ **Type-agnostic**: Works with any type, including custom types.\n- 📦 **No External Dependencies**: Easy to integrate into any project.\n\n## Installation\n\n### Swift Package Manager\n\nYou can add `Choosable` to your project via Swift Package Manager, by adding the following to your `Package.swift` file in the `dependencies` array:\n\n```swift\n.package(url: \"https://github.com/ipedro/swift-choosable.git\", .upToNextMajor(from: \"1.0.0\"))\n```\n\n## Usage\n\nIntegrating `Choosable` into your types is straightforward:\n\n```swift\nextension MyType: Choosable {}\n```\n\nOnce conformed, the `or(_:when:)` method is available to elegantly handle conditional logic:\n\n```swift\nlet item = myDefaultItem.or(anotherItem, when: someCondition)\n```\n\n### Examples\n\n#### Primitives\n\n```swift\nlet discount = 0.0.or(15.0, when: isHolidaySeason) // Returns 15.0 if `isHolidaySeason` is true\n```\n\n#### Optionals\n\n```swift\nlet user = currentUser.or(defaultUser, when: currentUser == nil) // Returns `defaultUser` if `currentUser` is nil\n```\n\n#### Collections\n\n```swift\nlet data = cachedData.or(freshData, when: isCacheExpired) // Returns `freshData` if `isCacheExpired` is true\n```\n\n#### Custom Types\n\n```swift\nstruct FeatureFlag: Choosable {\n    let isEnabled: Bool\n}\n\nlet featureA = FeatureFlag(isEnabled: true)\nlet featureB = FeatureFlag(isEnabled: false)\nlet featureC = FeatureFlag(isEnabled: false)\n\n// Given a condition, choose `featureA`, `featureB` or `featureC`.\n// In this case, `isEnabled` flag is true, so `featureA` will be chosen.\nlet activeFeature = featureA\n    .or(featureB, when: featureA.isEnabled)\n    .or(featureC, when: featureB.isEnabled)\n```\n\n#### Adaptive UI Elements\n\n```swift\nimport SwiftUI\n\nstruct AdaptiveTextColor: View {\n    @State \n    private var isSelected: Bool = false\n    \n    @Environment(\\.isEnabled)\n    private var isEnabled\n\n    var body: some View {\n        Text(\"Hello, Choosable!\")\n            .foregroundColor(Color.black\n                .or(Color.white, when: isSelected)\n                .or(Color.gray, when: !isEnabled)\n            )\n            .background { Color.red }\n    }\n}\n\nstruct AdaptiveFontStyle: View {\n    @State \n    private var isHorizontal: Bool = false\n\n    var body: some View {\n        Text(\"Dynamic Font Size\")\n            .font(.body.or(.largeTitle, when: isHorizontal))\n        }\n    }\n```\n\n## License\n\n`Choosable` is released under the MIT license. See [LICENSE](LICENSE) for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fipedro%2Fswift-choosable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fipedro%2Fswift-choosable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fipedro%2Fswift-choosable/lists"}