[hasha] introduce typings (#18284)

This commit is contained in:
Dimitri Benin
2017-07-21 18:50:03 +02:00
committed by Wesley Wigham
parent 379d9ca112
commit 0dd0b27c90
4 changed files with 80 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
import hasha = require('hasha');
let str: string | null;
let buf: Buffer | null;
str = hasha('unicorn');
str = hasha('unicorn', {algorithm: 'md5'});
str = hasha('unicorn', {algorithm: 'md5', encoding: 'latin1'});
buf = hasha('unicorn', {algorithm: 'md5', encoding: 'buffer'});
process.stdin.pipe(hasha.stream()).pipe(process.stdout);
hasha.fromStream(process.stdin).then(hash => str = hash);
hasha.fromStream(process.stdin, {encoding: 'hex'}).then(hash => str = hash);
hasha.fromStream(process.stdin, {encoding: 'buffer'}).then(hash => buf = hash);
hasha.fromFile('unicorn.png').then(hash => str = hash);
hasha.fromFile('unicorn.png', {encoding: 'base64'}).then(hash => str = hash);
hasha.fromFile('unicorn.png', {encoding: 'buffer'}).then(hash => buf = hash);

38
types/hasha/index.d.ts vendored Normal file
View File

@@ -0,0 +1,38 @@
// Type definitions for hasha 3.0
// Project: https://github.com/sindresorhus/hasha
// Definitions by: BendingBender <https://github.com/BendgingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node"/>
import * as crypto from "crypto";
export = hasha;
declare function hasha(input: hasha.HashaInput): string;
declare function hasha<E extends hasha.ToStringEncoding>(input: hasha.HashaInput, options: hasha.HashaOptions<E>): string;
declare function hasha<E extends 'buffer'>(input: hasha.HashaInput, options: hasha.HashaOptions<E>): Buffer;
declare namespace hasha {
type HashaInput = string | string[] | Buffer | Buffer[];
type ToStringEncoding = 'hex' | 'base64' | 'latin1' | 'binary' | undefined;
type HashaEncoding = ToStringEncoding | 'buffer';
interface HashaOptions<E extends HashaEncoding> {
encoding?: E;
algorithm?: string;
}
function stream(options?: HashaOptions<HashaEncoding>): crypto.Hash;
function fromStream(stream: NodeJS.ReadableStream): Promise<string | null>;
function fromStream<E extends ToStringEncoding>(stream: NodeJS.ReadableStream, options?: HashaOptions<E>): Promise<string | null>;
function fromStream<E extends 'buffer'>(stream: NodeJS.ReadableStream, options?: HashaOptions<E>): Promise<Buffer | null>;
function fromFile(filePath: string): Promise<string | null>;
function fromFile<E extends ToStringEncoding>(filePath: string, options: HashaOptions<E>): Promise<string | null>;
function fromFile<E extends 'buffer'>(filePath: string, options: HashaOptions<E>): Promise<Buffer | null>;
function fromFileSync(filePath: string): string;
function fromFileSync<E extends ToStringEncoding>(filePath: string, options: HashaOptions<E>): string;
function fromFileSync<E extends 'buffer'>(filePath: string, options: HashaOptions<E>): Buffer;
}

22
types/hasha/tsconfig.json Normal file
View File

@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"hasha-tests.ts"
]
}

1
types/hasha/tslint.json Normal file
View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }