{"id":13837711,"url":"https://github.com/joaolucasl/proposal-array-prototype-partition","last_synced_at":"2025-04-11T05:35:27.470Z","repository":{"id":98728778,"uuid":"290907745","full_name":"joaolucasl/proposal-array-prototype-partition","owner":"joaolucasl","description":"A TC39 proposal for an utility function that returns two arrays based on the items' conformity to a predicate.","archived":false,"fork":false,"pushed_at":"2020-08-30T23:16:51.000Z","size":30,"stargazers_count":73,"open_issues_count":1,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-02-17T12:40:31.857Z","etag":null,"topics":["array","array-methods","ecma","ecmascript","javascript","partition","proposal","tc39"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joaolucasl.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-08-27T23:58:37.000Z","updated_at":"2023-07-03T10:05:44.000Z","dependencies_parsed_at":"2023-05-25T01:00:16.037Z","dependency_job_id":null,"html_url":"https://github.com/joaolucasl/proposal-array-prototype-partition","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaolucasl%2Fproposal-array-prototype-partition","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaolucasl%2Fproposal-array-prototype-partition/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaolucasl%2Fproposal-array-prototype-partition/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joaolucasl%2Fproposal-array-prototype-partition/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joaolucasl","download_url":"https://codeload.github.com/joaolucasl/proposal-array-prototype-partition/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226233325,"owners_count":17592878,"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":["array","array-methods","ecma","ecmascript","javascript","partition","proposal","tc39"],"created_at":"2024-08-04T15:01:21.673Z","updated_at":"2024-11-24T21:15:41.807Z","avatar_url":"https://github.com/joaolucasl.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# `Array.prototype.partition`\n\nA proposal for an utility function for splitting an Array using a predicate.\nYou can find an initial implementation of a polyfill [here](polyfill.js)\n\n## Usage:\n\n```js\nconst numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];\nconst isEven = (n) =\u003e n % 2 === 0;\n\nconst [even, odd] = numbers.partition(n =\u003e isEven(n));\n```\n\n## Why not use `.filter` or `.reduce`?\nWhile `.filter` is a helpful way of eliminating unwanted items from your collection by filtering them out, wanting to simply remove the items is not always the case. The `.reduce` function can serve the purpose of manipulating the items, but it doesn't cover easily the need of having multiple arrays built for you depending on conditions you apply to the items.\n\n## Current State\n### Using `.filter`\n```js\nconst numbers = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,15]\n\nconst isEven = n =\u003e n % 2 === 0\n\nconst even = numbers.filter(n =\u003e isEven(n))\nconst odd = numbers.filter(n =\u003e !isEven(n)) \n```\n\n### Using `.reduce`\n```js\nconst numbers = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,15]\n\nconst isEven = n =\u003e n % 2 === 0\n\nconst [even, odd] = numbers.reduce(\n  ([even, odd], num) =\u003e \n    isEven(num) ? [even.concat(num), odd]: [even, odd.concat(num)],\n  [[], []]\n)\n\n```\n\n### Using `for`\n```js\nconst numbers = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,15]\n\nconst isEven = n =\u003e n % 2 === 0\n\nlet even = []\nlet odd = []\n\nfor (const number of numbers) {\n  isEven(number) ? even.push(number) : odd.push(number)\n}\n```\n\n\n## Prior art\n### Other Languages\n- Ruby - [`Enumerable.partition`](https://apidock.com/ruby/Enumerable/partition)\n- OCaml - [`List.partition`](https://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html)\n- Elixir - [`Enum.split_with`](https://hexdocs.pm/elixir/Enum.html#split_with/2)\n- Kotlin - [`Collection.partition`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/partition.html)\n- Erlang - [`List.partition`](https://erlang.org/doc/man/lists.html#partition-2)\n- Rust - [`trait.Iterator.partition`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.partition)\n- Common Lisp - [`partition`](https://common-lisp.net/project/bese/docs/arnesi/html/api/function_005FIT.BESE.ARNESI_003A_003APARTITION.html)\n\n### Javascript Ecosystem\n- Lodash - [`_.partition(collection, predicate)`](https://lodash.com/docs/4.17.15#partition)\n- Underscore - [`_.partition(list, predicate)`](http://underscorejs.org/#partition)\n- Ramda - [`R.partition(predicate, filterable)`](https://ramdajs.com/docs/#partition)\n- RxJS - [`Observable.partition(predicate)`](https://www.learnrxjs.io/learn-rxjs/operators/transformation/partition)\n- Collect.js - [`.partition(predicate)`](https://collect.js.org/api/partition.html)\n\n## Draft Spec\n\n### **Array.prototype.partition ( callbackfn [ , thisArg ] )**\n\n\u003e NOTE - _callbackfn_ should be a function that accepts three arguments and returns a value that is coercible to the Boolean value true or false. **partition** calls callbackfn once for each element in the array, in ascending order, and constructs two new arrays: one of all the values for which _callbackfn_ returns `true` and another of all the values for which _callbackfn_ returns `false`. _callbackfn_ is called only for elements of the array which actually exist; it is not called for missing elements of the array.\n\nIf a thisArg parameter is provided, it will be used as the `this` value for each invocation of _callbackfn_. If it is not provided, `undefined` is used instead.\n\n_callbackfn_ is called with three arguments: the value of the element, the index of the element, and the object being traversed.\n\n**partition** does not directly mutate the object on which it is called but the object may be mutated by the calls to _callbackfn_.\n\nThe range of elements processed by *partition* is set before the first call to callbackfn. Elements which are appended to the array after the call to **partition** begins will not be visited by _callbackfn_. If existing elements of the array are changed their value as passed to _callbackfn_ will be the value at the time **partition** visits them; elements that are deleted after the call to **partition** begins and before being visited are not visited.\n\nWhen the **partition** method is called with one or two arguments, the following steps are taken:\n```\n1. Let `O` be ? `ToObject(this value)`.\n2. Let `len` be ? `LengthOfArrayLike(O)`.\n3. If `IsCallable(callbackfn)` is `false`, throw a `TypeError` exception.\n4. Let `A` be ? ArraySpeciesCreate(O, 0).\n5. Let `B` be ? ArraySpeciesCreate(O, 0).\n6. Let `Tuple` be ? ArrayCreate(2).\n7. Let k be 0.\n8. Let `toA` be 0.\n9. Let `toB` be 0.\n10. Repeat while `k \u003c len`: \n    a. Let `Pk` be ! ToString(k).\n    b. Let `kPresent` be ? HasProperty(O, Pk).\n    c. If `kPresent` is `true`, then\n        i. Let `kValue` be ? `Get(O, Pk)`.\n        ii. Let `selected` be ! `ToBoolean(? Call(callbackfn, thisArg, « kValue, k, O »))`.\n        iii. If `selected` is `true`, then\n            1. Perform ? `CreateDataPropertyOrThrow(A, ! ToString(toA), kValue)`.\n            2. Set `toA` to `toA + 1`.\n        iv. Else,\n            1. Perform ? `CreateDataPropertyOrThrow(B, ! ToString(toB), kValue)`.\n            2. Set `toB` to `toB + 1`.\n    d. Set `k` to `k + 1`\n11. Perform ? `CreateDataPropertyOrThrow(Tuple, ! ToString(0), A)\n12. Perform ? `CreateDataPropertyOrThrow(Tuple, ! ToString(1), B)\n13. Return `Tuple`\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoaolucasl%2Fproposal-array-prototype-partition","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoaolucasl%2Fproposal-array-prototype-partition","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoaolucasl%2Fproposal-array-prototype-partition/lists"}