Skip to content

Commit

Permalink
docs: Ensure compatibility of isArray with lodash
Browse files Browse the repository at this point in the history
  • Loading branch information
raon0211 committed Jul 21, 2024
1 parent e49c82b commit 35fbc4c
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand Down
2 changes: 1 addition & 1 deletion docs/ko/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand Down
2 changes: 1 addition & 1 deletion docs/zh_hans/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand Down
3 changes: 3 additions & 0 deletions src/compat/_internal/args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { toArgs } from './toArgs';

export const args = toArgs([1, 2, 3]);
12 changes: 12 additions & 0 deletions src/compat/_internal/toArgs.ts
Original file line number Diff line number Diff line change
@@ -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);
}
27 changes: 26 additions & 1 deletion src/compat/predicate/isArray.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -22,4 +24,27 @@ describe('isArray', function () {
expect(result2).toStrictEqual([[1, 2, 3]]);
expectTypeOf(result2).toEqualTypeOf<Array<readonly [1, 2, 3]>>();
});

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);
});
});
2 changes: 1 addition & 1 deletion src/compat/predicate/isArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
* console.log(isArray(value2)); // false
* console.log(isArray(value3)); // false
*/
export function isArray<T>(value: T): value is Extract<T, readonly unknown[]> {
export function isArray<T>(value?: T): value is Extract<T, readonly unknown[]> {
return Array.isArray(value);
}

0 comments on commit 35fbc4c

Please sign in to comment.