Skip to content

Commit befe222

Browse files
committed
add default callback support
1 parent 164c2a7 commit befe222

File tree

6 files changed

+131
-3669
lines changed

6 files changed

+131
-3669
lines changed

README.md

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,31 @@ If you find this useful, please consider supporting my work with a [donation](ht
66

77
## Description
88

9-
A class that represents an array where missing elements return a specified default value.
9+
A class that represents an array where missing elements return a specified default value. Wraps a Proxy for call interception.
1010

1111
The built-in JavaScript `Array` class automatically returns `undefined` when you attempt to access a missing element. Here's an example:
1212

1313
```js
14-
const items = [1, , 3];
14+
const items = [1, ,3];
1515
console.log(items[1]); // undefined
1616
```
1717

18-
In most cases that is okay, but in some cases you may want an alternate default value returned. The `ArrayWithDefault` class does that:
18+
In most cases that is okay, but in some cases you may want an alternative default value returned. The `ArrayWithDefault` class does that:
1919

2020
```js
21-
const items = new ArrayWithDefault({
22-
default: 0
23-
elements: [1, , 3]
21+
const items1 = new ArrayWithDefault({
22+
default: 0,
23+
elements: [1, ,3]
2424
});
2525

26-
console.log(items[1]); // 0
26+
console.log(items1[1]); // 0
27+
28+
const items2 = new ArrayWithDefault({
29+
default: (index) => ({ id: index }), // callback
30+
elements: [{ id: 0 }, ,{ id: 2 }]
31+
});
32+
33+
console.log(items2[1]); // { id: 1 }
2734
```
2835

2936
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.
@@ -34,7 +41,7 @@ The primary use case for this class is when an array is an optional argument for
3441

3542
Install using [npm][npm] or [yarn][yarn]:
3643

37-
```
44+
```bash
3845
npm install @humanwhocodes/array-with-default --save
3946

4047
# or
@@ -79,6 +86,7 @@ import { ArrayWithDefault } from "https://cdn.skypack.dev/@humanwhocodes/array-w
7986
After importing, create a new instance of `ArrayWithDefault`. The constructor expects one object argument with the following properties:
8087

8188
* `default` **(required)** - the default value to return for the missing items.
89+
* Note: can be a callback: `(index: number) => any`
8290
* `elements` - an optional iterable object used to populate the array.
8391
* `length` - an optional value to set the array's `length` property to.
8492
* `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.
@@ -116,19 +124,29 @@ console.log(emptyItems[4]); // 0
116124
// items past the end still return undefined
117125
console.log(emptyItems[5]); // undefined
118126

119-
const numbers = new ArrayWithDefault({
127+
const numberItems = new ArrayWithDefault({
120128
default: 0,
121129
elements: [1, 2, 3],
122130
outOfRange: true
123131
});
124132

125133
// 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
134+
console.log(numberItems[0]); // 1
135+
console.log(numberItems[1]); // 2
136+
console.log(numberItems[2]); // 3
137+
console.log(numberItems[3]); // 0
138+
console.log(numberItems[4]); // 0
139+
console.log(numberItems[5]); // 0
140+
141+
const objectItems = new ArrayWithDefault({
142+
default: (index: number) => ({}),
143+
elements: [{ id: 42 }],
144+
outOfRange: true
145+
});
146+
147+
// default returns an empty object
148+
console.log(objectItems[0]); // { id: 42 }
149+
console.log(objectItems[1]); // {}
132150
```
133151

134152
## Developer Setup

0 commit comments

Comments
 (0)