Skip to content

Commit

Permalink
Add TypeScript definition (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 12, 2019
1 parent 8cee9d2 commit 35c640c
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 11 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: node_js
node_js:
- '10'
- '8'
- '6'
19 changes: 19 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
Get consecutively unique elements from an array.
@returns A function that when called will return a random element that's never the same as the previous.
@example
```
import uniqueRandomArray = require('unique-random-array');
const random = uniqueRandomArray([1, 2, 3, 4]);
console.log(random(), random(), random(), random());
//=> 4 2 1 4
```
*/
declare function uniqueRandomArray<ValueType>(
array: readonly ValueType[]
): () => ValueType;

export = uniqueRandomArray;
5 changes: 5 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {expectType} from 'tsd';
import uniqueRandomArray = require('.');

expectType<() => number>(uniqueRandomArray([1, 2, 3, 4]));
expectType<() => string | number>(uniqueRandomArray(['1', 2, 3, 4]));
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"unique",
Expand All @@ -36,7 +37,8 @@
"unique-random": "^2.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
}
}
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ console.log(random(), random(), random(), random());

## API

### uniqueRandomArray(input)
### uniqueRandomArray(array)

Returns a function that when called will return a random element that's never the same as the previous.

#### input
#### array

Type: `Array`
Type: `Array<unknown>`


## Related
Expand Down
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import test from 'ava';
import m from '.';
import uniqueRandomArray from '.';

test('main', t => {
const random = m([1, 2, 3, 4]);
const random = uniqueRandomArray([1, 2, 3, 4]);

let current;
let previous;
Expand Down

0 comments on commit 35c640c

Please sign in to comment.