{"id":16742049,"url":"https://github.com/theohbkim/Scope","last_synced_at":"2025-03-29T18:31:07.388Z","repository":{"id":42425230,"uuid":"159530186","full_name":"theohbkim/Scope","owner":"theohbkim","description":"🌷 Swift-Style Scoping Functions for Readable Code 🤗","archived":false,"fork":false,"pushed_at":"2024-05-08T14:00:27.000Z","size":43,"stargazers_count":9,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-18T05:35:02.655Z","etag":null,"topics":["cocoapods","ios","kotlin","macos","scoping-function","swift","swift-package-manager","tvos","watchos","xcode"],"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/theohbkim.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-11-28T16:15:49.000Z","updated_at":"2024-05-08T14:00:30.000Z","dependencies_parsed_at":"2024-05-10T08:31:58.402Z","dependency_job_id":null,"html_url":"https://github.com/theohbkim/Scope","commit_stats":null,"previous_names":["theokim24601/scope","hb1love/scope","theohkim/scope","theohbkim/scope"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theohbkim%2FScope","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theohbkim%2FScope/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theohbkim%2FScope/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theohbkim%2FScope/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/theohbkim","download_url":"https://codeload.github.com/theohbkim/Scope/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246226869,"owners_count":20743838,"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":["cocoapods","ios","kotlin","macos","scoping-function","swift","swift-package-manager","tvos","watchos","xcode"],"created_at":"2024-10-13T01:08:25.555Z","updated_at":"2025-03-29T18:31:07.077Z","avatar_url":"https://github.com/theohbkim.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Scope\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://swift.org\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/badge/Swift-5.3-orange.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://cocoapods.org/pods/Scope\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/cocoapods/p/Scope.svg?style=flat\"\u003e\u003c/a\u003e\n\u003ca href=\"https://cocoapods.org/pods/Scope\" target=\"_blank\"\u003e\u003cimg src=\"http://img.shields.io/cocoapods/v/Scope.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://swift.org/package-manager\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/badge/Swift%20Package%20Manager-compatible-4BC51D.svg?style=flat\"\u003e\u003c/a\u003e\n\u003cbr /\u003e\n\u003ca href=\"https://github.com/hb1love/Scope/actions\"\u003e\u003cimg src=\"https://github.com/hb1love/Scope/workflows/CI/badge.svg?branch=main\"\u003e\u003c/a\u003e\n\u003ca href=\"https://codecov.io/gh/hb1love/Scope\" target=\"_blank\"\u003e\u003cimg src=\"https://codecov.io/gh/hb1love/Scope/branch/main/graph/badge.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"LICENSE\"\u003e\u003cimg src=\"https://img.shields.io/github/license/hb1love/Scope\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\nScoping Functions of Swift Style for Readable Code\n\n## 🌷 Usage\n\n**`apply`**, **`also`** , **`let`**, **`with`**, **`run`** \n\n### Reture Value\n\nThe scope functions differ by the result they return:\n\n- `apply`, `also` return the context object.\n- `let`, `with`, and `run` return the closure result.\n\nThe return value of `also`  and `apply` is the context object itself. so they can be included into call chains as side steps.\n\n```swift\nlet org = Organization()\n  .also { print(\"new podo org\") }\n  .apply {\n    $0.name = \"podo\"\n    $0.member = Member(name: \"theo\", role: .owner)\n  }\n```\n\nThey also can be used in return statements of functions returning the context object.\n\n```swift\nfunc newPodoOrg() -\u003e Organization {\n  Organization().apply {\n    $0.name = \"podo\"\n    $0.member = Member(name: \"theo\", role: .owner)\n  }\n}\n```\n\n### apply\n\nUse `apply` for code blocks that don't return a value and mainly operate on the members of the receiver object. \n\n```swift\nlet org = Organization().apply {\n  $0.name = \"podo\"\n  $0.member = Member(name: \"theo\", role: .owner)\n}\nprint(org)\n```\n\n### also\n\nUse `also` for additional actions that don't alter the object, such as logging or printing debug information.\n\n```swift\nlet member = Member(name: \"theo\", role: .owner)\nlet org = Organization()\n\norg.also { print(\"new member\") }\n  .addMember(member)\n``` \n\n### let\n\n`let` can be used to invoke one or more functions on result of call chains.\n\n```swift\nlet numbers = [1, 5, 10]\nnumbers.map { $0 * 2 }\n  .filter { $0 \u003e 3}\n  .let { $0.count }\n```\n\n`let` is often used for executing a code block only with non-nil values.\n\n```swift\nvar org: Organization?\norg = Organization(name: \"podo\", member: Member(name: \"theo\", role: .member))\n\norg?.member?.let {\n  $0.role = .owner // $0 is not nil inside '?.let {}' \n}\n```\n\n### with\n\nA non-extension function: the context object is passed as an argument, and return value is the closure result.\n\n`with` can be read as *\"with this object, do the following.\"*\n\n```swift\nlet description = with(org) {\n  \"Orgnaization name is \\($0.name), \"\n    .appending(\"member name is \\($0.member.name)\")\n}\nprint(description)\n```\n\n### run\n\nUse `run` as a non-extension function. \nNon-extension `run` lets you execute a block of several statements where an expression is required.\n\n```swift\nlet hexNumberRegex = run { () -\u003e Regex in\n  let digits = \"0-9\"\n  let hexDigits = \"A-Fa-f\"\n  let sign = \"+-\"\n\n  return Regex(\"[\\(sign)]?[\\(digits)\\(hexDigits)]+\")\n}\n```\n\n## ⚙️ Installation\n\n- **Using [Swift Package Manager](https://swift.org/package-manager/)**\n\n  ```swift\n  import PackageDescription\n\n  let package = Package(\n    name: \"MyApp\",\n    dependencies: [\n      .package(url: \"https://github.com/hb1love/Scope\", .upToNextMajor(from: \"2.1.0\"))\n    ]\n  )\n  ```\n\n- **Using [CocoaPods](https://cocoapods.org)**\n\n  ```ruby\n  pod 'Scope'\n  ```\n\n## 👮‍ License\n\n**Scope** is available under the MIT license. See the [LICENSE](LICENSE) for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheohbkim%2FScope","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftheohbkim%2FScope","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftheohbkim%2FScope/lists"}