collection.d.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. export type Matcher<T> =
  2. ((e: T) => boolean) |
  3. ((e: T, idx: number) => boolean) |
  4. ((e: T, key: string) => boolean) |
  5. any;
  6. export type Extractor<T, U=T[keyof T]> = ((e: T) => U) | string | number;
  7. export type ArrayCollection<T> = Array<T>;
  8. export type StringKeyValueCollection<T> = { [key: string]: T };
  9. export type NumberKeyValueCollection<T> = { [key: number]: T };
  10. export type KeyValueCollection<T> = StringKeyValueCollection<T> | NumberKeyValueCollection<T>;
  11. export type Collection<T> = KeyValueCollection<T> | ArrayCollection<T> | null | undefined;
  12. /**
  13. * Find element in collection.
  14. *
  15. * @param collection
  16. * @param matcher
  17. *
  18. * @return
  19. */
  20. export function find<T>(collection: Collection<T>, matcher: Matcher<T>): T | undefined;
  21. /**
  22. * Find element index in collection.
  23. *
  24. * @param collection
  25. * @param matcher
  26. *
  27. * @return
  28. */
  29. export function findIndex<T>(collection: Collection<T>, matcher: Matcher<T>): number | undefined;
  30. /**
  31. * Find element in collection.
  32. *
  33. * @param collection
  34. * @param matcher
  35. *
  36. * @return result
  37. */
  38. export function filter<T>(collection: Collection<T>, matcher: Matcher<T>): T[];
  39. /**
  40. * Iterate over collection; returning something
  41. * (non-undefined) will stop iteration.
  42. *
  43. * @param collection
  44. * @param iterator
  45. *
  46. * @return return result that stopped the iteration
  47. */
  48. export function forEach<T>(collection: Collection<T>, iterator: (item: T, convertKey: any /* TODO */) => boolean | void): T;
  49. /**
  50. * Return collection without element.
  51. *
  52. * @param arr
  53. * @param matcher
  54. *
  55. * @return
  56. */
  57. export function without<T>(arr: T[], matcher: Matcher<T>): T[];
  58. /**
  59. * Reduce collection, returning a single result.
  60. *
  61. * @param collection
  62. * @param iterator
  63. * @param result
  64. *
  65. * @return result returned from last iterator
  66. */
  67. export function reduce<T, V>(collection: Collection<T>, iterator: (result: V, entry: T, index: any) => V, result: V): V;
  68. /**
  69. * Return true if every element in the collection
  70. * matches the criteria.
  71. *
  72. * @param collection
  73. * @param matcher
  74. *
  75. * @return
  76. */
  77. export function every<T>(collection: Collection<T>, matcher: Matcher<T>): boolean;
  78. /**
  79. * Return true if some elements in the collection
  80. * match the criteria.
  81. *
  82. * @param collection
  83. * @param matcher
  84. *
  85. * @return
  86. */
  87. export function some<T>(collection: Collection<T>, matcher: Matcher<T>): boolean;
  88. /**
  89. * Transform a collection into another collection
  90. * by piping each member through the given fn.
  91. *
  92. * @param collection
  93. * @param fn
  94. *
  95. * @return transformed collection
  96. */
  97. export function map<T, U>(collection: Collection<T>, fn: (value: T, key: number) => U): U[];
  98. /**
  99. * Get the collections keys.
  100. *
  101. * @param collection
  102. *
  103. * @return
  104. */
  105. export function keys<T>(collection: Collection<T>): T extends Array<any> ? number[] : (keyof T)[];
  106. /**
  107. * Shorthand for `keys(o).length`.
  108. *
  109. * @param collection
  110. *
  111. * @return
  112. */
  113. export function size<T>(collection: Collection<T>): number;
  114. /**
  115. * Get the values in the collection.
  116. *
  117. * @param collection
  118. *
  119. * @return
  120. */
  121. export function values<T>(collection: Collection<T>): T[];
  122. /**
  123. * Group collection members by attribute.
  124. *
  125. * @param collection
  126. * @param extractor
  127. *
  128. * @return map with { attrValue => [ a, b, c ] }
  129. */
  130. export function groupBy<T>(collection: Collection<T>, extractor: Extractor<T>, grouped?: any): { [attrValue: string]: any[] };
  131. export function uniqueBy<T>(extractor: Extractor<T>, ...collections: Collection<T>[]): T[];
  132. export function unionBy<T>(extractor: Extractor<T>, ...collections: Collection<T>[]): T[];
  133. /**
  134. * Sort collection by criteria.
  135. *
  136. * @param collection
  137. * @param extractor
  138. *
  139. * @return
  140. */
  141. export function sortBy<T>(collection: Collection<T>, extractor: Extractor<T, number | string>): T[];
  142. /**
  143. * Create an object pattern matcher.
  144. *
  145. * @example
  146. *
  147. * const matcher = matchPattern({ id: 1 });
  148. *
  149. * let element = find(elements, matcher);
  150. *
  151. * @param pattern
  152. *
  153. * @return matcherFn
  154. */
  155. export function matchPattern<T>(pattern: T): (e: any) => boolean;