| 1 | # hoopy
|
|---|
| 2 |
|
|---|
| 3 | [](https://gitlab.com/philbooth/hoopy/pipelines)
|
|---|
| 4 | [](https://www.npmjs.com/package/hoopy)
|
|---|
| 5 | [](https://www.npmjs.com/package/hoopy)
|
|---|
| 6 | [](https://opensource.org/licenses/MIT)
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 | Like an array, but rounder.
|
|---|
| 10 |
|
|---|
| 11 | * [Huh?](#huh)
|
|---|
| 12 | * [What's it useful for?](#whats-it-useful-for)
|
|---|
| 13 | * [How do I install it?](#how-do-i-install-it)
|
|---|
| 14 | * [How do I use it?](#how-do-i-use-it)
|
|---|
| 15 | * [Loading the library](#loading-the-library)
|
|---|
| 16 | * [Creating arrays](#creating-arrays)
|
|---|
| 17 | * [Accessing array items](#accessing-array-items)
|
|---|
| 18 | * [Growing the array](#growing-the-array)
|
|---|
| 19 | * [Is there a change log?](#is-there-a-change-log)
|
|---|
| 20 | * [How do I set up the dev environment?](#how-do-i-set-up-the-dev-environment)
|
|---|
| 21 | * [What license is it released under?](#what-license-is-it-released-under)
|
|---|
| 22 |
|
|---|
| 23 | ## Huh?
|
|---|
| 24 |
|
|---|
| 25 | Hoopy is a circular array
|
|---|
| 26 | data type.
|
|---|
| 27 | It extends `Array`
|
|---|
| 28 | so that out-of-bounds indices
|
|---|
| 29 | wrap back round
|
|---|
| 30 | to the start of the array
|
|---|
| 31 | (or if they're negative indices,
|
|---|
| 32 | they wrap back round
|
|---|
| 33 | to the end of the array).
|
|---|
| 34 |
|
|---|
| 35 | ## What's it useful for?
|
|---|
| 36 |
|
|---|
| 37 | If you want a fixed-length buffer
|
|---|
| 38 | for streamed I/O,
|
|---|
| 39 | Hoopy can do that for you.
|
|---|
| 40 |
|
|---|
| 41 | ## How do I install it?
|
|---|
| 42 |
|
|---|
| 43 | Via `npm`:
|
|---|
| 44 |
|
|---|
| 45 | ```
|
|---|
| 46 | npm i hoopy --save
|
|---|
| 47 | ```
|
|---|
| 48 |
|
|---|
| 49 | Or if you just want the git repo:
|
|---|
| 50 |
|
|---|
| 51 | ```
|
|---|
| 52 | git clone git@gitlab.com:philbooth/hoopy.git
|
|---|
| 53 | ```
|
|---|
| 54 |
|
|---|
| 55 | ## How do I use it?
|
|---|
| 56 |
|
|---|
| 57 | ### Loading the library
|
|---|
| 58 |
|
|---|
| 59 | ```js
|
|---|
| 60 | const Hoopy = require('hoopy');
|
|---|
| 61 | ```
|
|---|
| 62 |
|
|---|
| 63 | ### Creating arrays
|
|---|
| 64 |
|
|---|
| 65 | ```js
|
|---|
| 66 | const hoopy = new Hoopy(10);
|
|---|
| 67 | assert(Array.isArray(hoopy));
|
|---|
| 68 | ```
|
|---|
| 69 |
|
|---|
| 70 | You must pass
|
|---|
| 71 | a `size` argument
|
|---|
| 72 | to the `Hoopy` constructor,
|
|---|
| 73 | otherwise it will throw.
|
|---|
| 74 |
|
|---|
| 75 | ### Accessing array items
|
|---|
| 76 |
|
|---|
| 77 | ```js
|
|---|
| 78 | for (let i = 0; i < hoopy.length; ++i) {
|
|---|
| 79 | hoopy[i] = i;
|
|---|
| 80 | console.log(hoopy[i]);
|
|---|
| 81 | }
|
|---|
| 82 | ```
|
|---|
| 83 |
|
|---|
| 84 | You can read and write array items
|
|---|
| 85 | using square brackets for indexing
|
|---|
| 86 | as you would with a normal array.
|
|---|
| 87 | However, if you write to
|
|---|
| 88 | an out-of-bounds index,
|
|---|
| 89 | it will not increase
|
|---|
| 90 | the length of the array.
|
|---|
| 91 | Instead the index is applied
|
|---|
| 92 | modulo the array length,
|
|---|
| 93 | wrapping back round to the start.
|
|---|
| 94 | Negative indices work in reverse,
|
|---|
| 95 | wrapping back round to the end
|
|---|
| 96 | of the array.
|
|---|
| 97 |
|
|---|
| 98 | The methods
|
|---|
| 99 | `push`, `pop`, `shift` and `unshift`
|
|---|
| 100 | will throw if called.
|
|---|
| 101 | Future versions of the library
|
|---|
| 102 | may implement sane behaviour
|
|---|
| 103 | for them.
|
|---|
| 104 | All of the other `Array` methods
|
|---|
| 105 | work normally.
|
|---|
| 106 |
|
|---|
| 107 | ### Growing the array
|
|---|
| 108 |
|
|---|
| 109 | ```js
|
|---|
| 110 | hoopy.grow(50);
|
|---|
| 111 | ```
|
|---|
| 112 |
|
|---|
| 113 | The `grow` method
|
|---|
| 114 | adds items to the array.
|
|---|
| 115 | It takes one argument,
|
|---|
| 116 | which is the number
|
|---|
| 117 | of items to grow the array by.
|
|---|
| 118 | The new length of the array
|
|---|
| 119 | will be the old length
|
|---|
| 120 | plus the number you pass to `grow`.
|
|---|
| 121 |
|
|---|
| 122 | If the current state of the array
|
|---|
| 123 | includes overflowed indices,
|
|---|
| 124 | `grow` will take care
|
|---|
| 125 | to move those items
|
|---|
| 126 | in to the freshly-created
|
|---|
| 127 | available space,
|
|---|
| 128 | so that the correct order is maintained
|
|---|
| 129 | for your data.
|
|---|
| 130 |
|
|---|
| 131 | The caller is responsible
|
|---|
| 132 | for ensuring they don't overwrite
|
|---|
| 133 | unprocessed items.
|
|---|
| 134 | If you need to increase
|
|---|
| 135 | the size of the array,
|
|---|
| 136 | you must call `grow`.
|
|---|
| 137 |
|
|---|
| 138 | ## Is there a change log?
|
|---|
| 139 |
|
|---|
| 140 | [Yes](CHANGELOG.md).
|
|---|
| 141 |
|
|---|
| 142 | ## How do I set up the dev environment?
|
|---|
| 143 |
|
|---|
| 144 | To install the dependencies:
|
|---|
| 145 |
|
|---|
| 146 | ```
|
|---|
| 147 | npm i
|
|---|
| 148 | ```
|
|---|
| 149 |
|
|---|
| 150 | To run the tests:
|
|---|
| 151 |
|
|---|
| 152 | ```
|
|---|
| 153 | npm t
|
|---|
| 154 | ```
|
|---|
| 155 |
|
|---|
| 156 | To lint the code:
|
|---|
| 157 |
|
|---|
| 158 | ```
|
|---|
| 159 | npm run lint
|
|---|
| 160 | ```
|
|---|
| 161 |
|
|---|
| 162 | ## What license is it released under?
|
|---|
| 163 |
|
|---|
| 164 | [MIT](LICENSE).
|
|---|
| 165 |
|
|---|