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: add reduceKeys function #29

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

feat: add reduceKeys function #29

wants to merge 2 commits into from

Conversation

aleclarson
Copy link
Member

@aleclarson aleclarson commented Jun 26, 2024

Tip

The owner of this PR can publish a preview release by commenting /publish in this PR. Afterwards, anyone can try it out by running pnpm add radashi@pr<PR_NUMBER>.

Description

Introduce the reduceKeys function which is like Array::reduce but for an object's own keys (i.e. not inherited). It uses the new filterKey function to support either an array of specific keys to reduce or a filter function for more advanced cases.

The basic function signature (minus the generics) is this:

function reduceKeys(
  obj: object,
  filter: KeyFilter,
  reducer: (
    acc: any,
    value: any,
    key: string | number | symbol,
    obj: object
  ) => any,
  initial?: any
): any

In a single loop, it calls the reducer for every key that matches the filter.

When filter is an array of property names, it's used instead of Object.keys for enumeration purposes.

Checklist

  • Changes are covered by tests if behavior has been changed or added
  • Tests have 100% coverage
  • If code changes were made, the documentation (in the /docs directory) has been updated

Resolves

@aleclarson aleclarson added the new feature This PR adds a new function or extends an existing one label Jun 28, 2024
@aleclarson aleclarson force-pushed the main branch 4 times, most recently from 35fc67a to 04bad7b Compare July 1, 2024 19:17
@aleclarson aleclarson added the stage 0: proposed A proposal for a change that is offered for community and team evaluation. label Sep 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature This PR adds a new function or extends an existing one stage 0: proposed A proposal for a change that is offered for community and team evaluation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant