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.

503 lines
17 KiB

4 years ago
  1. type Operator = "===" | "==" | "!==" | "!=" | "<>" | ">" | "<" | ">=" | "<="
  2. declare module 'collect.js' {
  3. export function collect<T>(collection?: T[] | Object): Collection<T>;
  4. export default function collect<T>(collection?: T[] | Object): Collection<T>;
  5. export class Collection<Item> {
  6. /**
  7. * The all method returns the underlying array represented by the collection.
  8. */
  9. all(): Item[];
  10. /**
  11. * Alias for the avg() method.
  12. */
  13. average<K>(key?: keyof Item | K): number;
  14. /**
  15. * The avg method returns the average of all items in the collection.
  16. */
  17. avg<K>(key?: keyof Item | K): number;
  18. /**
  19. * The chunk method breaks the collection into multiple, smaller collections of a given size.
  20. */
  21. chunk(size: number): Collection<Item[]>;
  22. /**
  23. * The collapse method collapses a collection of arrays into a single, flat collection.
  24. */
  25. collapse(): Collection<Item>;
  26. /**
  27. * The combine method combines the keys of the collection with the values of another array or collection.
  28. */
  29. combine<T, U>(array: U[]): Collection<T>;
  30. /**
  31. * The concat method is used to merge two or more collections/arrays/objects.
  32. */
  33. concat<T>(collectionOrArrayOrObject: Collection<T> | T[] | object): any;
  34. /**
  35. * The contains method determines whether the collection contains a given item.
  36. */
  37. contains<K, V>(key: keyof Item | K | Function, value?: V): boolean;
  38. /**
  39. * The count method returns the total number of items in the collection.
  40. */
  41. count(): number;
  42. /**
  43. * The crossJoin method cross joins the collection with the given array or collection, returning all possible permutations.
  44. */
  45. crossJoin<T>(values: T[]): Collection<[Item, T]>;
  46. /**
  47. * The dd method will console.log the collection and exit the current process.
  48. */
  49. dd(): void;
  50. /**
  51. * The diff method compares the collection against another collection or a plain array based on its values.
  52. * This method will return the values in the original collection that are not present in the given collection.
  53. */
  54. diff<T>(values: T[] | Collection<Item>): Collection<Item>;
  55. /**
  56. * The diffAssoc method compares the collection against another collection or a plain object based on its keys
  57. * and values. This method will return the key / value pairs in the original collection that are not present in
  58. * the given collection:
  59. */
  60. diffAssoc<T>(values: T[] | Collection<T>): Collection<Item>;
  61. /**
  62. * The diffKeys method compares the collection against another collection or a plain object based on its keys.
  63. * This method will return the key / value pairs in the original collection that are not present in the given collection.
  64. */
  65. diffKeys<K extends keyof Item>(object: object): Collection<K>;
  66. /**
  67. * The dump method outputs the results at that moment and then continues processing.
  68. */
  69. dump(): this;
  70. /**
  71. * The each method iterates over the items in the collection and passes each item to a callback.
  72. */
  73. each(fn: (item: Item) => void): this;
  74. /**
  75. * The every method may be used to verify that all elements of a collection pass a given truth test.
  76. */
  77. every(fn: (item: Item) => boolean): boolean;
  78. /**
  79. * The except method returns all items in the collection except for those with the specified keys.
  80. */
  81. except<K>(properties: K[]): Collection<Item>;
  82. /**
  83. * The filter method filters the collection using the given callback,
  84. * keeping only those items that pass a given truth test.
  85. */
  86. filter(fn: (item: Item) => boolean): Collection<Item>;
  87. filter(fn: (item: Item, key?: any) => boolean): Collection<Item>;
  88. /**
  89. * The first method returns the first element in the collection that passes a given truth test.
  90. */
  91. first<V>(fn?: (item: Item) => boolean, defaultValue?: (...any: any[]) => V | Item): Item;
  92. /**
  93. * The flatMap method iterates through the collection and passes each value to the given callback.
  94. * The callback is free to modify the item and return it, thus forming a new collection of modified items.
  95. * Then, the array is flattened by a level.
  96. */
  97. flatMap<T>(fn: (item: Item, key: any) => T): Collection<T>;
  98. /**
  99. * The flatten method flattens a multi-dimensional collection into a single dimension.
  100. */
  101. flatten(depth?: number): Collection<Item>;
  102. /**
  103. * The flip method swaps the collection's keys with their corresponding values.
  104. */
  105. flip(): Collection<Item>;
  106. /**
  107. * The forget method removes an item from the collection by its key.
  108. */
  109. forget<K>(key: keyof Item | K): this;
  110. /**
  111. * The forPage method returns a new collection containing the items that would be present on a given page number.
  112. * The method accepts the page number as its first argument
  113. * and the number of items to show per page as its second argument.
  114. */
  115. forPage(page: number, chunk: number): Collection<Item>;
  116. /**
  117. * The get method returns the item at a given key. If the key does not exist, null is returned.
  118. */
  119. get<K, V>(key: keyof Item | K, defaultValue?: (...any: any[]) => V | Item): Item | null;
  120. /**
  121. * The groupBy method groups the collection's items by a given key.
  122. *
  123. */
  124. groupBy<T, K>(key: ((item: Item, index?: number) => K) | keyof Item | K): Collection<T>;
  125. /**
  126. * The has method determines if one or more keys exists in the collection.
  127. */
  128. has<K>(key: keyof Item | K | (keyof Item)[]): boolean;
  129. /**
  130. * The implode method joins the items in a collection.
  131. * Its arguments depend on the type of items in the collection.
  132. *
  133. * If the collection contains arrays or objects,
  134. * you should pass the key of the attributes you wish to join,
  135. * and the "glue" string you wish to place between the values.
  136. */
  137. implode<K>(key: keyof Item | K, glue?: string): string;
  138. /**
  139. * The intersect method removes any values from the original collection
  140. * that are not present in the given array or collection.
  141. * The resulting collection will preserve the original collection's keys.
  142. */
  143. intersect(values: Item[] | Collection<Item>): Collection<Item>;
  144. /**
  145. * The intersectByKeys method removes any keys from the original collection
  146. * that are not present in the given array or collection.
  147. */
  148. intersectByKeys<K extends keyof Item>(values: Item | Collection<Item>): Collection<K>
  149. /**
  150. * The isEmpty method returns true if the collection is empty; otherwise, false is returned.
  151. */
  152. isEmpty(): boolean;
  153. /**
  154. * The isNotEmpty method returns true if the collection is not empty; otherwise, false is returned.
  155. */
  156. isNotEmpty(): boolean;
  157. /**
  158. * The keyBy method keys the collection by the given key.
  159. * If multiple items have the same key, only the last one will appear in the new collection.
  160. */
  161. keyBy<T, K>(key: keyof Item | K | Function): Collection<T>;
  162. /**
  163. * The keys method returns all of the collection's keys.
  164. */
  165. keys(): Collection<string>;
  166. /**
  167. * The last method returns the last element in the collection that passes a given truth test.
  168. */
  169. last(fn?: (item: Item) => boolean): Item;
  170. /**
  171. * The macro method lets you register custom methods.
  172. */
  173. macro(name: string, fn: Function): void;
  174. /**
  175. * The map method iterates through the collection and passes each value to the given callback.
  176. * The callback is free to modify the item and return it, thus forming a new collection of modified items.
  177. */
  178. map<T>(fn: (item: Item, index: any) => T): Collection<T>;
  179. /**
  180. * The mapInto method iterates through the collection and instantiates the given class with each element as a constructor.
  181. */
  182. mapInto<T extends Function>(ClassName: T): Collection<T>;
  183. /**
  184. * The mapToGroups method iterates through the collection and passes each value to the given callback.
  185. */
  186. mapToGroups(fn: Function): Collection<any>;
  187. /**
  188. * The mapWithKeys method iterates through the collection and passes each value to the given callback.
  189. * The callback should return an array where the first element represents the key
  190. * and the second element represents the value pair.
  191. */
  192. mapWithKeys<T>(fn: Function): Collection<T>;
  193. /**
  194. * The max method returns the maximum value of a given key.
  195. */
  196. max(key?: keyof Item | string): number;
  197. /**
  198. * The median method returns the median value of a given key.
  199. */
  200. median<K>(key?: keyof Item | K): Item;
  201. /**
  202. * The merge method merges the given object into the original collection.
  203. * If a key in the given object matches a key in the original collection,
  204. * the given objects value will overwrite the value in the original collection.
  205. */
  206. merge<T>(objectOrArray: object | T[]): Collection<T>;
  207. /**
  208. * The min method returns the minimum value of a given key.
  209. */
  210. min<K>(key?: keyof Item | K): number;
  211. /**
  212. * The mode method returns the mode value of a given key.
  213. */
  214. mode<K>(key?: keyof Item | K): Collection<Item> | null;
  215. /**
  216. * The nth method creates a new collection consisting of every n-th element.
  217. */
  218. nth(n: number, offset?: number): Collection<Item>;
  219. /**
  220. * The only method returns the items in the collection with the specified keys.
  221. */
  222. only<K>(properties: K[]): Collection<Item>;
  223. /**
  224. * The partition method may be combined with destructuring to separate elements
  225. * that pass a given truth test from those that do not.
  226. */
  227. partition(fn: (item: Item) => boolean): [Item[], Item[]];
  228. /**
  229. * The pipe method passes the collection to the given callback and returns the result.
  230. */
  231. pipe<U>(fn: (...any: any[]) => U): U;
  232. /**
  233. * The pluck method retrieves all of the values for a given key.
  234. */
  235. pluck<T, K, V>(value: keyof Item | V, key?: keyof Item | K): Collection<T>;
  236. /**
  237. * The pop method removes and returns the last item from the collection.
  238. */
  239. pop(): Item;
  240. /**
  241. * The prepend method adds an item to the beginning of the collection.
  242. */
  243. prepend<K, V>(value: V, key?: K): this;
  244. /**
  245. * The pull method removes and returns an item from the collection by its key.
  246. */
  247. pull<K>(key: keyof Item | K): Item | null;
  248. /**
  249. * The push method appends an item to the end of the collection.
  250. */
  251. push(item: Item): this;
  252. /**
  253. * The put method sets the given key and value in the collection.
  254. */
  255. put<K, V>(key: K, value: V): this;
  256. /**
  257. * The random method returns a random item from the collection.
  258. */
  259. random(length?: number): this | Item;
  260. /**
  261. * The reduce method reduces the collection to a single value,
  262. * passing the result of each iteration into the subsequent iteration.
  263. */
  264. reduce<T>(fn: (_carry: T | null, item: Item) => T, carry?: T): any;
  265. /**
  266. * The reject method filters the collection using the given callback.
  267. * The callback should return true if the item should be removed from the resulting collection.
  268. */
  269. reject(fn: (item: Item) => boolean): Collection<Item>;
  270. /**
  271. * The reverse method reverses the order of the collection's items.
  272. */
  273. reverse(): Collection<Item>;
  274. /**
  275. * The search method searches the collection for the given value and returns its key if found.
  276. * If the item is not found, false is returned.
  277. */
  278. search(valueOrFunction: Item | ((value: Item, key: number) => boolean), strict: boolean): any;
  279. /**
  280. * The shift method removes and returns the first item from the collection.
  281. */
  282. shift(): Item;
  283. /**
  284. * The shuffle method randomly shuffles the items in the collection.
  285. */
  286. shuffle(): this;
  287. /**
  288. * The slice method returns a slice of the collection starting at the given index.
  289. */
  290. slice(remove: number, limit?: number): Collection<Item>;
  291. /**
  292. * The sort method sorts the collection.
  293. */
  294. sort(fn?: (a: Item, b: Item) => number): Collection<Item>;
  295. /**
  296. * The sortBy method sorts the collection by the given key.
  297. * The sorted collection keeps the original array keys.
  298. */
  299. sortBy<V>(value: V): Collection<Item>;
  300. /**
  301. * The sortBy method sorts the collection by the given callback.
  302. * The sorted collection keeps the original array keys.
  303. */
  304. sortBy(fn: (item: Item) => number): Collection<Item>;
  305. /**
  306. * This method has the same signature as the sortBy method,
  307. * but will sort the collection in the opposite order.
  308. */
  309. sortByDesc<V>(value: V): Collection<Item>;
  310. /**
  311. * This method has the same signature as the sortBy method,
  312. * but will sort the collection in the opposite order.
  313. */
  314. sortByDesc(fn: (item: Item) => number): Collection<Item>;
  315. /**
  316. * The splice method removes and returns a slice of items starting at the specified index.
  317. * You may pass a second argument to limit the size of the resulting chunk.
  318. */
  319. splice(index: number, limit: number, replace?: Item[]): Collection<Item>;
  320. /**
  321. * The split method breaks a collection into the given number of groups.
  322. */
  323. split(numberOfGroups: number): Item[];
  324. /**
  325. * The sum method returns the sum of all items in the collection.
  326. */
  327. sum<K>(key?: keyof Item | K | ((item: Item) => number | string)): number | string;
  328. [Symbol.iterator]: () => Iterator<Item>;
  329. /**
  330. * The take method returns a new collection with the specified number of items:
  331. * You may also pass a negative integer to take the specified amount of items from the end of the collection.
  332. */
  333. take(length: number): Collection<Item>;
  334. /**
  335. * The tap method passes the collection to the given callback,
  336. * allowing you to "tap" into the collection at a specific point
  337. * and do something with the items while not affecting the collection itself.
  338. */
  339. tap(fn: (collection: Collection<Item>) => void): this;
  340. /**
  341. * The times method creates a new collection by invoking the callback a given amount of times.
  342. */
  343. times<T>(times: number, fn: (time: number) => T): T[];
  344. /**
  345. * The toArray method converts the collection into a plain array.
  346. * If the collection is an object, an array containing the values will be returned.
  347. */
  348. toArray<T>(): T[];
  349. /**
  350. * The toJson method converts the collection into JSON string.
  351. */
  352. toJson(): string;
  353. /**
  354. * The transform method iterates over the collection and calls the given callback with each item in the collection.
  355. * The items in the collection will be replaced by the values returned by the callback.
  356. */
  357. transform<T>(fn: (item: Item) => T): Collection<T>;
  358. /**
  359. * The union method adds the given array to the collection.
  360. * If the given array contains keys that are already in the original collection,
  361. * the original collection's values will be preferred.
  362. */
  363. union<T>(object: Object): Collection<T>;
  364. /**
  365. * The unique method returns all of the unique items in the collection.
  366. */
  367. unique<K>(key?: keyof Item | K | Function): Collection<Item>;
  368. /**
  369. * The unless method will execute the given callback when the first argument given to the method evaluates to false.
  370. */
  371. unless(value: boolean, fn: (this: any) => any, defaultFn: (this: any) => any): void;
  372. /**
  373. * The unwrap method will unwrap the given collection.
  374. */
  375. unwrap<T>(value: T[] | Collection<T>): T[];
  376. /**
  377. * The values method returns a new collection with the keys reset to consecutive integers.
  378. */
  379. values<T>(): Collection<T>;
  380. /**
  381. * The when method will execute the given callback when the first argument given to the method evaluates to true.
  382. */
  383. when(condition: boolean, fn: (this: any) => any, defaultFn: (this: any) => any): void;
  384. /**
  385. * The where method filters the collection by a given key / value pair.
  386. */
  387. where<K, V>(key: keyof Item | K, value: V): Collection<Item>;
  388. /**
  389. * The where method filters the collection by a given key / value pair.
  390. */
  391. where<K, V>(key: keyof Item | K, operator: Operator, value: V): Collection<Item>;
  392. /**
  393. * The whereIn method filters the collection by a given key / value contained within the given array.
  394. */
  395. whereIn<K, V>(key: keyof Item | K, values: V[]): Collection<Item>;
  396. /**
  397. * The whereNotIn method filters the collection by a given key / value not contained within the given array.
  398. */
  399. whereNotIn<K, V>(key: keyof Item | K, values: V[]): Collection<Item>;
  400. /**
  401. * The wrap method will wrap the given value in a collection.
  402. */
  403. wrap<T>(value: T | T[] | Collection<T>): Collection<T>;
  404. /**
  405. * The zip method merges together the values of the given array with the values
  406. * of the original collection at the corresponding index.
  407. */
  408. zip<T>(array: T[]): Collection<[Item, T]>;
  409. [macroFn: string]: Function;
  410. }
  411. }