-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add(isArrayOfSame): add isArrayOfSame function
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |