From 3f7136734b73394a48ef422615aacb9718b88885 Mon Sep 17 00:00:00 2001 From: Youngjun Choi Date: Sat, 20 Jul 2024 11:04:28 +0900 Subject: [PATCH] test: Add test cases for `take` (#252) Co-authored-by: Sojin Park --- docs/compatibility.md | 2 +- src/compat/array/take.spec.ts | 22 ++++++++++++++++++++++ src/compat/array/take.ts | 31 +++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/compat/array/take.spec.ts create mode 100644 src/compat/array/take.ts diff --git a/docs/compatibility.md b/docs/compatibility.md index 9fdf8320..3a2439d8 100644 --- a/docs/compatibility.md +++ b/docs/compatibility.md @@ -94,7 +94,7 @@ Even if a feature is marked "in review," it might already be under review to ens | [sortedUniq](https://lodash.com/docs/4.17.15#sortedUniq) | No support | | [sortedUniqBy](https://lodash.com/docs/4.17.15#sortedUniqBy) | No support | | [tail](https://lodash.com/docs/4.17.15#tail) | ✅ | -| [take](https://lodash.com/docs/4.17.15#take) | 📝 | +| [take](https://lodash.com/docs/4.17.15#take) | ✅ | | [takeRight](https://lodash.com/docs/4.17.15#takeRight) | 📝 | | [takeRightWhile](https://lodash.com/docs/4.17.15#takeRightWhile) | 📝 | | [takeWhile](https://lodash.com/docs/4.17.15#takeWhile) | 📝 | diff --git a/src/compat/array/take.spec.ts b/src/compat/array/take.spec.ts new file mode 100644 index 00000000..e43b368a --- /dev/null +++ b/src/compat/array/take.spec.ts @@ -0,0 +1,22 @@ +import { describe, expect, it } from 'vitest'; +import { take } from './take.ts'; + +describe('take', () => { + const array = [1, 2, 3]; + + it('should take the first two elements', () => { + expect(take(array, 2)).toEqual([1, 2]); + }); + + it('should return an empty array when `n` < `1`', () => { + [0, -1, -Infinity].forEach(n => { + expect(take(array, n)).toEqual([]); + }); + }); + + it('should return all elements when `n` >= `length`', () => { + [3, 4, 2 ** 32, Infinity].forEach(n => { + expect(take(array, n)).toEqual(array); + }); + }); +}); diff --git a/src/compat/array/take.ts b/src/compat/array/take.ts new file mode 100644 index 00000000..b2e6c9b7 --- /dev/null +++ b/src/compat/array/take.ts @@ -0,0 +1,31 @@ +import { take as takeToolkit } from '../../array/take.ts'; + +/** + * Returns a new array containing the first `count` elements from the input array `arr`. + * If `count` is greater than the length of `arr`, the entire array is returned. + * + * @template T - Type of elements in the input array. + * + * @param {T[]} arr - The array to take elements from. + * @param {number} count - The number of elements to take. + * @returns {T[]} A new array containing the first `count` elements from `arr`. + * + * @example + * // Returns [1, 2, 3] + * take([1, 2, 3, 4, 5], 3); + * + * @example + * // Returns ['a', 'b'] + * take(['a', 'b', 'c'], 2); + * + * @example + * // Returns [1, 2, 3] + * take([1, 2, 3], 5); + */ +export function take(arr: readonly T[], count: number): T[] { + if (count < 1) { + return []; + } + + return takeToolkit(arr, count); +}