11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst {
51cb0ef41Sopenharmony_ci  Readable,
61cb0ef41Sopenharmony_ci} = require('stream');
71cb0ef41Sopenharmony_ciconst { deepStrictEqual, rejects, throws, strictEqual } = require('assert');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst { from } = Readable;
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst fromAsync = (...args) => from(...args).map(async (x) => x);
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst naturals = () => from(async function*() {
141cb0ef41Sopenharmony_ci  let i = 1;
151cb0ef41Sopenharmony_ci  while (true) {
161cb0ef41Sopenharmony_ci    yield i++;
171cb0ef41Sopenharmony_ci  }
181cb0ef41Sopenharmony_ci}());
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci{
211cb0ef41Sopenharmony_ci  // Synchronous streams
221cb0ef41Sopenharmony_ci  (async () => {
231cb0ef41Sopenharmony_ci    deepStrictEqual(await from([1, 2, 3]).drop(2).toArray(), [3]);
241cb0ef41Sopenharmony_ci    deepStrictEqual(await from([1, 2, 3]).take(1).toArray(), [1]);
251cb0ef41Sopenharmony_ci    deepStrictEqual(await from([]).drop(2).toArray(), []);
261cb0ef41Sopenharmony_ci    deepStrictEqual(await from([]).take(1).toArray(), []);
271cb0ef41Sopenharmony_ci    deepStrictEqual(await from([1, 2, 3]).drop(1).take(1).toArray(), [2]);
281cb0ef41Sopenharmony_ci    deepStrictEqual(await from([1, 2]).drop(0).toArray(), [1, 2]);
291cb0ef41Sopenharmony_ci    deepStrictEqual(await from([1, 2]).take(0).toArray(), []);
301cb0ef41Sopenharmony_ci  })().then(common.mustCall());
311cb0ef41Sopenharmony_ci  // Asynchronous streams
321cb0ef41Sopenharmony_ci  (async () => {
331cb0ef41Sopenharmony_ci    deepStrictEqual(await fromAsync([1, 2, 3]).drop(2).toArray(), [3]);
341cb0ef41Sopenharmony_ci    deepStrictEqual(await fromAsync([1, 2, 3]).take(1).toArray(), [1]);
351cb0ef41Sopenharmony_ci    deepStrictEqual(await fromAsync([]).drop(2).toArray(), []);
361cb0ef41Sopenharmony_ci    deepStrictEqual(await fromAsync([]).take(1).toArray(), []);
371cb0ef41Sopenharmony_ci    deepStrictEqual(await fromAsync([1, 2, 3]).drop(1).take(1).toArray(), [2]);
381cb0ef41Sopenharmony_ci    deepStrictEqual(await fromAsync([1, 2]).drop(0).toArray(), [1, 2]);
391cb0ef41Sopenharmony_ci    deepStrictEqual(await fromAsync([1, 2]).take(0).toArray(), []);
401cb0ef41Sopenharmony_ci  })().then(common.mustCall());
411cb0ef41Sopenharmony_ci  // Infinite streams
421cb0ef41Sopenharmony_ci  // Asynchronous streams
431cb0ef41Sopenharmony_ci  (async () => {
441cb0ef41Sopenharmony_ci    deepStrictEqual(await naturals().take(1).toArray(), [1]);
451cb0ef41Sopenharmony_ci    deepStrictEqual(await naturals().drop(1).take(1).toArray(), [2]);
461cb0ef41Sopenharmony_ci    const next10 = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
471cb0ef41Sopenharmony_ci    deepStrictEqual(await naturals().drop(10).take(10).toArray(), next10);
481cb0ef41Sopenharmony_ci    deepStrictEqual(await naturals().take(5).take(1).toArray(), [1]);
491cb0ef41Sopenharmony_ci  })().then(common.mustCall());
501cb0ef41Sopenharmony_ci}
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci// Don't wait for next item in the original stream when already consumed the requested take amount
541cb0ef41Sopenharmony_ci{
551cb0ef41Sopenharmony_ci  let reached = false;
561cb0ef41Sopenharmony_ci  let resolve;
571cb0ef41Sopenharmony_ci  const promise = new Promise((res) => resolve = res);
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  const stream = from((async function *() {
601cb0ef41Sopenharmony_ci    yield 1;
611cb0ef41Sopenharmony_ci    await promise;
621cb0ef41Sopenharmony_ci    reached = true;
631cb0ef41Sopenharmony_ci    yield 2;
641cb0ef41Sopenharmony_ci  })());
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci  stream.take(1)
671cb0ef41Sopenharmony_ci    .toArray()
681cb0ef41Sopenharmony_ci    .then(common.mustCall(() => {
691cb0ef41Sopenharmony_ci      strictEqual(reached, false);
701cb0ef41Sopenharmony_ci    }))
711cb0ef41Sopenharmony_ci    .finally(() => resolve());
721cb0ef41Sopenharmony_ci}
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci{
751cb0ef41Sopenharmony_ci  // Coercion
761cb0ef41Sopenharmony_ci  (async () => {
771cb0ef41Sopenharmony_ci    // The spec made me do this ^^
781cb0ef41Sopenharmony_ci    deepStrictEqual(await naturals().take('cat').toArray(), []);
791cb0ef41Sopenharmony_ci    deepStrictEqual(await naturals().take('2').toArray(), [1, 2]);
801cb0ef41Sopenharmony_ci    deepStrictEqual(await naturals().take(true).toArray(), [1]);
811cb0ef41Sopenharmony_ci  })().then(common.mustCall());
821cb0ef41Sopenharmony_ci}
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci{
851cb0ef41Sopenharmony_ci  // Support for AbortSignal
861cb0ef41Sopenharmony_ci  const ac = new AbortController();
871cb0ef41Sopenharmony_ci  rejects(
881cb0ef41Sopenharmony_ci    Readable.from([1, 2, 3]).take(1, { signal: ac.signal }).toArray(), {
891cb0ef41Sopenharmony_ci      name: 'AbortError',
901cb0ef41Sopenharmony_ci    }).then(common.mustCall());
911cb0ef41Sopenharmony_ci  rejects(
921cb0ef41Sopenharmony_ci    Readable.from([1, 2, 3]).drop(1, { signal: ac.signal }).toArray(), {
931cb0ef41Sopenharmony_ci      name: 'AbortError',
941cb0ef41Sopenharmony_ci    }).then(common.mustCall());
951cb0ef41Sopenharmony_ci  ac.abort();
961cb0ef41Sopenharmony_ci}
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci{
991cb0ef41Sopenharmony_ci  // Support for AbortSignal, already aborted
1001cb0ef41Sopenharmony_ci  const signal = AbortSignal.abort();
1011cb0ef41Sopenharmony_ci  rejects(
1021cb0ef41Sopenharmony_ci    Readable.from([1, 2, 3]).take(1, { signal }).toArray(), {
1031cb0ef41Sopenharmony_ci      name: 'AbortError',
1041cb0ef41Sopenharmony_ci    }).then(common.mustCall());
1051cb0ef41Sopenharmony_ci}
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci{
1081cb0ef41Sopenharmony_ci  // Error cases
1091cb0ef41Sopenharmony_ci  const invalidArgs = [
1101cb0ef41Sopenharmony_ci    -1,
1111cb0ef41Sopenharmony_ci    -Infinity,
1121cb0ef41Sopenharmony_ci    -40,
1131cb0ef41Sopenharmony_ci  ];
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci  for (const example of invalidArgs) {
1161cb0ef41Sopenharmony_ci    throws(() => from([]).take(example).toArray(), /ERR_OUT_OF_RANGE/);
1171cb0ef41Sopenharmony_ci  }
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci  throws(() => Readable.from([1]).drop(1, 1), /ERR_INVALID_ARG_TYPE/);
1201cb0ef41Sopenharmony_ci  throws(() => Readable.from([1]).drop(1, { signal: true }), /ERR_INVALID_ARG_TYPE/);
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci  throws(() => Readable.from([1]).take(1, 1), /ERR_INVALID_ARG_TYPE/);
1231cb0ef41Sopenharmony_ci  throws(() => Readable.from([1]).take(1, { signal: true }), /ERR_INVALID_ARG_TYPE/);
1241cb0ef41Sopenharmony_ci}
125