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:
3 | 4 | 5 | 0 | 1 | 2 | 6 | 7 | 8 | ... |
---|
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.
Properties | Type | Usage |
---|---|---|
length |
number |
See Array.prototype.length |
Method | Return Type | Usage |
---|---|---|
push() |
number |
See Array.prototype.push |
pop() |
any or undefined |
See Array.prototype.pop |
forEach() |
undefined |
See Array.prototype.forEach |
indexOf() |
number |
See Array.prototype.indexOf |
includes() |
boolean |
See Array.prototype.includes |
toNormalArray() |
array |
Returns the values as a regular Javascript array with start-to-end ordering. |
toOrderedArray(boolean: removeGaps) |
array |
Returns the values as a regular Javascript array with SWArray ordering. If removeGaps is true, then any gaps left by the reordering are removed. |
toString() |
string |
See Array.prototype.toString |
keys() |
array |
See Array.prototype.keys |
map() |
SWArray |
See 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.