{"id":20545488,"url":"https://github.com/eonist/with","last_synced_at":"2025-04-14T10:12:51.767Z","repository":{"id":64060163,"uuid":"160323426","full_name":"eonist/With","owner":"eonist","description":"Manipulate an object with a closure","archived":false,"fork":false,"pushed_at":"2025-01-23T11:23:56.000Z","size":51,"stargazers_count":16,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-14T10:12:46.383Z","etag":null,"topics":["closure","swift","swift-package-manager","swift5","with"],"latest_commit_sha":null,"homepage":"","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/eonist.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":"2018-12-04T08:24:31.000Z","updated_at":"2025-01-23T11:24:00.000Z","dependencies_parsed_at":"2024-11-06T14:18:37.748Z","dependency_job_id":"95294e13-b217-4e13-b414-90a6efdf6286","html_url":"https://github.com/eonist/With","commit_stats":{"total_commits":12,"total_committers":1,"mean_commits":12.0,"dds":0.0,"last_synced_commit":"f09b483c52bd84ec433871b5f99a55b076edd612"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eonist%2FWith","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eonist%2FWith/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eonist%2FWith/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eonist%2FWith/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eonist","download_url":"https://codeload.github.com/eonist/With/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248860168,"owners_count":21173342,"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","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":["closure","swift","swift-package-manager","swift5","with"],"created_at":"2024-11-16T01:52:24.224Z","updated_at":"2025-04-14T10:12:51.754Z","avatar_url":"https://github.com/eonist.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# With 💗\n![mit](https://img.shields.io/badge/License-MIT-brightgreen.svg) ![platform](https://img.shields.io/badge/Platform-iOS-blue.svg) ![Lang](https://img.shields.io/badge/Language-Swift%205.0-orange.svg)\n[![SPM compatible](https://img.shields.io/badge/SPM-compatible-4BC51D.svg?style=flat)](https://github.com/apple/swift)\n[![Tests](https://github.com/eonist/With/actions/workflows/Tests.yml/badge.svg)](https://github.com/eonist/With/actions/workflows/Tests.yml)\n[![codebeat badge](https://codebeat.co/badges/a8f6fe0d-17b0-4d17-a781-c6d5b8930b2a)](https://codebeat.co/projects/github-com-eonist-with-master)\n\n### What is it\nAn extension that let's you manipulate an object with a closure\n\n### Description: \nWith is a Swift extension that lets you manipulate an object with a closure. It provides a concise and expressive way to modify an object's properties without having to create temporary variables or write boilerplate code.\n\n### How does it work\nWith works by taking an object and a closure as input. The closure takes an inout reference to the object, allowing you to modify its properties directly. The modified object is then returned by the with function.\nSee [Example](https://github.com/eonist/With#example)\n\n### How can I get it\nYou can add With to your project using Swift Package Manager by adding the following line to your Package.swift file: `.package(url: \"https://github.com/eonist/With.git\", branch: `\"master\")`\n\n### Example:\n\n```swift\n// Example 1:\nlet rectangle: CGRect = with(.init(x: 0, y: 0, width: 100, height: 100)) {\n  $0 = $0.offsetBy(dx: 20, dy: 20)\n  $0 = $0.insetBy(dx: 10, dy: 10)\n}\nSwift.print(rectangle) // X:30.0, y:30.0, width:80.0, height:80.0\n\n// Example 2:\nlet color: UIColor = with(.init(red: 50, green: 100, blue: 0, alpha: 0.9)) { ( col:inout UIColor) -\u003e Void  in\n  col = col.withAlphaComponent(0.2)\n}\nSwift.print(color.cgColor.alpha) // 0.2\n\n// Example 3:\nvar size: CGSize = .init(width: 50, height: 40)\nwith(size) {\n  $0.width = 100\n  $0.height = 50\n}\nSwift.print(size)//100, 50\n\n// Example 4:\nfunc createImageView() -\u003e UIImageView {\n   return with(.init()){\n      $0.image = UIImage(named: \"someGraphic\")\n      self.addSubview($0)\n   }\n}\ncreateImage() // Adds image to view\n```\n\n**Example for with Using Key Paths:**\n```swift\n// Using with function with key paths to configure a UILabel\nlet label = UILabel()\n  .with(\\.textColor, setTo: .red)\n  .with(\\.text, setTo: \"Hello World\")\n  .with(\\.textAlignment, setTo: .center)\n  .with(\\.layer.cornerRadius, setTo: 5)\n```\n\n**Example for withMap:**\n\n```swift\n// Using withMap to configure DateFormatter and get a formatted date string\nlet dateString: String = withMap(DateFormatter()) {\n  $0.dateStyle = .medium\n  $0.timeStyle = .none\n  return $0.string(from: Date())\n}\nSwift.print(dateString) // Outputs: Jan 1, 2022\n```\n\n\n### Credit:\n\nThanks [https://github.com/sindresorhus](https://github.com/sindresorhus) for teaching me this JavaScript-esque super power 💪\n\n### Todo:\n- Add examples for withMap and with that uses keypath to readme\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feonist%2Fwith","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feonist%2Fwith","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feonist%2Fwith/lists"}