amp-index-of Array Utility
The `amp-index-of` package provides a lightweight utility function for arrays, serving as an `indexOf` implementation similar to `Array.prototype.indexOf`. It is a component of the Ampersand.js ecosystem, a modular, non-frameworky framework for client-side JavaScript applications, initially popular in the mid-2010s. The package is at version 1.1.0 and has not seen active development or releases for several years, aligning with the broader inactivity of the Ampersand.js project. Its original purpose was to offer a consistent array utility within the Ampersand.js environment. However, in modern JavaScript development, direct use of native `Array.prototype.indexOf` is preferred due to universal browser support and native performance. Therefore, `amp-index-of` is considered abandoned and primarily serves historical contexts or legacy applications built with Ampersand.js. Its release cadence was tied to the now-inactive Ampersand.js development cycle.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` in an ES Module context without proper transpilation or configuration.fixIf running in an ESM environment (e.g., Node.js with `"type": "module"` in package.json), you may need to use a CommonJS wrapper or switch to native `Array.prototype.indexOf`. -
TypeError: indexOf is not a function
cause Incorrect import of the `amp-index-of` function, or the module being used incorrectly after import.fixEnsure you are using `const indexOf = require('amp-index-of');` and then calling `indexOf(myArray, item)`.
Warnings
- breaking This package is part of the deprecated Ampersand.js ecosystem and is no longer actively maintained. No further updates, bug fixes, or security patches are expected. Consider migrating to native `Array.prototype.indexOf`.
- gotcha The package is CommonJS-only. Using ES Module `import` syntax directly will cause a runtime error (`require is not defined` or similar) in environments configured for ESM.
- gotcha This package provides identical functionality to the native `Array.prototype.indexOf` method, which is universally supported in modern browsers and Node.js. Using the native method avoids an unnecessary dependency.
Install
-
npm install amp-index-of -
yarn add amp-index-of -
pnpm add amp-index-of
Imports
- indexOf
import { indexOf } from 'amp-index-of';const indexOf = require('amp-index-of'); - * as indexOf
import * as indexOf from 'amp-index-of';
const indexOf = require('amp-index-of');
Quickstart
const indexOf = require('amp-index-of');
const myArray = ['apple', 'banana', 'orange', 'grape', 'banana'];
// Find the first occurrence of 'banana'
const firstIndex = indexOf(myArray, 'banana');
console.log(`First 'banana' found at index: ${firstIndex}`); // Expected: 1
// Find an element that does not exist
const notFoundIndex = indexOf(myArray, 'kiwi');
console.log(`'kiwi' found at index: ${notFoundIndex}`); // Expected: -1
// Demonstrating behavior similar to Array.prototype.indexOf
const anotherArray = [10, 20, 30, 10, 40];
console.log(`Native indexOf of 10: ${anotherArray.indexOf(10)}`); // Expected: 0
console.log(`amp-index-of of 10: ${indexOf(anotherArray, 10)}`); // Expected: 0