Repository: marpple/FxTS
Pull request #146 (2022-03-10 12:42:22)

fix: minimum length to be possible from 0

shine1594: @hg-pyun 
While we won't explicitly use length(or count) as zero, this is useful because it can be zero if computed dynamically 🙂 


Patch:
diff --git a/src/Lazy/drop.ts b/src/Lazy/drop.ts
index e9fea1a2..b262824d 100644
--- a/src/Lazy/drop.ts
+++ b/src/Lazy/drop.ts
@@ -1,6 +1,7 @@
 import ReturnIterableIteratorType from "../types/ReturnIterableIteratorType";
 import { isAsyncIterable, isIterable } from "../_internal/utils";
 import concurrent, { isConcurrent } from "./concurrent";
+import consume from "../consume";
 
 function* sync<A>(length: number, iterable: Iterable<A>): IterableIterator<A> {
   const iterator = iterable[Symbol.iterator]();
@@ -9,13 +10,8 @@ function* sync<A>(length: number, iterable: Iterable<A>): IterableIterator<A> {
       return iterator;
     },
   };
-  let i = 0;
-  // eslint-disable-next-line @typescript-eslint/no-unused-vars
-  for (const _ of iterableIterator) {
-    if (++i === length) {
-      yield* iterableIterator;
-    }
-  }
+  consume(iterableIterator, length);
+  return yield* iterableIterator;
 }
 
 async function* asyncSequential<A>(length: number, iterable: AsyncIterable<A>) {
@@ -25,13 +21,8 @@ async function* asyncSequential<A>(length: number, iterable: AsyncIterable<A>) {
       return iterator;
     },
   };
-  let i = 0;
-  // eslint-disable-next-line @typescript-eslint/no-unused-vars
-  for await (const _ of iterableIterator) {
-    if (++i === length) {
-      yield* iterableIterator;
-    }
-  }
+  await consume(iterableIterator, length);
+  return yield* iterableIterator;
 }
 
 function async<A>(
@@ -121,8 +112,8 @@ function drop<A extends Iterable<unknown> | AsyncIterable<unknown>>(
     };
   }
 
