Skip to content

Commit

Permalink
add(isArrayOfSame): add isArrayOfSame function
Browse files Browse the repository at this point in the history
  • Loading branch information
Akurganow committed Nov 20, 2023
1 parent df65a0b commit a3171d7
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
55 changes: 55 additions & 0 deletions __tests__/is-array-of-same.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { isArrayOfSame } from '../src'

// jest tests for isArrayOfSame
describe('isArrayOfSame', () => {
test('returns false for an empty array', () => {
expect(isArrayOfSame([])).toBe(false)
})
test('returns true for an array of strings', () => {
expect(isArrayOfSame(['a', 'b', 'c'])).toBe(true)
})
test('returns true for an array of numbers', () => {
expect(isArrayOfSame([1, 2, 3])).toBe(true)
})
test('returns true for an array of booleans', () => {
expect(isArrayOfSame([true, false, true])).toBe(true)
})
test('returns true for an array of functions', () => {
expect(isArrayOfSame([() => {
}, () => {
}])).toBe(true)
})
test('returns true for an array of objects', () => {
expect(isArrayOfSame([{ a: 1 }, { b: 2 }])).toBe(true)
})
test('returns true for an array of arrays', () => {
expect(isArrayOfSame([[1], [2]])).toBe(true)
})
test('returns true for an array of symbols', () => {
expect(isArrayOfSame([Symbol(), Symbol()])).toBe(true)
})
test('returns true for an array of bigints', () => {
expect(isArrayOfSame([BigInt(1), BigInt(2)])).toBe(true)
})
test('returns true for an array of regexps', () => {
expect(isArrayOfSame([/a/, /b/])).toBe(true)
})
test('returns true for an array of dates', () => {
expect(isArrayOfSame([new Date(), new Date()])).toBe(true)
})
test('returns true for an array of errors', () => {
expect(isArrayOfSame([new Error(), new Error()])).toBe(true)
})
test('returns true for an array of promises', () => {
expect(isArrayOfSame([Promise.resolve(), Promise.resolve()])).toBe(true)
})
test('returns true for an array of nulls', () => {
expect(isArrayOfSame([null, null])).toBe(true)
})
test('returns true for an array of undefineds', () => {
expect(isArrayOfSame([undefined, undefined])).toBe(true)
})
test('returns false for an array of mixed types', () => {
expect(isArrayOfSame([1, 'a', true])).toBe(false)
})
})
25 changes: 25 additions & 0 deletions src/utils/is-array-of-same.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Checks if a given array contains only elements of the same type.
*
* This function uses the `detect` function to determine the type of each element in the array.
* If all elements are of the same type, the function returns true. Otherwise, it returns false.
* An empty array will return false.
*
* @param {unknown[]} value - The array to check.
* @returns {boolean} True if all elements in the array are of the same type, false otherwise.
*/
export default function isArrayOfSame(value: unknown[]): boolean {
if (value.length === 0) return false
if (value.length === 1) return true

const firstElement = value[0]
const firstElementType = typeof firstElement

if (firstElementType === 'object') {
const firstElementConstructor = firstElement?.constructor

return value.every(item => item?.constructor === firstElementConstructor)
}

return value.every(item => typeof item === firstElementType)
}

0 comments on commit a3171d7

Please sign in to comment.