-
-
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.
Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
a44c021
commit e42c69d
Showing
5 changed files
with
62 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
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,13 @@ | ||
/** | ||
* @file Type Tests - isArrayIndex | ||
* @module tutils/utils/tests/unit-d/isArrayIndex | ||
*/ | ||
|
||
import type { Numeric } from '#src/types' | ||
import type testSubject from '../is-array-index' | ||
|
||
describe('unit-d:utils/isArrayIndex', () => { | ||
it('should guard Numeric', () => { | ||
expectTypeOf<typeof testSubject>().guards.toEqualTypeOf<Numeric>() | ||
}) | ||
}) |
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,16 @@ | ||
/** | ||
* @file Unit Tests - isArrayIndex | ||
* @module tutils/utils/tests/unit/isArrayIndex | ||
*/ | ||
|
||
import testSubject from '../is-array-index' | ||
|
||
describe('unit:utils/isArrayIndex', () => { | ||
it('should return false if value is not an array index', () => { | ||
expect(testSubject('.')).to.be.false | ||
}) | ||
|
||
it('should return true if value is an array index', () => { | ||
expect(testSubject(`${faker.number.int({ max: 13, min: 0 })}`)).to.be.true | ||
}) | ||
}) |
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
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,31 @@ | ||
/** | ||
* @file Utilities - isArrayIndex | ||
* @module tutils/utils/isArrayIndex | ||
*/ | ||
|
||
import type { Numeric } from '#src/types' | ||
import isNumeric from './is-numeric' | ||
|
||
/** | ||
* Checks if a value is an array index by ECMAScript Language Specification | ||
* standards. | ||
* | ||
* @see https://tc39.es/ecma262/#integer-index | ||
* | ||
* @todo examples | ||
* | ||
* @param {unknown} value - Value to check | ||
* @return {value is Numeric} `true` if `value` is array index | ||
*/ | ||
const isArrayIndex = (value: unknown): value is Numeric => { | ||
/** | ||
* Numeric value as a number. | ||
* | ||
* @const {number} num | ||
*/ | ||
const num: number = isNumeric(value) ? +value : Number.NaN | ||
|
||
return `${num}` !== value ? false : num >= 0 && num < 0xffff_ffff | ||
} | ||
|
||
export default isArrayIndex |