Skip to content

Commit 8870574

Browse files
committed
Add Typescript definition
1 parent 0004ef9 commit 8870574

File tree

3 files changed

+283
-0
lines changed

3 files changed

+283
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 2.0
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { IterableIterator } from '@stdlib/types/iter';
24+
import { Collection } from '@stdlib/types/object';
25+
26+
/**
27+
* Circular buffer.
28+
*/
29+
declare class CircularBuffer {
30+
/**
31+
* Circular buffer constructor.
32+
*
33+
* @param buffer - buffer size or an array-like object to use as the underlying buffer
34+
* @throws must provide either a valid buffer size or an array-like object
35+
* @returns circular buffer instance
36+
*
37+
* @example
38+
* var b = new CircularBuffer( 3 );
39+
*
40+
* // Fill the buffer...
41+
* var v = b.push( 'foo' );
42+
* // returns undefined
43+
*
44+
* v = b.push( 'bar' );
45+
* // returns undefined
46+
*
47+
* v = b.push( 'beep' );
48+
* // returns undefined
49+
*
50+
* // Add another value to the buffer and return the removed element:
51+
* v = b.push( 'boop' );
52+
* // returns 'foo'
53+
*/
54+
constructor( buffer: number | Collection );
55+
56+
/**
57+
* Clears the buffer.
58+
*
59+
* @returns circular buffer instance
60+
*
61+
* @example
62+
* var b = new CircularBuffer( 2 );
63+
*
64+
* // Add values to the buffer:
65+
* b.push( 'foo' );
66+
* b.push( 'bar' );
67+
* b.push( 'beep' );
68+
* b.push( 'boop' );
69+
*
70+
* // Get the number of elements currently in the buffer:
71+
* var n = b.count;
72+
* // returns 2
73+
*
74+
* // Clear the buffer:
75+
* b.clear();
76+
*
77+
* // Get the number of buffer values:
78+
* n = b.count;
79+
* // returns 0
80+
*/
81+
clear(): CircularBuffer;
82+
83+
/**
84+
* Number of elements currently in the buffer.
85+
*
86+
* @example
87+
* var b = new CircularBuffer( 4 );
88+
*
89+
* // Get the value count:
90+
* var n = b.count;
91+
* // returns 0
92+
*
93+
* // Add values to the buffer:
94+
* b.push( 'foo' );
95+
* b.push( 'bar' );
96+
*
97+
* // Get the value count:
98+
* n = b.count;
99+
* // returns 2
100+
*/
101+
readonly count: number;
102+
103+
/**
104+
* Boolean indicating whether a circular buffer is full.
105+
*
106+
* @example
107+
* var b = new CircularBuffer( 3 );
108+
*
109+
* // Determine if the buffer is full:
110+
* var bool = b.full;
111+
* // returns false
112+
*
113+
* // Add values to the buffer:
114+
* b.push( 'foo' );
115+
* b.push( 'bar' );
116+
* b.push( 'beep' );
117+
* b.push( 'boop' );
118+
*
119+
* // Determine if the buffer is full:
120+
* bool = b.full;
121+
* // returns true
122+
*/
123+
readonly full: boolean;
124+
125+
/**
126+
* Returns an iterator for iterating over a circular buffer.
127+
*
128+
* ## Notes
129+
*
130+
* - An iterator does not iterate over partially full buffers.
131+
*
132+
* @param niters - number of iterations
133+
* @throws must provide a nonnegative integer
134+
* @returns iterator
135+
*
136+
* @example
137+
* var b = new CircularBuffer( 3 );
138+
*
139+
* // Add values to the buffer:
140+
* b.push( 'foo' );
141+
* b.push( 'bar' );
142+
* b.push( 'beep' );
143+
* b.push( 'boop' );
144+
*
145+
* // Create an iterator:
146+
* var it = b.iterator( b.length );
147+
*
148+
* // Iterate over the buffer...
149+
* var v = it.next().value;
150+
* // returns 'bar'
151+
*
152+
* v = it.next().value;
153+
* // returns 'beep'
154+
*
155+
* v = it.next().value;
156+
* // returns 'boop'
157+
*
158+
* var bool = it.next().done;
159+
* // returns true
160+
*/
161+
iterator( niters?: number ): IterableIterator;
162+
163+
/**
164+
* Circular buffer length (capacity).
165+
*
166+
* @example
167+
* var b = new CircularBuffer( 4 );
168+
*
169+
* // Get the buffer capacity:
170+
* var len = b.length;
171+
* // returns 4
172+
*/
173+
readonly length: number;
174+
175+
/**
176+
* Adds a value to the circular buffer.
177+
*
178+
* @returns removed element or undefined
179+
*
180+
* @example
181+
* var b = new CircularBuffer( 3 );
182+
*
183+
* // Fill the buffer:
184+
* var v = b.push( 'foo' );
185+
* // returns undefined
186+
*
187+
* v = b.push( 'bar' );
188+
* // returns undefined
189+
*
190+
* v = b.push( 'beep' );
191+
* // returns undefined
192+
*
193+
* // Add another value to the buffer and return the removed element:
194+
* v = b.push( 'boop' );
195+
* // returns 'foo'
196+
*/
197+
push(): any;
198+
199+
/**
200+
* Returns an array of circular buffer values.
201+
*
202+
* @returns circular buffer values
203+
*
204+
* @example
205+
* var b = new CircularBuffer( 3 );
206+
*
207+
* // Add values to the buffer:
208+
* b.push( 'foo' );
209+
* b.push( 'bar' );
210+
* b.push( 'beep' );
211+
* b.push( 'boop' );
212+
*
213+
* // Get an array of buffer values:
214+
* var vals = b.toArray();
215+
* // returns [ 'bar', 'beep', 'boop' ]
216+
*/
217+
toArray(): Array<any>;
218+
219+
/**
220+
* Serializes a circular buffer as JSON.
221+
*
222+
* ## Notes
223+
*
224+
* - `JSON.stringify()` implicitly calls this method when stringifying a `CircularBuffer` instance.
225+
*
226+
* @returns serialized circular buffer
227+
*
228+
* @example
229+
* var b = new CircularBuffer( 3 );
230+
*
231+
* // Add values to the buffer:
232+
* b.push( 'foo' );
233+
* b.push( 'bar' );
234+
* b.push( 'beep' );
235+
* b.push( 'boop' );
236+
*
237+
* // Serialize to JSON:
238+
* var o = b.toJSON();
239+
* // returns { 'type': 'circular-buffer', 'length': 3, 'data': [ 'bar', 'beep', 'boop' ] }
240+
*/
241+
toJSON(): any;
242+
}
243+
244+
245+
// EXPORTS //
246+
247+
export = CircularBuffer;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/* tslint:disable:no-unused-expression */
20+
21+
import CircularBuffer = require( './index' );
22+
23+
24+
// TESTS //
25+
26+
// The function returns a circular buffer instance...
27+
{
28+
new CircularBuffer( 3 ); // $ExpectType CircularBuffer
29+
}
30+
31+
// The compiler throws an error if the constructor function is provided an invalid number of arguments...
32+
{
33+
new CircularBuffer(); // $ExpectError
34+
new CircularBuffer( 3, 4 ); // $ExpectError
35+
}

lib/node_modules/@stdlib/utils/circular-buffer/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"lib": "./lib",
2222
"test": "./test"
2323
},
24+
"types": "./docs/types",
2425
"scripts": {},
2526
"homepage": "https://github.com/stdlib-js/stdlib",
2627
"repository": {

0 commit comments

Comments
 (0)