You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # pick-util [![Build Status](https://travis-ci.com/jonkemp/pick-util.svg?branch=master)](https://travis-ci.com/jonkemp/pick-util)
  2. > Return a copy of the object only containing the whitelisted properties. Alternatively accepts a predicate indicating which keys to pick.
  3. Inspired by `_.pick`. 😄
  4. ## Install
  5. Install with [npm](https://npmjs.org/package/pick-util)
  6. ```
  7. $ npm install pick-util
  8. ```
  9. Or [unpkg](https://unpkg.com/pick-util/)
  10. ```
  11. <script src="https://unpkg.com/pick-util@1.1.2/umd/index.js" />
  12. ```
  13. Check out the unit tests on [CodePen](https://codepen.io/jonkemp/full/OJVXabQ).
  14. ## Usage
  15. ```js
  16. const pick = require('pick-util');
  17. pick({ 'a': 1, 'b': '2', 'c': 3 }, ['a', 'c']);
  18. //=> { 'a': 1, 'c': 3 }
  19. pick({ name: 'moe', age: 50, userid: 'moe1' }, 'name', 'age');
  20. //=> { name: 'moe', age: 50 }
  21. const isNumber = obj => toString.call(obj) === '[object Number]';
  22. pick({ name: 'moe', age: 50, userid: 'moe1' }, (value) => isNumber(value));
  23. //=> { age: 50 }
  24. ```
  25. ---
  26. | **Like us a lot?** Help others know why you like us! **Review this package on [pkgreview.dev](https://pkgreview.dev/npm/pick-util)** | ➡ | [![Review us on pkgreview.dev](https://i.ibb.co/McjVMfb/pkgreview-dev.jpg)](https://pkgreview.dev/npm/pick-util) |
  27. | ----------------------------------------------------------------------------------------------------------------------------------------- | --- | --------------------------------------------------------------------------------------------------------------------- |
  28. ## API
  29. ### pick(object, *keys)
  30. #### object
  31. Type: `object`
  32. Default: `none`
  33. The object to filter.
  34. #### keys
  35. Type: `array` or comma separated list of `string` values or `function`
  36. Default: `none`
  37. Keys for the picked properties. Or a predicate indicating which keys to pick.
  38. ## License
  39. MIT