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

Proposal: hasOnly object utility #558

Open
arimgibson opened this issue May 9, 2023 · 0 comments
Open

Proposal: hasOnly object utility #558

arimgibson opened this issue May 9, 2023 · 0 comments

Comments

@arimgibson
Copy link

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};
@arimgibson arimgibson changed the title Proposal: has-only object utility Proposal: hasOnly object utility May 9, 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

No branches or pull requests

1 participant