| 1 |
import { Observable } from '../Observable'; |
| 2 |
import { SchedulerLike } from '../types'; |
| 3 |
import { from } from './from'; |
| 4 |
|
| 5 |
/** |
| 6 |
* @deprecated Use `from(Object.entries(obj))` instead. Will be removed in v8. |
| 7 |
*/ |
| 8 |
export function pairs<T>(arr: readonly T[], scheduler?: SchedulerLike): Observable<[string, T]>; |
| 9 |
/** |
| 10 |
* @deprecated Use `from(Object.entries(obj))` instead. Will be removed in v8. |
| 11 |
*/ |
| 12 |
export function pairs<O extends Record<string, unknown>>(obj: O, scheduler?: SchedulerLike): Observable<[keyof O, O[keyof O]]>; |
| 13 |
/** |
| 14 |
* @deprecated Use `from(Object.entries(obj))` instead. Will be removed in v8. |
| 15 |
*/ |
| 16 |
export function pairs<T>(iterable: Iterable<T>, scheduler?: SchedulerLike): Observable<[string, T]>; |
| 17 |
/** |
| 18 |
* @deprecated Use `from(Object.entries(obj))` instead. Will be removed in v8. |
| 19 |
*/ |
| 20 |
export function pairs( |
| 21 |
n: number | bigint | boolean | ((...args: any[]) => any) | symbol, |
| 22 |
scheduler?: SchedulerLike |
| 23 |
): Observable<[never, never]>; |
| 24 |
|
| 25 |
/** |
| 26 |
* Convert an object into an Observable of `[key, value]` pairs. |
| 27 |
* |
| 28 |
* <span class="informal">Turn entries of an object into a stream.</span> |
| 29 |
* |
| 30 |
*  |
| 31 |
* |
| 32 |
* `pairs` takes an arbitrary object and returns an Observable that emits arrays. Each |
| 33 |
* emitted array has exactly two elements - the first is a key from the object |
| 34 |
* and the second is a value corresponding to that key. Keys are extracted from |
| 35 |
* an object via `Object.keys` function, which means that they will be only |
| 36 |
* enumerable keys that are present on an object directly - not ones inherited |
| 37 |
* via prototype chain. |
| 38 |
* |
| 39 |
* By default, these arrays are emitted synchronously. To change that you can |
| 40 |
* pass a {@link SchedulerLike} as a second argument to `pairs`. |
| 41 |
* |
| 42 |
* ## Example |
| 43 |
* |
| 44 |
* Converts an object to an Observable |
| 45 |
* |
| 46 |
* ```ts |
| 47 |
* import { pairs } from 'rxjs'; |
| 48 |
* |
| 49 |
* const obj = { |
| 50 |
* foo: 42, |
| 51 |
* bar: 56, |
| 52 |
* baz: 78 |
| 53 |
* }; |
| 54 |
* |
| 55 |
* pairs(obj).subscribe({ |
| 56 |
* next: value => console.log(value), |
| 57 |
* complete: () => console.log('Complete!') |
| 58 |
* }); |
| 59 |
* |
| 60 |
* // Logs: |
| 61 |
* // ['foo', 42] |
| 62 |
* // ['bar', 56] |
| 63 |
* // ['baz', 78] |
| 64 |
* // 'Complete!' |
| 65 |
* ``` |
| 66 |
* |
| 67 |
* ### Object.entries required |
| 68 |
* |
| 69 |
* In IE, you will need to polyfill `Object.entries` in order to use this. |
| 70 |
* [MDN has a polyfill here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries) |
| 71 |
* |
| 72 |
* @param obj The object to inspect and turn into an Observable sequence. |
| 73 |
* @param scheduler An optional IScheduler to schedule when resulting |
| 74 |
* Observable will emit values. |
| 75 |
* @returns An observable sequence of [key, value] pairs from the object. |
| 76 |
* @deprecated Use `from(Object.entries(obj))` instead. Will be removed in v8. |
| 77 |
*/ |
| 78 |
export function pairs(obj: any, scheduler?: SchedulerLike) { |
| 79 |
return from(Object.entries(obj), scheduler as any); |
| 80 |
} |