index.d.ts 17 KB

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