1.0.2 • Published 5 years ago

star-wars-array v1.0.2

Weekly downloads
17
License
MIT
Repository
github
Last release
5 years ago

star-wars-array

Node package for creating an array which is indexed in the order of the Star Wars films.

In other words, the indices for each array starts as follows:

345012678...

NOTE: This is an esoteric (joke) package. Although if you manage to actually use it in a meaningful way, I will be really impressed.

Usage

Installing

$ npm i -s star-wars-array

Example

Usage is similar to a regular array.

const SWArray = require("star-wars-array");

let array = new SWArray([1, 2, 3, 4, 5, 6]);
console.log(array[0]); // prints 4
console.log(array[3]); // prints 1

array.forEach((val, idx) => {
    console.log(`${idx} = ${val}`);
});
/* prints:
    3 = 1
    4 = 2
    5 = 3
    0 = 4
    1 = 5
    2 = 6
 */

console.log(array.length); // prints 6

Wait so how tf do I iterate?

The easiest way to iterate over the array is to use the SWArray.keys() method as a lookup table to map the sequential indices of the loop to the correct indices for the array.

let arr = new SWArray(...);
let idx = arr.keys();
for(let i = 0; i < arr.length; i++) {
    let val = arr[idx[i]];
    ...
}

Alternatively, you can just convert it to a regular array and iterate over that instead, using either SWArray.toNormalArray() or SWArray.toOrderedArray().

let arr = new SWArray(...);
let iter = arr.toNormalArray();
for(let i = 0; i < arr.length; i++) {
    let val = iter[i];
    ...
}

Reference

The SWArray class implements some of the functions which are supported by native Javascript arrays, with the exception of all indices being in the format specified above.

For shared functions, please see the documentation for the regular Javascript Array methods, since their arguments and return values are the same.

PropertiesTypeUsage
lengthnumberSee Array.prototype.length
MethodReturn TypeUsage
push()numberSee Array.prototype.push
pop()any or undefinedSee Array.prototype.pop
forEach()undefinedSee Array.prototype.forEach
indexOf()numberSee Array.prototype.indexOf
includes()booleanSee Array.prototype.includes
toNormalArray()arrayReturns the values as a regular Javascript array with start-to-end ordering.
toOrderedArray(boolean: removeGaps)arrayReturns the values as a regular Javascript array with SWArray ordering. If removeGaps is true, then any gaps left by the reordering are removed.
toString()stringSee Array.prototype.toString
keys()arraySee Array.prototype.keys
map()SWArraySee Array.prototype.map

Planned Features

  • Implement remaining native array functions (full list here).
  • More prequel memes (which are surprisingly lacking).
  • Make it iterable so using it with loops are more pleasant.