index.d.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. Filter object keys and values into a new object.
  3. @param object - The source object to filter properties from.
  4. @param predicate - Predicate function that determines whether a property should be assigned to the new object.
  5. @param keys - Property keys that should be assigned to the new object.
  6. @example
  7. ```
  8. import {includeKeys} from 'filter-obj';
  9. const object = {
  10. foo: true,
  11. bar: false
  12. };
  13. const newObject = includeKeys(object, (key, value) => value === true);
  14. //=> {foo: true}
  15. const newObject2 = includeKeys(object, ['bar']);
  16. //=> {bar: false}
  17. ```
  18. */
  19. export function includeKeys<ObjectType extends Record<PropertyKey, any>>(
  20. object: ObjectType,
  21. predicate: (
  22. key: keyof ObjectType,
  23. value: ObjectType[keyof ObjectType]
  24. ) => boolean
  25. ): Partial<ObjectType>;
  26. export function includeKeys<
  27. ObjectType extends Record<PropertyKey, any>,
  28. IncludedKeys extends keyof ObjectType,
  29. >(
  30. object: ObjectType,
  31. keys: readonly IncludedKeys[]
  32. ): Pick<ObjectType, IncludedKeys>;
  33. /**
  34. Filter object keys and values into a new object.
  35. @param object - The source object to filter properties from.
  36. @param predicate - Predicate function that determines whether a property should not be assigned to the new object.
  37. @param keys - Property keys that should not be assigned to the new object.
  38. @example
  39. ```
  40. import {excludeKeys} from 'filter-obj';
  41. const object = {
  42. foo: true,
  43. bar: false
  44. };
  45. const newObject = excludeKeys(object, (key, value) => value === true);
  46. //=> {bar: false}
  47. const newObject3 = excludeKeys(object, ['bar']);
  48. //=> {foo: true}
  49. ```
  50. */
  51. export function excludeKeys<ObjectType extends Record<PropertyKey, any>>(
  52. object: ObjectType,
  53. predicate: (
  54. key: keyof ObjectType,
  55. value: ObjectType[keyof ObjectType]
  56. ) => boolean
  57. ): Partial<ObjectType>;
  58. export function excludeKeys<
  59. ObjectType extends Record<PropertyKey, any>,
  60. ExcludedKeys extends keyof ObjectType,
  61. >(
  62. object: ObjectType,
  63. keys: readonly ExcludedKeys[]
  64. ): Omit<ObjectType, ExcludedKeys>;