From 35fbc4caa7ab1165c33ad2b94ff5bc2a84dc84df Mon Sep 17 00:00:00 2001 From: Sojin Park Date: Sun, 21 Jul 2024 10:41:25 +0900 Subject: [PATCH] docs: Ensure compatibility of isArray with lodash --- docs/compatibility.md | 2 +- docs/ko/compatibility.md | 2 +- docs/zh_hans/compatibility.md | 2 +- src/compat/_internal/args.ts | 3 +++ src/compat/_internal/toArgs.ts | 12 ++++++++++++ src/compat/predicate/isArray.spec.ts | 27 ++++++++++++++++++++++++++- src/compat/predicate/isArray.ts | 2 +- 7 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 src/compat/_internal/args.ts create mode 100644 src/compat/_internal/toArgs.ts diff --git a/docs/compatibility.md b/docs/compatibility.md index a8298c0ad..743c8c870 100644 --- a/docs/compatibility.md +++ b/docs/compatibility.md @@ -195,7 +195,7 @@ Even if a feature is marked "in review," it might already be under review to ens | [gt](https://lodash.com/docs/4.17.15#gt) | ❌ | | [gte](https://lodash.com/docs/4.17.15#gte) | ❌ | | [isArguments](https://lodash.com/docs/4.17.15#isArguments) | ❌ | -| [isArray](https://lodash.com/docs/4.17.15#isArray) | ❌ | +| [isArray](https://lodash.com/docs/4.17.15#isArray) | ✅ | | [isArrayBuffer](https://lodash.com/docs/4.17.15#isArrayBuffer) | ❌ | | [isArrayLike](https://lodash.com/docs/4.17.15#isArrayLike) | ❌ | | [isArrayLikeObject](https://lodash.com/docs/4.17.15#isArrayLikeObject) | ❌ | diff --git a/docs/ko/compatibility.md b/docs/ko/compatibility.md index bcfc950a5..6f09ebe51 100644 --- a/docs/ko/compatibility.md +++ b/docs/ko/compatibility.md @@ -196,7 +196,7 @@ chunk([1, 2, 3, 4], 0); | [gt](https://lodash.com/docs/4.17.15#gt) | ❌ | | [gte](https://lodash.com/docs/4.17.15#gte) | ❌ | | [isArguments](https://lodash.com/docs/4.17.15#isArguments) | ❌ | -| [isArray](https://lodash.com/docs/4.17.15#isArray) | ❌ | +| [isArray](https://lodash.com/docs/4.17.15#isArray) | ✅ | | [isArrayBuffer](https://lodash.com/docs/4.17.15#isArrayBuffer) | ❌ | | [isArrayLike](https://lodash.com/docs/4.17.15#isArrayLike) | ❌ | | [isArrayLikeObject](https://lodash.com/docs/4.17.15#isArrayLikeObject) | ❌ | diff --git a/docs/zh_hans/compatibility.md b/docs/zh_hans/compatibility.md index a0e52fcbb..64d4d2b20 100644 --- a/docs/zh_hans/compatibility.md +++ b/docs/zh_hans/compatibility.md @@ -195,7 +195,7 @@ chunk([1, 2, 3, 4], 0); | [gt](https://lodash.com/docs/4.17.15#gt) | ❌ | | [gte](https://lodash.com/docs/4.17.15#gte) | ❌ | | [isArguments](https://lodash.com/docs/4.17.15#isArguments) | ❌ | -| [isArray](https://lodash.com/docs/4.17.15#isArray) | ❌ | +| [isArray](https://lodash.com/docs/4.17.15#isArray) | ✅ | | [isArrayBuffer](https://lodash.com/docs/4.17.15#isArrayBuffer) | ❌ | | [isArrayLike](https://lodash.com/docs/4.17.15#isArrayLike) | ❌ | | [isArrayLikeObject](https://lodash.com/docs/4.17.15#isArrayLikeObject) | ❌ | diff --git a/src/compat/_internal/args.ts b/src/compat/_internal/args.ts new file mode 100644 index 000000000..50aeaf18d --- /dev/null +++ b/src/compat/_internal/args.ts @@ -0,0 +1,3 @@ +import { toArgs } from './toArgs'; + +export const args = toArgs([1, 2, 3]); diff --git a/src/compat/_internal/toArgs.ts b/src/compat/_internal/toArgs.ts new file mode 100644 index 000000000..05b7d50e4 --- /dev/null +++ b/src/compat/_internal/toArgs.ts @@ -0,0 +1,12 @@ +/** + * Converts `array` to an `arguments` object. + * + * @private + * @param {Array} array The array to convert. + * @returns {Object} Returns the converted `arguments` object. + */ +export function toArgs(array: any[]) { + return function (..._: any[]) { + return arguments; + }.apply(undefined, array); +} diff --git a/src/compat/predicate/isArray.spec.ts b/src/compat/predicate/isArray.spec.ts index 1c9b98e40..7f82fd918 100644 --- a/src/compat/predicate/isArray.spec.ts +++ b/src/compat/predicate/isArray.spec.ts @@ -1,5 +1,7 @@ import { describe, expect, expectTypeOf, it } from 'vitest'; -import { isArray } from '../compat/predicate/isArray'; +import { isArray } from './isArray'; +import { falsey } from '../_internal/falsey'; +import { args } from '../_internal/args'; describe('isArray', function () { it('returns true if value is an array', () => { @@ -22,4 +24,27 @@ describe('isArray', function () { expect(result2).toStrictEqual([[1, 2, 3]]); expectTypeOf(result2).toEqualTypeOf>(); }); + + it('should return `true` for arrays', () => { + expect(isArray([1, 2, 3])).toBe(true); + }); + + it('should return `false` for non-arrays', () => { + const expected = falsey.map(() => false); + + const actual = falsey.map((value, index) => (index ? isArray(value) : isArray())); + + expect(actual).toEqual(expected); + + expect(isArray(args)).toBe(false); + expect(isArray(true)).toBe(false); + expect(isArray(new Date())).toBe(false); + expect(isArray(new Error())).toBe(false); + expect(isArray(Array.prototype.slice)).toBe(false); + expect(isArray({ 0: 1, length: 1 })).toBe(false); + expect(isArray(1)).toBe(false); + expect(isArray(/x/)).toBe(false); + expect(isArray('a')).toBe(false); + expect(isArray(Symbol('a'))).toBe(false); + }); }); diff --git a/src/compat/predicate/isArray.ts b/src/compat/predicate/isArray.ts index 844bc856c..c7dad74c2 100644 --- a/src/compat/predicate/isArray.ts +++ b/src/compat/predicate/isArray.ts @@ -19,6 +19,6 @@ * console.log(isArray(value2)); // false * console.log(isArray(value3)); // false */ -export function isArray(value: T): value is Extract { +export function isArray(value?: T): value is Extract { return Array.isArray(value); }