Skip to content

Commit 4b14ea2

Browse files
BendingBendersindresorhus
authored andcommitted
Require Node.js 8, add TypeScript definition (#3)
1 parent 16e9f28 commit 4b14ea2

File tree

12 files changed

+80
-71
lines changed

12 files changed

+80
-71
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ charset = utf-8
77
trim_trailing_whitespace = true
88
insert_final_newline = true
99

10-
[{package.json,*.yml}]
10+
[*.yml]
1111
indent_style = space
1212
indent_size = 2

.gitattributes

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
* text=auto
2-
*.js text eol=lf
1+
* text=auto eol=lf

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
yarn.lock

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
language: node_js
22
node_js:
3-
- '6'
4-
- '4'
3+
- '10'
4+
- '8'

index.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
Generate a [cryptographically strong](https://en.m.wikipedia.org/wiki/Strong_cryptography) random string.
3+
4+
@param length - Length of the returned string.
5+
@returns A [`hex`](https://en.wikipedia.org/wiki/Hexadecimal) string.
6+
7+
@example
8+
```
9+
import cryptoRandomString = require('crypto-random-string');
10+
11+
cryptoRandomString(10);
12+
//=> '2cf05d94db'
13+
```
14+
*/
15+
declare function cryptoRandomString(length: number): string;
16+
17+
export = cryptoRandomString;

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict';
22
const crypto = require('crypto');
33

4-
module.exports = len => {
5-
if (!Number.isFinite(len)) {
4+
module.exports = length => {
5+
if (!Number.isFinite(length)) {
66
throw new TypeError('Expected a finite number');
77
}
88

9-
return crypto.randomBytes(Math.ceil(len / 2)).toString('hex').slice(0, len);
9+
return crypto.randomBytes(Math.ceil(length / 2)).toString('hex').slice(0, length);
1010
};

index.test-d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {expectType} from 'tsd';
2+
import generate = require('.');
3+
4+
expectType<string>(generate(10));

license

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
1-
The MIT License (MIT)
1+
MIT License
22

33
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
44

5-
Permission is hereby granted, free of charge, to any person obtaining a copy
6-
of this software and associated documentation files (the "Software"), to deal
7-
in the Software without restriction, including without limitation the rights
8-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
copies of the Software, and to permit persons to whom the Software is
10-
furnished to do so, subject to the following conditions:
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
116

12-
The above copyright notice and this permission notice shall be included in
13-
all copies or substantial portions of the Software.
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
148

15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21-
THE SOFTWARE.
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package.json

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,42 @@
11
{
2-
"name": "crypto-random-string",
3-
"version": "1.0.0",
4-
"description": "Generate a cryptographically strong random string",
5-
"license": "MIT",
6-
"repository": "sindresorhus/crypto-random-string",
7-
"author": {
8-
"name": "Sindre Sorhus",
9-
"email": "sindresorhus@gmail.com",
10-
"url": "sindresorhus.com"
11-
},
12-
"engines": {
13-
"node": ">=4"
14-
},
15-
"scripts": {
16-
"test": "xo && ava"
17-
},
18-
"files": [
19-
"index.js"
20-
],
21-
"keywords": [
22-
"random",
23-
"string",
24-
"str",
25-
"rand",
26-
"text",
27-
"id",
28-
"identifier",
29-
"slug",
30-
"salt",
31-
"crypto",
32-
"strong",
33-
"secure",
34-
"hex"
35-
],
36-
"devDependencies": {
37-
"ava": "*",
38-
"xo": "*"
39-
},
40-
"xo": {
41-
"esnext": true
42-
}
2+
"name": "crypto-random-string",
3+
"version": "1.0.0",
4+
"description": "Generate a cryptographically strong random string",
5+
"license": "MIT",
6+
"repository": "sindresorhus/crypto-random-string",
7+
"author": {
8+
"name": "Sindre Sorhus",
9+
"email": "sindresorhus@gmail.com",
10+
"url": "sindresorhus.com"
11+
},
12+
"engines": {
13+
"node": ">=8"
14+
},
15+
"scripts": {
16+
"test": "xo && ava && tsd"
17+
},
18+
"files": [
19+
"index.js",
20+
"index.d.ts"
21+
],
22+
"keywords": [
23+
"random",
24+
"string",
25+
"str",
26+
"rand",
27+
"text",
28+
"id",
29+
"identifier",
30+
"slug",
31+
"salt",
32+
"crypto",
33+
"strong",
34+
"secure",
35+
"hex"
36+
],
37+
"devDependencies": {
38+
"ava": "^1.4.1",
39+
"tsd": "^0.7.2",
40+
"xo": "^0.24.0"
41+
}
4342
}

0 commit comments

Comments
 (0)