Skip to content

Commit 73ae86d

Browse files
russaatrevnorris
authored andcommitted
Added TypeScript definitions
PR-URL: #28 Reviewed-by: Trevor Norris <[email protected]>
1 parent 967d85b commit 73ae86d

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

cbuffer.d.ts

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
2+
/*~ Note that ES6 modules cannot directly export class objects.
3+
*~ This file should be imported using the CommonJS-style:
4+
*~ import CBuffer = require('CBuffer');
5+
*~
6+
*~ Alternatively, if --allowSyntheticDefaultImports or
7+
*~ --esModuleInterop is turned on, this file can also be
8+
*~ imported as a default import:
9+
*~ import CBuffer from 'CBuffer';
10+
*~
11+
*~ Refer to the documentation to understand common
12+
*~ workarounds for this limitation of ES6 modules.
13+
*/
14+
15+
/*~ This module is a UMD module that exposes a global variable 'CBuffer' when
16+
*~ loaded outside a module loader environment.
17+
*/
18+
export as namespace CBuffer;
19+
20+
/*~ This declaration specifies that the class constructor function
21+
*~ is the exported object from the file
22+
*/
23+
export = CBuffer;
24+
25+
declare class CBuffer<T> {
26+
constructor(entry: T, ...entries: T[]);
27+
constructor(size: number);
28+
29+
/* fields */
30+
/** Gets or sets the length of the buffer */
31+
length: number;
32+
/** Gets or sets the capacity of the buffer */
33+
size: number;
34+
35+
/* hooks */
36+
/** overflow hook: is called when a data entry in the buffer is about to be overwritten */
37+
overflow: null | ((overwrittenEntry: T) => void);
38+
39+
/* internal data representation */
40+
/** internal data representation of the buffer */
41+
readonly data: ReadonlyArray<T>;
42+
/** start index for the data */
43+
readonly start: number;
44+
/** end index for the data */
45+
readonly end: number;
46+
47+
/* mutator methods */
48+
/** pop last item */
49+
pop(): T | undefined;
50+
/** push item to the end */
51+
push(...items: T[]): number;
52+
/** reverse order of the buffer */
53+
reverse(): this;
54+
/** rotate buffer to the left by cntr, or by 1 */
55+
rotateLeft(cntr?: number): this;
56+
/** rotate buffer to the right by cntr, or by 1 */
57+
rotateRight(cntr?: number): this;
58+
/** remove and return first item */
59+
shift(): T;
60+
/** sort items */
61+
sort(sortFunction?: (v1: T, v2: T) => number): this;
62+
/** add item to beginning of buffer */
63+
unshift(...items: T[]): number;
64+
65+
/* accessor methods */
66+
/** return index of first matched element */
67+
indexOf(searchElement: T, fromIndex?: number): number;
68+
/** return last index of the first match */
69+
lastIndexOf(searchElement: T, fromIndex?: number): number;
70+
/**
71+
* return the index an item would be inserted to if this
72+
* is a sorted circular buffer
73+
*/
74+
sortedIndex(value: T, comparitor?: (v1: T, v2: T) => number, thisArg?: any): number;
75+
76+
/* iteration methods */
77+
/** check every item in the array against a test */
78+
every(callbackfn: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean;
79+
/** loop through each item in buffer */
80+
// TODO: figure out how to emulate Array use better
81+
forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void;
82+
/** construct new CBuffer of same length, apply map function, and return new CBuffer */
83+
map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): CBuffer<U>;
84+
/** check items agains test until one returns true */
85+
some(callbackfn: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean;
86+
/** calculate the average value of a circular buffer */
87+
avg(): number;
88+
/** loop through each item in buffer and calculate sum */
89+
sum(): number;
90+
/** loop through each item in buffer and calculate median */
91+
median(): number;
92+
93+
/* utility methods */
94+
/**
95+
* reset pointers to buffer with zero items
96+
* note: this will not remove values in cbuffer, so if for security values
97+
* need to be overwritten, run `.fill(null).empty()`
98+
*/
99+
empty(): this;
100+
/** fill all places with passed value or function */
101+
fill(value: T | (() => T)): this; // NOTE orig. API Array.fill: fill(value: T, start?: number, end?: number): this;
102+
/** return first item in buffer */
103+
first(): T | undefined;
104+
/** return last item in buffer */
105+
last(): T | undefined;
106+
/** return specific index in buffer */
107+
get(index: number): T | undefined;
108+
isFull(): boolean;
109+
/** set value at specified index */
110+
set(idx: number, arg: T): T;
111+
/** return clean array of values */
112+
toArray(): T[];
113+
/** return a string based on the array */
114+
join(separator?: string): string;
115+
/** slice the buffer to an array */
116+
slice(start?: number, end?: number): T[];
117+
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "Circular Buffer JavaScript implementation",
55
"homepage": "https://github.com/trevnorris/cbuffer",
66
"main": "./cbuffer.js",
7+
"types": "cbuffer.d.ts",
78
"author": {
89
"name": "Trevor Norris",
910
"email": "[email protected]",

0 commit comments

Comments
 (0)