Skip to content

Commit

Permalink
feat(utils): isArrayIndex
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Jul 20, 2023
1 parent a44c021 commit e42c69d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"yarn.lock"
],
"ignoreRegExpList": [
"/0x*/",
"/@flex-development\\/.*/",
"/from\\s+(['\"]).*\\1/",
"import\\(.*\\)"
Expand Down
13 changes: 13 additions & 0 deletions src/utils/__tests__/is-array-index.spec-d.ts
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>()
})
})
16 changes: 16 additions & 0 deletions src/utils/__tests__/is-array-index.spec.ts
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
})
})
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export { default as intersects } from './intersects'
export { default as isAppEnv } from './is-app-env'
export { default as isArray } from './is-array'
export { default as isArrayBuffer } from './is-array-buffer'
export { default as isArrayIndex } from './is-array-index'
export { default as isBigInt } from './is-big-int'
export { default as isBoolean } from './is-boolean'
export { default as isBooleanish } from './is-booleanish'
Expand Down
31 changes: 31 additions & 0 deletions src/utils/is-array-index.ts
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

0 comments on commit e42c69d

Please sign in to comment.