Skip to content

Commit

Permalink
Add TypeScript definition (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Apr 9, 2019
1 parent b7a701c commit 950c2d3
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 5 deletions.
75 changes: 75 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
export interface OptionsHexOutput {
outputFormat?: 'hex';
}

export interface OptionBufferOutput {
outputFormat: 'buffer';
}

/**
[SHA-1 is insecure](https://stackoverflow.com/a/38045085/64949) and should not be used for anything sensitive.
_Note that even though it returns a promise, [in Node.js, the operation is synchronous 💩](https://github.com/nodejs/node/issues/678)._
@returns The hex-encoded hash.
*/
export function sha1(
input: string | ArrayBuffer | ArrayBufferView,
options?: OptionsHexOutput
): Promise<string>;
export function sha1(
input: string | ArrayBuffer | ArrayBufferView,
options: OptionBufferOutput
): Promise<ArrayBuffer>;

/**
_Note that even though it returns a promise, [in Node.js, the operation is synchronous 💩](https://github.com/nodejs/node/issues/678)._
@returns The hex-encoded hash.
@example
```
import {sha256} from 'crypto-hash';
(async () => {
console.log(await sha256('🦄'));
//=> '5df82936cbf0864be4b7ba801bee392457fde9e4'
})();
```
*/
export function sha256(
input: string | ArrayBuffer | ArrayBufferView,
options?: OptionsHexOutput
): Promise<string>;
export function sha256(
input: string | ArrayBuffer | ArrayBufferView,
options: OptionBufferOutput
): Promise<ArrayBuffer>;

/**
_Note that even though it returns a promise, [in Node.js, the operation is synchronous 💩](https://github.com/nodejs/node/issues/678)._
@returns The hex-encoded hash.
*/
export function sha384(
input: string | ArrayBuffer | ArrayBufferView,
options?: OptionsHexOutput
): Promise<string>;
export function sha384(
input: string | ArrayBuffer | ArrayBufferView,
options: OptionBufferOutput
): Promise<ArrayBuffer>;

/**
_Note that even though it returns a promise, [in Node.js, the operation is synchronous 💩](https://github.com/nodejs/node/issues/678)._
@returns The hex-encoded hash.
*/
export function sha512(
input: string | ArrayBuffer | ArrayBufferView,
options?: OptionsHexOutput
): Promise<string>;
export function sha512(
input: string | ArrayBuffer | ArrayBufferView,
options: OptionBufferOutput
): Promise<ArrayBuffer>;
15 changes: 15 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {expectType} from 'tsd';
import {sha1, sha256, sha384, sha512} from '.';

expectType<Promise<string>>(sha1('🦄'));
expectType<Promise<ArrayBuffer>>(sha1('🦄', {outputFormat: 'buffer'}));
expectType<Promise<string>>(sha1('🦄', {outputFormat: 'hex'}));
expectType<Promise<string>>(sha256('🦄'));
expectType<Promise<ArrayBuffer>>(sha256('🦄', {outputFormat: 'buffer'}));
expectType<Promise<string>>(sha256('🦄', {outputFormat: 'hex'}));
expectType<Promise<string>>(sha384('🦄'));
expectType<Promise<ArrayBuffer>>(sha384('🦄', {outputFormat: 'buffer'}));
expectType<Promise<string>>(sha384('🦄', {outputFormat: 'hex'}));
expectType<Promise<string>>(sha512('🦄'));
expectType<Promise<ArrayBuffer>>(sha512('🦄', {outputFormat: 'buffer'}));
expectType<Promise<string>>(sha512('🦄', {outputFormat: 'hex'}));
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"node": ">=8"
},
"scripts": {
"test": "xo && ava test.js && karmatic test-browser.js"
"test": "xo && ava test.js && karmatic test-browser.js && tsd"
},
"files": [
"index.js",
"index.d.ts",
"browser.js"
],
"keywords": [
Expand All @@ -33,11 +34,12 @@
"browser"
],
"devDependencies": {
"@sindresorhus/is": "^0.13.0",
"ava": "^0.25.0",
"@sindresorhus/is": "^0.15.0",
"ava": "^1.4.1",
"hash.js": "^1.1.5",
"karmatic": "1.0.6",
"xo": "^0.23.0"
"karmatic": "1.0.7",
"tsd": "^0.7.2",
"xo": "^0.24.0"
},
"browser": "browser.js"
}

0 comments on commit 950c2d3

Please sign in to comment.