8000 Proposal: `hasOnly` object utility · Issue #558 · angus-c/just · GitHub
[go: up one dir, main page]

Skip to content

Proposal: hasOnly object utility #558

Open

Description

@arimgibson

Long time user of just, first time contributor!

I wanted to add a suggestion for a new object utility called hasOnly, which checks whether a specified object has only certain keys.

My ideal implementation is not ES5 compatible as it relies on Array.includes(). I've found some polyfills online but might need help from maintainers/anyone more ES5 familiar to ensure it works as expected still and covers all edge cases.

Have read the contribution guidelines and am willing to submit the necessary code, tests, and PR, but wanted to run it by maintainers before going through the work. Thanks!

import hasOnly from 'just-has-only'

hasOnly({ email: 'example@example.com', password: 'pass123' }, ['email']) // false
hasOnly({ email: 'example@example.com', password: 'pass123' }, ['email', 'password']) // true 

var obj = {
  a: 1,
  b: 2,
  c: 3,
  d: 4,
  e: 5
};

hasOnly(obj, ['b', 'c']) // false
hasOnly(obj, ['a', 'b', 'c', 'd']) // false
hasOnly(obj, ['a', 'b', 'c', 'd', 'e']) // true
function hasOnly(object: Record<string, any>, keys: string[]) {
  return Object.keys(object).every((key) => keys.includes(key));
}

export {hasOnly as default};

Activity

  1. changed the title [-]Proposal: `has-only` object utility[/-] [+]Proposal: `hasOnly` object utility[/+] on May 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0