Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(collections): add partitionEntries #3365

Merged
merged 1 commit into from
May 7, 2023

Conversation

scarf005
Copy link
Contributor

@scarf005 scarf005 commented May 6, 2023

Rationale

The collection functions for arrays and objects are mostly in parity, such as filter{keys,values,entries}. However, partition is available only for arrays.

Implementaion and testing

bulk of the code and test are copied from filterEntries, as the only difference was not discarding entries that does not match given predicate.

Example usage

Example usages
const args = {
  "armor_bullet": 30,
  "id": "item_id1",
  "name": "item1",
  "armor_bash": 46,
  "material": "nanite",
  "armor_cut": 30,
  "weight": "460 g",
  "volume": "430 ml",
  "armor_acid": 80,
  "armor_foo": 123,
  "armor_bar": 456,
  "armor_baz": 789,
  "flags": ["foo", "bar", "baz"],
}

before:

import { partition } from "https://deno.land/std/collections/partition.ts"

const [armors, notArmors] = partition(Object.entries(args), ([key]) => key.startsWith("armor_"))
const armorskeys = armors.map(([key, value]) => [key.replace("armor_", ""), value])

const result = {
  armor: Object.fromEntries(armorskeys),
  ...Object.fromEntries(notArmors),
}

after:

import { partitionEntries } from "https://deno.land/std/collections/partition_entries.ts"

const [armors, notArmors] = partitionEntries(args, ([key]) => key.startsWith("armor_"))
const armorskeys = mapKeys(armors, (key) => key.replace("armor_", ""))
  
const result = {
  armor: armorskeys,
  ...notArmors,
}

@scarf005 scarf005 requested a review from kt3k as a code owner May 6, 2023 05:07
@CLAassistant
Copy link

CLAassistant commented May 6, 2023

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a reasonable addition to me. LGTM

@kt3k kt3k merged commit a951ed6 into denoland:main May 7, 2023
7 checks passed
@scarf005 scarf005 deleted the partition-entries branch May 7, 2023 06:55
mxdvl pushed a commit to mxdvl/deno_std that referenced this pull request May 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants