Skip to content

Commit

Permalink
feat(utils): isArray
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed May 16, 2023
1 parent 703fd70 commit 469f4ac
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/utils/__tests__/is-array.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @file Type Tests - isArray
* @module tutils/utils/tests/unit-d/isArray
*/

import type testSubject from '../is-array'

describe('unit-d:utils/isArray', () => {
it('should guard T[] | readonly T[]', () => {
expectTypeOf<typeof testSubject>().guards.toEqualTypeOf<
unknown[] | readonly unknown[]
>()
})
})
16 changes: 16 additions & 0 deletions src/utils/__tests__/is-array.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @file Unit Tests - isArray
* @module tutils/utils/tests/unit/isArray
*/

import testSubject from '../is-array'

describe('unit:utils/isArray', () => {
it('should return false if value is not an array', () => {
expect(testSubject(faker.datatype.boolean())).to.be.false
})

it('should return true if value is an array', () => {
expect(testSubject([0, 1, 2, 3, 4])).to.be.true
})
})
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

export { default as isAppEnv } from './is-app-env'
export { default as isArray } from './is-array'
export { default as isBigInt } from './is-big-int'
export { default as isBoolean } from './is-boolean'
export { default as isBooleanish } from './is-booleanish'
Expand Down
20 changes: 20 additions & 0 deletions src/utils/is-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @file Utilities - isArray
* @module tutils/utils/isArray
*/

import isObject from './is-object'

/**
* Checks if `value` is an array.
*
* @template T - Array item type
*
* @param {unknown} value - Value to check
* @return {value is ReadonlyArray<T> | T[]} `true` if `value` is an array
*/
function isArray<T>(value: unknown): value is T[] | readonly T[] {
return isObject(value) && Reflect.get(value, 'constructor') === Array
}

export default isArray

0 comments on commit 469f4ac

Please sign in to comment.