🤖 Merge PR #59635 Add types for the 'fnv-plus' package by @mihnea-s

This commit is contained in:
mihnea-s
2022-04-12 12:20:04 +03:00
committed by GitHub
parent c4d1ee5775
commit df0543b120
4 changed files with 157 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
import * as fnv from 'fnv-plus';
const astring = 'hello world';
{
const ahash52 = fnv.hash(astring);
// $ExpectType string
ahash52.hex();
// $ExpectType string
ahash52.str();
// $ExpectType string
ahash52.dec();
}
{
fnv.seed(astring);
const ahash64 = fnv.hash(astring, 64);
// $ExpectType string
ahash64.hex();
// $ExpectType string
ahash64.str();
// $ExpectType string
ahash64.dec();
}
{
// $ExpectType number
fnv.fast1a32(astring);
// $ExpectType number
fnv.fast1a32utf(astring);
// $ExpectType string
fnv.fast1a32hex(astring);
// $ExpectType string
fnv.fast1a32hexutf(astring);
// $ExpectType number
fnv.fast1a52(astring);
// $ExpectType number
fnv.fast1a52utf(astring);
// $ExpectType string
fnv.fast1a52hex(astring);
// $ExpectType string
fnv.fast1a52hexutf(astring);
// $ExpectType number
fnv.fast1a64(astring);
// $ExpectType number
fnv.fast1a64utf(astring);
// $ExpectType string
fnv.fast1a64hex(astring);
// $ExpectType string
fnv.fast1a64hexutf(astring);
}

74
types/fnv-plus/index.d.ts vendored Normal file
View File

@@ -0,0 +1,74 @@
// Type definitions for fnv-plus 1.3
// Project: https://github.com/tjwebb/fnv-plus
// Definitions by: Mihnea Stoian <https://github.com/mihnea-s>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export as namespace fnv;
export interface FnvHash {
/** Returns the hashed value as an ascii string */
str: () => string;
/** Returns the hashed value as an hexadecimal string */
hex: () => string;
/** Returns the hashed value as an decimal string */
dec: () => string;
}
/**
* Hash a string using the given bit length.
*
* @default bitlength 52
*/
export function hash(value: string, bitlength?: number): FnvHash;
/**
* Seed the algorithm to produce different values. Hashing the same value
* with different seeds will very likely result in different results. To the
* extent your seed can be random, it can serve as a source of randomness,
* but nonetheless is not a replacement for a crypgographic PRG (pseudo-random
* generator). By default the seed is `chongo <Landon Curt Noll> /\>./\\`.
*/
export function seed(seed: string): void;
/**
* Controls UTF-8 awareness of hash functions. By default this is `false`.
*/
export function useUTF8(enable: boolean): void;
/** Calculate FNV-1a 32bit hash. */
export function fast1a32(value: string): number;
/** Calculate FNV-1a 32bit hash. */
export function fast1a32hex(value: string): string;
/** Calculate FNV-1a 32bit hash, handles UTF-8 strings. */
export function fast1a32utf(value: string): number;
/** Calculate FNV-1a 32bit hash, handles UTF-8 strings. */
export function fast1a32hexutf(value: string): string;
/** Calculate FNV-1a 52bit hash. */
export function fast1a52(value: string): number;
/** Calculate FNV-1a 52bit hash. */
export function fast1a52hex(value: string): string;
/** Calculate FNV-1a 52bit hash, handles UTF-8 strings. */
export function fast1a52utf(value: string): number;
/** Calculate FNV-1a 52bit hash, handles UTF-8 strings. */
export function fast1a52hexutf(value: string): string;
/** Calculate FNV-1a 64bit hash. */
export function fast1a64(value: string): number;
/** Calculate FNV-1a 64bit hash. */
export function fast1a64hex(value: string): string;
/** Calculate FNV-1a 64bit hash, handles UTF-8 strings. */
export function fast1a64utf(value: string): number;
/** Calculate FNV-1a 64bit hash, handles UTF-8 strings. */
export function fast1a64hexutf(value: string): string;

View File

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

View File

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