index.d.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Unique symbol cannot be declared in a namespace directly, so we declare it top-level
  2. // See: https://github.com/sindresorhus/map-obj/pull/38#discussion_r702396878
  3. declare const skipSymbol: unique symbol;
  4. declare namespace mapObject {
  5. type Mapper<
  6. SourceObjectType extends {[key: string]: any},
  7. MappedObjectKeyType extends string,
  8. MappedObjectValueType
  9. > = (
  10. sourceKey: keyof SourceObjectType,
  11. sourceValue: SourceObjectType[keyof SourceObjectType],
  12. source: SourceObjectType
  13. ) => [
  14. targetKey: MappedObjectKeyType,
  15. targetValue: MappedObjectValueType,
  16. mapperOptions?: mapObject.MapperOptions
  17. ] | typeof mapObject.mapObjectSkip;
  18. interface Options {
  19. /**
  20. Recurse nested objects and objects in arrays.
  21. @default false
  22. */
  23. deep?: boolean;
  24. /**
  25. Target object to map properties on to.
  26. @default {}
  27. */
  28. target?: {[key: string]: any};
  29. }
  30. interface DeepOptions extends Options {
  31. deep: true;
  32. }
  33. interface TargetOptions<TargetObjectType extends {[key: string]: any}> extends Options {
  34. target: TargetObjectType;
  35. }
  36. interface MapperOptions {
  37. /**
  38. Whether `targetValue` should be recursed.
  39. Requires `deep: true`.
  40. @default true
  41. */
  42. readonly shouldRecurse?: boolean;
  43. }
  44. /**
  45. Return this value from a `mapper` function to remove a key from an object.
  46. @example
  47. ```
  48. const mapObject = require('map-obj');
  49. const object = {one: 1, two: 2}
  50. const mapper = (key, value) => value === 1 ? [key, value] : mapObject.mapObjectSkip
  51. const result = mapObject(object, mapper);
  52. console.log(result);
  53. //=> {one: 1}
  54. ```
  55. */
  56. const mapObjectSkip: typeof skipSymbol
  57. }
  58. /**
  59. Map object keys and values into a new object.
  60. @param source - Source object to copy properties from.
  61. @param mapper - Mapping function.
  62. @example
  63. ```
  64. import mapObject = require('map-obj');
  65. const newObject = mapObject({foo: 'bar'}, (key, value) => [value, key]);
  66. //=> {bar: 'foo'}
  67. const newObject = mapObject({FOO: true, bAr: {bAz: true}}, (key, value) => [key.toLowerCase(), value]);
  68. //=> {foo: true, bar: {bAz: true}}
  69. const newObject = mapObject({FOO: true, bAr: {bAz: true}}, (key, value) => [key.toLowerCase(), value], {deep: true});
  70. //=> {foo: true, bar: {baz: true}}
  71. const newObject = mapObject({one: 1, two: 2}, (key, value) => value === 1 ? [key, value] : mapObject.mapObjectSkip);
  72. //=> {one: 1}
  73. ```
  74. */
  75. declare function mapObject<
  76. SourceObjectType extends object,
  77. TargetObjectType extends {[key: string]: any},
  78. MappedObjectKeyType extends string,
  79. MappedObjectValueType
  80. >(
  81. source: SourceObjectType,
  82. mapper: mapObject.Mapper<
  83. SourceObjectType,
  84. MappedObjectKeyType,
  85. MappedObjectValueType
  86. >,
  87. options: mapObject.DeepOptions & mapObject.TargetOptions<TargetObjectType>
  88. ): TargetObjectType & {[key: string]: unknown};
  89. declare function mapObject<
  90. SourceObjectType extends object,
  91. MappedObjectKeyType extends string,
  92. MappedObjectValueType
  93. >(
  94. source: SourceObjectType,
  95. mapper: mapObject.Mapper<
  96. SourceObjectType,
  97. MappedObjectKeyType,
  98. MappedObjectValueType
  99. >,
  100. options: mapObject.DeepOptions
  101. ): {[key: string]: unknown};
  102. declare function mapObject<
  103. SourceObjectType extends {[key: string]: any},
  104. TargetObjectType extends {[key: string]: any},
  105. MappedObjectKeyType extends string,
  106. MappedObjectValueType
  107. >(
  108. source: SourceObjectType,
  109. mapper: mapObject.Mapper<
  110. SourceObjectType,
  111. MappedObjectKeyType,
  112. MappedObjectValueType
  113. >,
  114. options: mapObject.TargetOptions<TargetObjectType>
  115. ): TargetObjectType & {[K in MappedObjectKeyType]: MappedObjectValueType};
  116. declare function mapObject<
  117. SourceObjectType extends {[key: string]: any},
  118. MappedObjectKeyType extends string,
  119. MappedObjectValueType
  120. >(
  121. source: SourceObjectType,
  122. mapper: mapObject.Mapper<
  123. SourceObjectType,
  124. MappedObjectKeyType,
  125. MappedObjectValueType
  126. >,
  127. options?: mapObject.Options
  128. ): {[K in MappedObjectKeyType]: MappedObjectValueType};
  129. export = mapObject;