Skip to content

Add TypeScript definition #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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