array-negative-index

1.0.0 • Public • Published

npm version code style: prettier

array-negative-index

Wrapper for JavaScript arrays to make them accept negative indexes. Comes with Snake-like looping teleportation.

Prerequisites

This project utilizes Proxies and the Array.prototype.at method, so please make sure your environment supports those two features.

For development purposes, we run tests using NodeJS's internal test runner.

Installation

[!NOTE] This project is not meant to be used in production. Use at your own risk.

Just install the package to your node project via npm:

npm install array-negative-index

Alternatively, clone the repo with:

$ git clone https://github.com/tkalmi/array-negative-index.git

Usage

// Simply import the module to the JS file you wish to use it in:
import { getArrayWithNegativeIndexes } from 'array-negative-index;
// or `const { getArrayWithNegativeIndexes } = require('array-negative-index');`

// Then, wrap an array you want to query with negative indexes
const array = getArrayWithNegativeIndexes([1, 2, 3, 4, 5, 6]);

// Now you can query the array using negative indexes, just like in, e.g., Python
const lastValue = array[-1]; // `6`
const secondToLastValue = array[-2] // `5`

// You can also enable Snake-like teleporting by passing the `enableSnakeTeleport: true` option to the array initializer:
const teleportArray = getArrayWithNegativeIndexes([1, 2, 3, 4, 5, 6], { enableSnakeTeleport: true });

// Once teleporting is enabled, indexes that would otherwise be out of range are looped back to the start (or end, in case the index is negative)
teleportArray[6] // `1`
teleportArray[100] // `5`
teleportArray[-7] // `6`

Package Sidebar

Install

npm i array-negative-index

Weekly Downloads

3

Version

1.0.0

License

MIT

Unpacked Size

6.79 kB

Total Files

5

Last publish

Collaborators

  • tomikalmi