index.d.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Copyright (c) 2016, Lee Byron
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the MIT license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. */
  8. // Note: TypeScript already has built-in definitions for
  9. // Iterable, Iterator, AsyncIterable, and AsyncIterator so they are not
  10. // defined here. However you may need to configure TypeScript to include them.
  11. export const $$iterator: unique symbol
  12. export function isIterable(obj: any): obj is Iterable<any>
  13. export function isArrayLike(obj: any): obj is { length: number }
  14. export function isCollection(obj: any): obj is Iterable<any> | { length: number }
  15. export function getIterator<TValue>(
  16. iterable: Iterable<TValue>
  17. ): Iterator<TValue>
  18. export function getIterator(iterable: any): void | Iterator<any>
  19. export function getIteratorMethod<TValue>(
  20. iterable: Iterable<TValue>
  21. ): () => Iterator<TValue>
  22. export function getIteratorMethod(iterable: any): void | (() => Iterator<any>)
  23. export function createIterator<TValue>(
  24. collection: Iterable<TValue>
  25. ): Iterator<TValue>
  26. export function createIterator(collection: { length: number }): Iterator<any>
  27. export function createIterator(collection: any): void | Iterator<any>
  28. type ValueOf<TCollection> =
  29. TCollection extends Iterable<infer TValue> ? TValue : never
  30. export function forEach<TCollection extends Iterable<any>>(
  31. collection: TCollection,
  32. callbackFn: (value: ValueOf<TCollection>, index: number, collection: TCollection) => any,
  33. thisArg?: any
  34. ): void
  35. export function forEach<TCollection extends { length: number }>(
  36. collection: TCollection,
  37. callbackFn: (value: any, index: number, collection: TCollection) => any,
  38. thisArg?: any
  39. ): void
  40. export const $$asyncIterator: unique symbol
  41. export function isAsyncIterable(obj: any): obj is AsyncIterable<any>
  42. export function getAsyncIterator<TValue>(
  43. asyncIterable: AsyncIterable<TValue>
  44. ): AsyncIterator<TValue>
  45. export function getAsyncIterator(
  46. asyncIterable: any
  47. ): void | AsyncIterator<any>
  48. export function getAsyncIteratorMethod<TValue>(
  49. asyncIterable: AsyncIterable<TValue>
  50. ): () => AsyncIterator<TValue>
  51. export function getAsyncIteratorMethod(
  52. asyncIterable: any
  53. ): void | (() => AsyncIterator<any>)
  54. export function createAsyncIterator<TValue>(
  55. collection: AsyncIterable<TValue> | Iterable<Promise<TValue> | TValue>
  56. ): AsyncIterator<TValue>
  57. export function createAsyncIterator(
  58. collection: {length: number}
  59. ): AsyncIterator<any>
  60. export function createAsyncIterator(
  61. collection: any
  62. ): void | AsyncIterator<any>
  63. type ResolvedOf<TCollection> =
  64. TCollection extends AsyncIterable<infer TValue> ? TValue :
  65. TCollection extends Iterable<infer U> ?
  66. U extends Promise<infer TValue> ? TValue : U :
  67. never
  68. export function forAwaitEach<TCollection extends AsyncIterable<any>>(
  69. collection: TCollection,
  70. callbackFn: (value: ResolvedOf<TCollection>, index: number, collection: TCollection) => any,
  71. thisArg?: any
  72. ): Promise<void>
  73. export function forAwaitEach<TCollection extends Iterable<any>>(
  74. collection: TCollection,
  75. callbackFn: (value: ResolvedOf<TCollection>, index: number, collection: TCollection) => any,
  76. thisArg?: any
  77. ): Promise<void>
  78. export function forAwaitEach<TCollection extends { length: number }>(
  79. collection: TCollection,
  80. callbackFn: (value: any, index: number, collection: TCollection) => any,
  81. thisArg?: any
  82. ): Promise<void>