Skip to content

Commit a4de6c0

Browse files
committed
feat: Add outOfRange option
1 parent 5e02fe5 commit a4de6c0

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const items = new ArrayWithDefault({
2626
console.log(items[1]); // 0
2727
```
2828

29-
The primary use case for this class is when an array is an optional argument for a function, but using an empty array won't work.
29+
The primary use case for this class is when an array is an optional argument for a function or when an array may have holes.
3030

3131
## Usage
3232

@@ -81,6 +81,7 @@ After importing, create a new instance of `ArrayWithDefault`. The constructor ex
8181
* `default` **(required)** - the default value to return for the missing items.
8282
* `elements` - an optional iterable object used to populate the array.
8383
* `length` - an optional value to set the array's `length` property to.
84+
* `outOfRange` - an optional value that, when set to `true`, indicates that numeric indices after the end of the array should also return the default value.
8485

8586
Here are some examples:
8687

@@ -115,6 +116,19 @@ console.log(emptyItems[4]); // 0
115116
// items past the end still return undefined
116117
console.log(emptyItems[5]); // undefined
117118

119+
const numbers = new ArrayWithDefault({
120+
default: 0,
121+
elements: [1, 2, 3],
122+
outOfRange: true
123+
});
124+
125+
// all elements return 0
126+
console.log(emptyItems[0]); // 1
127+
console.log(emptyItems[1]); // 2
128+
console.log(emptyItems[2]); // 3
129+
console.log(emptyItems[3]); // 0
130+
console.log(emptyItems[4]); // 0
131+
console.log(emptyItems[5]); // 0
118132
```
119133

120134
## Developer Setup

src/array-with-default.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export class ArrayWithDefault extends Array {
1616
* @param {Array} [options.elements] The array elements to prepopulate into
1717
* the array.
1818
* @param {number} [options.length] The number of elements in the array.
19+
* @param {boolean} [options.outOfRange=false] When true, the default value
20+
* with also be returned for indices after the last element.
1921
*/
2022
constructor(options = {}) {
2123
super();
@@ -55,7 +57,7 @@ export class ArrayWithDefault extends Array {
5557
*/
5658
if (
5759
Number.isInteger(index) &&
58-
index < target.length &&
60+
(options.outOfRange || index < target.length) &&
5961
value === undefined
6062
) {
6163
return options.default;

tests/array-with-default.test.js

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,44 @@ describe("ArrayWithDefault", () => {
143143
expect(itemsWithDefault).to.deep.equal([0, 0, 0, 0, 0]);
144144
});
145145

146-
it("should return default when element is missing", () => {
146+
it("should set length when elements are passed", () => {
147147

148148
const itemsWithDefault = new ArrayWithDefault({
149+
elements: [1, 2, 3],
149150
length: 5,
150151
default: 0
151152
});
152153

153-
expect(itemsWithDefault).to.deep.equal([0, 0, 0, 0, 0]);
154+
expect(itemsWithDefault.length).to.equal(5);
155+
expect(itemsWithDefault).to.deep.equal([1, 2, 3, 0, 0]);
156+
});
157+
});
158+
159+
describe("outOfRange", () => {
160+
161+
it("should return default for elements after the last element", () => {
162+
163+
const itemsWithDefault = new ArrayWithDefault({
164+
default: 0,
165+
outOfRange: true
166+
});
167+
168+
expect(itemsWithDefault[0]).to.equal(0);
169+
expect(itemsWithDefault[1]).to.equal(0);
170+
});
171+
172+
it("should return default for elements after the last element when length is set", () => {
173+
174+
const itemsWithDefault = new ArrayWithDefault({
175+
default: 0,
176+
length: 5,
177+
outOfRange: true
178+
});
179+
180+
expect(itemsWithDefault[0]).to.equal(0);
181+
expect(itemsWithDefault[5]).to.equal(0);
154182
});
183+
155184
});
156185

157186
describe("concat()", () => {

0 commit comments

Comments
 (0)