[sparse-bitfield] Add types

This commit is contained in:
Dimitri Benin 2018-12-30 00:45:40 +01:00
parent 9272fc1637
commit bc5ffa8aa7
4 changed files with 122 additions and 0 deletions

74
types/sparse-bitfield/index.d.ts vendored Normal file
View File

@ -0,0 +1,74 @@
// Type definitions for sparse-bitfield 3.0
// Project: https://github.com/mafintosh/sparse-bitfield
// Definitions by: BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
import { PagerInstance } from 'memory-pager';
export = BitField;
declare const BitField: BitField;
interface BitField {
(bufferOrOptions?: BitField.Options | Buffer): BitField.BitFieldInstance;
new (bufferOrOptions?: BitField.Options | Buffer): BitField.BitFieldInstance;
}
declare namespace BitField {
interface Options {
/**
* @default 0
*/
pageOffset?: number;
/**
* How big should the partial buffers be.
* @default 1024
*/
pageSize?: number;
/**
* A pre-configured Pager instance.
*/
pages?: PagerInstance;
/**
* An existing bitfield.
*/
buffer?: Buffer;
/**
* Track when pages are being updated in the pager.
* @default false
*/
trackUpdates?: boolean;
}
interface BitFieldInstance {
/**
* A `memory-pager` instance that is managing the underlying memory.
* If you set `trackUpdates` to `true` in the constructor you can use `.lastUpdate()` on this instance
* to get the last updated memory page.
*/
readonly pages: PagerInstance;
/**
* Get the value of a bit.
*/
get(index: number): boolean;
/**
* Get the value of a byte.
*/
getByte(index: number): number;
/**
* Set a bit to true or false.
*/
set(index: number, value: boolean): boolean;
/**
* Set a byte to a new value.
*/
setByte(index: number, byte: number): boolean;
/**
* Get a single buffer representing the entire bitfield.
*/
toBuffer(): Buffer;
}
}

View File

@ -0,0 +1,24 @@
import BitField = require('sparse-bitfield');
import Pager = require('memory-pager');
const bits = BitField();
BitField(new Buffer(1));
BitField({ pageOffset: 1 });
BitField({ pageSize: 1024 });
BitField({ pages: new Pager() });
BitField({ buffer: new Buffer(1) });
BitField({ trackUpdates: true });
new BitField();
new BitField(new Buffer(1));
new BitField({ pageOffset: 1 });
new BitField({ pageSize: 1024 });
new BitField({ pages: new Pager() });
new BitField({ buffer: new Buffer(1) });
new BitField({ trackUpdates: true });
bits.get(0); // $ExpectType boolean
bits.getByte(0); // $ExpectType number
bits.set(0, true); // $ExpectType boolean
bits.setByte(0, 1); // $ExpectType boolean
bits.toBuffer(); // $ExpectType Buffer
bits.pages; // $ExpectType PagerInstance

View File

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

View File

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