Rocky_Mountain_Vending/.pnpm-store/v10/files/b4/c7d91f812a1404499ee32805c4d5f371e20e663dfcff2497bdbfa89b28e2ae5e5f153cdaab395d25f9eb26739ec1d0d88aaef46b66bc2e81ff3acf596a55b9
DMleadgen 46d973904b
Initial commit: Rocky Mountain Vending website
Next.js website for Rocky Mountain Vending company featuring:
- Product catalog with Stripe integration
- Service areas and parts pages
- Admin dashboard with Clerk authentication
- SEO optimized pages with JSON-LD structured data

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-12 16:22:15 -07:00

68 lines
2.4 KiB
Text

import { ArgumentOutOfRangeError } from '../util/ArgumentOutOfRangeError';
import { Observable } from '../Observable';
import { OperatorFunction } from '../types';
import { filter } from './filter';
import { throwIfEmpty } from './throwIfEmpty';
import { defaultIfEmpty } from './defaultIfEmpty';
import { take } from './take';
/**
* Emits the single value at the specified `index` in a sequence of emissions
* from the source Observable.
*
* <span class="informal">Emits only the i-th value, then completes.</span>
*
* ![](elementAt.png)
*
* `elementAt` returns an Observable that emits the item at the specified
* `index` in the source Observable, or a default value if that `index` is out
* of range and the `default` argument is provided. If the `default` argument is
* not given and the `index` is out of range, the output Observable will emit an
* `ArgumentOutOfRangeError` error.
*
* ## Example
*
* Emit only the third click event
*
* ```ts
* import { fromEvent, elementAt } from 'rxjs';
*
* const clicks = fromEvent(document, 'click');
* const result = clicks.pipe(elementAt(2));
* result.subscribe(x => console.log(x));
*
* // Results in:
* // click 1 = nothing
* // click 2 = nothing
* // click 3 = MouseEvent object logged to console
* ```
*
* @see {@link first}
* @see {@link last}
* @see {@link skip}
* @see {@link single}
* @see {@link take}
*
* @throws {ArgumentOutOfRangeError} When using `elementAt(i)`, it delivers an
* `ArgumentOutOfRangeError` to the Observer's `error` callback if `i < 0` or the
* Observable has completed before emitting the i-th `next` notification.
*
* @param index Is the number `i` for the i-th source emission that has happened
* since the subscription, starting from the number `0`.
* @param defaultValue The default value returned for missing indices.
* @return A function that returns an Observable that emits a single item, if
* it is found. Otherwise, it will emit the default value if given. If not, it
* emits an error.
*/
export function elementAt<T, D = T>(index: number, defaultValue?: D): OperatorFunction<T, T | D> {
if (index < 0) {
throw new ArgumentOutOfRangeError();
}
const hasDefaultValue = arguments.length >= 2;
return (source: Observable<T>) =>
source.pipe(
filter((v, i) => i === index),
take(1),
hasDefaultValue ? defaultIfEmpty(defaultValue!) : throwIfEmpty(() => new ArgumentOutOfRangeError())
);
}