adds typings for the 'bloom-filter' package (#28838)

This commit is contained in:
Daniel Byrne
2018-09-14 10:18:51 -07:00
committed by Ryan Cavanaugh
parent 58451091ce
commit a3b7777878
4 changed files with 88 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
import BloomFilter = require('bloom-filter');
const numElements = 3;
const falsePositiveRate = 0.01;
const filter = BloomFilter.create(numElements, falsePositiveRate);
// elements
const a = Buffer.from('99108ad8ed9bb6274d3980bab5a85c048f0950c8', 'hex');
const c = Buffer.from('b5a2c786d9ef4658287ced5914b37a1b4aa32eee', 'hex');
// insert elements
// $ExpectType void
filter.insert(a);
// $ExpectType boolean
!filter.contains(c);
// reinstantiate from an object
const serialized = filter.toObject();
const woahFilter = new BloomFilter(serialized);
// initialize directly
const newFilter = new BloomFilter({
vData: Buffer.from('123', 'hex'), // the data of the filter
nHashFuncs: 3, // the number of hash functions to use
nTweak: 2147483649, // the seed used for the hash fuctions
nFlags: 0 // flags used to update the filter when matched
});

38
types/bloom-filter/index.d.ts vendored Normal file
View File

@@ -0,0 +1,38 @@
// Type definitions for bloom-filter 0.2
// Project: https://github.com/bitpay/bloom-filter
// Definitions by: Daniel Byrne <https://github.com/danwbyrne>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
declare class Filter {
constructor(options: Filter.FilterOptions);
static create(elements: number, falsePositiveRate: number, nTweak?: number, nFlags?: number): Filter;
toObject(): Filter.FilterOptions;
hash(nHashNum: number, vDataToHash: Buffer): number;
insert(data: Buffer): void;
contains(data: Buffer): boolean;
clear(): void;
inspect(): string;
BLOOM_UPDATE_NONE: number;
BLOOM_UPDATE_ALL: number;
BLOOM_UPDATE_P2PUBKEY_ONLY: number;
MAX_BLOOM_FILTER_SIZE: number;
MAX_HASH_FUNCS: number;
MIN_HASH_FUNCS: number;
LN2SQUARED: number;
LN2: number;
}
declare namespace Filter {
interface FilterOptions {
vData: Buffer;
nHashFuncs: number;
nTweak?: number;
nFlags?: number;
}
function MurmurHash3(seed: number, data: Buffer): number;
}
export = Filter;

View File

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

View File

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