Skip to content

Commit 1b5f844

Browse files
committed
Require Node.js 18
1 parent 8d08e8c commit 1b5f844

File tree

7 files changed

+32
-18
lines changed

7 files changed

+32
-18
lines changed

.github/workflows/main.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13-
- 14
14-
- 12
13+
- 20
14+
- 18
1515
steps:
16-
- uses: actions/checkout@v2
17-
- uses: actions/setup-node@v2
16+
- uses: actions/checkout@v4
17+
- uses: actions/setup-node@v4
1818
with:
1919
node-version: ${{ matrix.node-version }}
2020
- run: npm install

index.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
type EmptyArray = readonly never[] & {length: 0};
2+
type NonEmptyArray<T> = readonly [T, ...T[]] | readonly [...T[], T] | readonly [T, ...T[], T];
3+
14
/**
25
Get consecutively unique elements from an array.
36
@@ -13,4 +16,6 @@ console.log(random(), random(), random(), random());
1316
//=> 4 2 1 4
1417
```
1518
*/
16-
export default function uniqueRandomArray<T>(array: readonly T[]): () => T;
19+
export default function uniqueRandomArray<T>(array: EmptyArray): () => undefined;
20+
export default function uniqueRandomArray<T>(array: NonEmptyArray<T>): () => T;
21+
export default function uniqueRandomArray<T>(array: readonly T[]): () => T | undefined;

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import uniqueRandom from 'unique-random';
1+
import {consecutiveUniqueRandom} from 'unique-random';
22

33
export default function uniqueRandomArray(array) {
4-
const random = uniqueRandom(0, array.length - 1);
4+
const random = consecutiveUniqueRandom(0, array.length - 1);
55
return () => array[random()];
66
}

index.test-d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {expectType} from 'tsd';
22
import uniqueRandomArray from './index.js';
33

4-
expectType<() => number>(uniqueRandomArray([1, 2, 3, 4]));
5-
expectType<() => string | number>(uniqueRandomArray(['1', 2, 3, 4]));
4+
expectType<number>(uniqueRandomArray([1, 2, 3, 4])());
5+
expectType<string | number>(uniqueRandomArray(['1', 2, 3, 4])());
6+
expectType<undefined>(uniqueRandomArray([])());

package.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111
"url": "https://sindresorhus.com"
1212
},
1313
"type": "module",
14-
"exports": "./index.js",
14+
"exports": {
15+
"types": "./index.d.ts",
16+
"default": "./index.js"
17+
},
18+
"sideEffects": false,
1519
"engines": {
16-
"node": ">=12"
20+
"node": ">=18"
1721
},
1822
"scripts": {
1923
"test": "xo && ava && tsd"
@@ -34,11 +38,11 @@
3438
"element"
3539
],
3640
"dependencies": {
37-
"unique-random": "^3.0.0"
41+
"unique-random": "^4.0.0"
3842
},
3943
"devDependencies": {
40-
"ava": "^3.15.0",
41-
"tsd": "^0.14.0",
42-
"xo": "^0.38.2"
44+
"ava": "^6.1.3",
45+
"tsd": "^0.31.0",
46+
"xo": "^0.58.0"
4347
}
4448
}

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Useful for things like slideshows where you don't want to have the same slide tw
66

77
## Install
88

9-
```
10-
$ npm install unique-random-array
9+
```sh
10+
npm install unique-random-array
1111
```
1212

1313
## Usage
@@ -25,7 +25,7 @@ console.log(random(), random(), random(), random());
2525

2626
### uniqueRandomArray(array)
2727

28-
Returns a function, that when called, will return a random element that's never the same as the previous.
28+
Returns a function, that when called, will return a random element that's never the same as the previous, or `undefined` if the array is empty.
2929

3030
#### array
3131

test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ test('main', t => {
1313
previous = current;
1414
}
1515
});
16+
17+
test('empty array', t => {
18+
t.is(uniqueRandomArray([])(), undefined);
19+
});

0 commit comments

Comments
 (0)