-  if (length < 1) {
-    throw new TypeError("'length' must be greater than 1");
+  if (length < 0) {
+    throw new RangeError("'length' must be greater than 0");
   }
 
   if (isIterable<A>(iterable)) {
diff --git a/src/Lazy/dropRight.ts b/src/Lazy/dropRight.ts
index ff85acff..0374a245 100644
--- a/src/Lazy/dropRight.ts
+++ b/src/Lazy/dropRight.ts
@@ -126,8 +126,8 @@ function dropRight<A extends Iterable<unknown> | AsyncIterable<unknown>>(
     };
   }
 
-  if (length < 1) {
-    throw new TypeError("'length' must be greater than 1");
+  if (length < 0) {
+    throw new RangeError("'length' must be greater than 0");
   }
 
   if (isArray(iterable)) {
diff --git a/src/Lazy/takeRight.ts b/src/Lazy/takeRight.ts
index a4cfd542..fe21764d 100644
--- a/src/Lazy/takeRight.ts
+++ b/src/Lazy/takeRight.ts
@@ -4,8 +4,7 @@ import IterableInfer from "../types/IterableInfer";
 import toArray from "../toArray";
 import isArray from "../isArray";
 import isString from "../isString";
-import concurrent from "./concurrent";
-import { isConcurrent } from "./concurrent";
+import concurrent, { isConcurrent } from "./concurrent";
 
 function* stringTakeRight(length: number, str: string) {
   const arr = Array.from(str);
@@ -121,7 +120,7 @@ function takeRight<A extends Iterable<unknown> | AsyncIterable<unknown>>(
   | AsyncIterableIterator<IterableInfer<A>>
   | ((iterable: A) => ReturnIterableIteratorType<A>) {
   if (l < 0) {
-    throw new TypeError("'length' must be greater than 0");
+    throw new RangeError("'length' must be greater than 0");
   }
 
   if (iterable === undefined) {


Test patch:
diff --git a/test/Lazy/drop.spec.ts b/test/Lazy/drop.spec.ts
index 6047a4e0..f7b116ee 100644
--- a/test/Lazy/drop.spec.ts
+++ b/test/Lazy/drop.spec.ts
@@ -20,6 +20,7 @@ describe("drop", function () {
         acc.push(a);
       }
       expect(acc).toEqual([3, 4, 5]);
+      expect(toArray(drop(0, [1, 2, 3, 4, 5]))).toEqual([1, 2, 3, 4, 5]);
     });
 
     it("should be able to be used as a curried function in the pipeline", function () {
@@ -42,6 +43,10 @@ describe("drop", function () {
         acc.push(a);
       }
       expect(acc).toEqual([3, 4, 5]);
+
+      expect(await toArray(drop(0, toAsync([1, 2, 3, 4, 5])))).toEqual([
+        1, 2, 3, 4, 5,
+      ]);
     });
 
     it("should be able to be used as a curried function in the pipeline", async function () {
diff --git a/test/Lazy/dropRight.spec.ts b/test/Lazy/dropRight.spec.ts
index 709c40c9..fe30a145 100644
--- a/test/Lazy/dropRight.spec.ts
+++ b/test/Lazy/dropRight.spec.ts
@@ -12,7 +12,7 @@ import { Concurrent } from "../../src/Lazy/concurrent";
 
 import { callFuncAfterTime, generatorMock } from "../utils";
 
-describe("dropRightRight", function () {
+describe("dropRight", function () {
   describe("sync", function () {
     it("should be discarded elements by length", function () {
       const acc = [];
@@ -20,6 +20,8 @@ describe("dropRightRight", function () {
         acc.push(a);
       }
       expect(acc).toEqual([1, 2, 3]);
+
+      expect(toArray(dropRight(0, [1, 2, 3, 4, 5]))).toEqual([1, 2, 3, 4, 5]);
     });
 
     it("should be discarded string by length", function () {
@@ -49,6 +51,10 @@ describe("dropRightRight", function () {
         acc.push(a);
       }
       expect(acc).toEqual([1, 2, 3]);
+
+      expect(await toArray(dropRight(0, [1, 2, 3, 4, 5]))).toEqual([
+        1, 2, 3, 4, 5,
+      ]);
     });
 
     it("should be able to be used as a curried function in the pipeline", async function () {
diff --git a/test/Lazy/takeRight.spec.ts b/test/Lazy/takeRight.spec.ts
index f18fd9d0..6c886527 100644
--- a/test/Lazy/takeRight.spec.ts
+++ b/test/Lazy/takeRight.spec.ts
@@ -1,4 +1,5 @@
 import {
+  concurrent,
   delay,
   filter,
   map,
@@ -7,7 +8,6 @@ import {
   takeRight,
   toArray,
   toAsync,
-  concurrent,
 } from "../../src/index";
 import { Concurrent } from "../../src/Lazy/concurrent";
 import { callFuncAfterTime, generatorMock } from "../utils";
@@ -21,6 +21,9 @@ describe("takeRight", function () {
       }
       expect(res).toEqual([4]);
 
+      const res0 = toArray(takeRight(0, [1, 2, 3, 4]));
+      expect(res0).toEqual([]);
+
       const res1 = toArray(takeRight(1, [1, 2, 3, 4]));
       expect(res1).toEqual([4]);
 
@@ -69,6 +72,9 @@ describe("takeRight", function () {
       }
       expect(res).toEqual([4]);
 
+      const res0 = await toArray(takeRight(0, toAsync([1, 2, 3, 4])));
+      expect(res0).toEqual([]);
+
       const res1 = await toArray(takeRight(1, toAsync([1, 2, 3, 4])));
       expect(res1).toEqual([4]);