{"id":32143080,"url":"https://github.com/softwareengineerchris/consolidate","last_synced_at":"2026-02-19T08:01:40.591Z","repository":{"id":42443320,"uuid":"211467780","full_name":"SoftwareEngineerChris/Consolidate","owner":"SoftwareEngineerChris","description":"🎳  Consolidates an array of Elements by KeyPath, Closure, or Conformance - Swift Micro Package","archived":false,"fork":false,"pushed_at":"2023-03-16T14:01:25.000Z","size":216,"stargazers_count":1,"open_issues_count":3,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-01-19T19:24:33.482Z","etag":null,"topics":["micropackage","reduce","swift"],"latest_commit_sha":null,"homepage":"https://softwareengineerchris.github.io/Consolidate","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/SoftwareEngineerChris.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}},"created_at":"2019-09-28T08:18:12.000Z","updated_at":"2024-08-06T21:00:44.000Z","dependencies_parsed_at":"2023-02-11T18:30:50.469Z","dependency_job_id":null,"html_url":"https://github.com/SoftwareEngineerChris/Consolidate","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/SoftwareEngineerChris/Consolidate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftwareEngineerChris%2FConsolidate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftwareEngineerChris%2FConsolidate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftwareEngineerChris%2FConsolidate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftwareEngineerChris%2FConsolidate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SoftwareEngineerChris","download_url":"https://codeload.github.com/SoftwareEngineerChris/Consolidate/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftwareEngineerChris%2FConsolidate/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29608152,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-19T06:47:36.664Z","status":"ssl_error","status_checked_at":"2026-02-19T06:45:47.551Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["micropackage","reduce","swift"],"created_at":"2025-10-21T07:49:29.106Z","updated_at":"2026-02-19T08:01:40.585Z","avatar_url":"https://github.com/SoftwareEngineerChris.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Consolidate\n\n[![Build Status](https://app.bitrise.io/app/2401d8d24819bc28/status.svg?token=rpSxCpEdigqkV5zTb-dRog)](https://app.bitrise.io/app/2401d8d24819bc28)\n[![Docs](https://softwareengineerchris.github.io/Consolidate/badge.svg)](https://softwareengineerchris.github.io/Consolidate)\n[![SPM](https://img.shields.io/badge/SPM-Supported-informational)](#)\n\n## Installation\n\n### Swift Package Manager\n```swift\ndependencies: [\n   .package(url: \"https://github.com/SoftwareEngineerChris/Consolidate.git\", from: \"1.0.0\")\n]\n```\n\n## Usage Example\n\n### Using KeyPath\n\n```swift\nlet allTaxes = [\n    TaxAmount(name: \"Import Tax\", amount: 3.00),\n    TaxAmount(name: \"Sales Tax\", amount: 1.75),\n    TaxAmount(name: \"Import Tax\", amount: 2.30)\n]\n\nlet consolidatedTaxes = allTaxes.consolidated(by: \\.name) {\n    TaxAmount(tax: $0.name, amount: $0.amount + $1.amount)\n}\n\n// Would result in:\n\nlet consolidatedTaxes = [\n    TaxAmount(name: \"Import Tax\", amount: 5.30),\n    TaxAmount(name: \"Sales Tax\", amount: 4.10)\n]\n```\n\n### Using Closures\n\n```swift\nlet allTaxes = [\n    TaxAmount(name: \"Import Tax\", amount: 3.00),\n    TaxAmount(name: \"Sales Tax\", amount: 1.75),\n    TaxAmount(name: \"Import Tax\", amount: 2.30)\n]\n\nlet consolidatedTaxes = allTaxes.consolidated(by: { $0.name == $1.name }) {\n    TaxAmount(tax: $0.name, amount: $0.amount + $1.amount)\n}\n\n// Would result in:\n\n let consolidatedTaxes = [\n    TaxAmount(name: \"Import Tax\", amount: 5.30),\n    TaxAmount(name: \"Sales Tax\", amount: 4.10)\n]\n```\n\n### Using Consolidatable Protocol\n\n```swift\nstruct TaxAmount: Consolidatable {\n\n    let name: String\n    let amount: Decimal\n\n    var consolidationGroup: AnyHashable {\n        return name\n    }\n\n    func consolidate(with other: Self) -\u003e Self {\n        return .init(name: name, amount: amount + other.amount)\n    }\n}\n```\n\nThe above implementation would consolidate like this:\n\n\n```swift\nlet taxes = [\n    TaxAmount(name: \"Import Tax\", amount: 3.00),\n    TaxAmount(name: \"Sales Tax\", amount: 1.75),\n    TaxAmount(name: \"Import Tax\", amount: 2.30)\n].consolidated()\n\n// Results in:\n\nlet taxes = [\n    TaxAmount(name: \"Import Tax\", amount: 5.30),\n    TaxAmount(name: \"Sales Tax\", amount: 4.10)\n]\n```\n\n### Consolidate into single item (throws if unable to)\n\nThere may be times where you want to consolidate a collection of elements into a single element.\nFor this, there exists the array extension `consolidatedIntoSingle`. It is functionally similar to the other\nconsolidation methods, except its return type is of type `Element` rather than `[Element]`. This means that\nthe collection has to be consolidatable to a single element. If the collection can't be consolidated to a single\nelement, then the method will throw the error `ConsolidationError.couldNotBeConolidatedIntoSingleElement`.\n\n```swift\nlet allTaxes = [\n    TaxAmount(name: \"Import Tax\", amount: 3.00),\n    TaxAmount(name: \"Sales Tax\", amount: 1.75),\n    TaxAmount(name: \"Import Tax\", amount: 2.30)\n]\n\n// The line below would throw `ConsolidationError.couldNotBeConolidatedIntoSingleElement`,\n// since the taxes can't be consolidated into a single tax when using the name key-path.\n\nlet consolidatedTax = try allTaxes.consolidatedIntoSingle(by: \\.name) {\n    TaxAmount(tax: $0.name, amount: $0.amount + $1.amount)\n}\n```\n\nWhereas:\n\n```swift\nlet allTaxes = [\n    TaxAmount(name: \"Import Tax\", amount: 3.00),\n    TaxAmount(name: \"Import Tax\", amount: 2.30)\n]\n\nlet consolidatedTax = try allTaxes.consolidatedIntoSingle(by: \\.name) {\n    TaxAmount(tax: $0.name, amount: $0.amount + $1.amount)\n}\n\n// Since all taxes have the name \"Import Tax\", they can be consolidated into a single tax\n// when using the name key-path. The result would be:\n\nlet consolidatedTax = [\n    TaxAmount(name: \"Import Tax\", amount: 5.30)\n]\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftwareengineerchris%2Fconsolidate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoftwareengineerchris%2Fconsolidate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftwareengineerchris%2Fconsolidate/lists"}