exclude-object-keys.js 483 B

12345678910111213141516171819
  1. import {InvalidArgumentError} from '../errors/index.js';
  2. /**
  3. * Exclude object keys.
  4. *
  5. * @param obj
  6. * @param keys
  7. */
  8. export function excludeObjectKeys(obj, keys) {
  9. if (typeof obj !== 'object' || !obj || Array.isArray(obj))
  10. throw new InvalidArgumentError(
  11. 'Cannot exclude keys from a non-Object value, %v given.',
  12. obj,
  13. );
  14. const result = {...obj};
  15. keys = Array.isArray(keys) ? keys : [keys];
  16. keys.forEach(key => delete result[key]);
  17. return result;
  18. }