Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/itsnickbarry/is-array-index
Test whether an input is a valid Array index 🗂️
https://github.com/itsnickbarry/is-array-index
array index isarrayindex numeral wow
Last synced: 2 months ago
JSON representation
Test whether an input is a valid Array index 🗂️
- Host: GitHub
- URL: https://github.com/itsnickbarry/is-array-index
- Owner: ItsNickBarry
- License: mit
- Created: 2019-01-30T03:17:42.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-07-13T10:17:57.000Z (6 months ago)
- Last Synced: 2024-10-27T11:20:42.851Z (2 months ago)
- Topics: array, index, isarrayindex, numeral, wow
- Language: JavaScript
- Size: 27.3 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# isArrayIndex
Utility for determining whether an input is a valid Array index.
## Usage
Install the library:
```
npm install --save is-array-index
```The module contains a single function, which takes a possible Array index as an argument and returns whether it is valid:
```javascript
let isArrayIndex = require('is-array-index');isArrayIndex(0);
// => trueisArrayIndex('1');
// => trueisArrayIndex('1.0');
// => falseisArrayIndex(0.5);
// => false
```In some cases, it may make sense to allow for Array indices which exceed the maximum length of an array, but still satisfy the semantic requirements. This may be achieved by passing a second argument, a `boolean` indicating whether to unrestrict the theoretical length of an array. See the [Phonewords](https://github.com/itsnickbarry/phonewords) library for an example.
```javascript
const MAX_ARRAY_LENGTH = Math.pow(2, 32) - 1;isArrayIndex(MAX_ARRAY_LENGTH);
// => falseisArrayIndex(MAX_ARRAY_LENGTH, true);
// => trueisArrayIndex(Number.MAX_SAFE_INTEGER, true);
// => true
```### Proxy Example
A `Proxy` wrapped around an `Array` might need to distinguish between array indexing and the accessing of additional properties.
```javascript
let proxy = new Proxy([], {
get: function (target, property) {
if (isArrayIndex(property)) {
// execute additional processing
return true;
} else {
return target[property];
}
},
});
```