Skip to content

Commit 5774172

Browse files
committed
Added documentation to lib
1 parent e4e50f8 commit 5774172

File tree

110 files changed

+1286
-642
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+1286
-642
lines changed

dist/lib/assert.js

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
33
const util_inspect_1 = require("./util-inspect");
44
//A number of useful assertion functions
55
//Used in tests and for validations in production
6+
/**
7+
* Throws an error if the given value is not an instance
8+
* of any of the provided constructors
9+
* @param instance The value in question
10+
* @param constructors A constructor or array of constructors to test against
11+
*/
612
function instanceOf(instance, constructors) {
713
if (!(constructors instanceof Array))
814
constructors = [constructors];
@@ -23,12 +29,25 @@ function instanceOf(instance, constructors) {
2329
.join(' or '));
2430
}
2531
}
32+
/**
33+
* Throws an error if the given value is not an integer
34+
* within the range of integers representable in JavaScript
35+
* @param instance The value in question
36+
*/
2637
function integer(instance) {
2738
instanceOf(instance, Number);
2839
if (!Number.isSafeInteger(instance)) {
2940
throw new RangeError(util_inspect_1.inspect(instance) + ' is not an integer');
3041
}
3142
}
43+
/**
44+
* Throws an error if a numeric value is not between
45+
* the given bounds
46+
* @param lower The lower bound (inclusive)
47+
* @param value The value in question
48+
* @param upper The upper bound (exclusive)
49+
* @param message An optional message to include in the error message
50+
*/
3251
function between(lower, value, upper, message) {
3352
if (value < lower || value >= upper) {
3453
const outOfBoundsMessage = util_inspect_1.inspect(value) +
@@ -43,18 +62,38 @@ function between(lower, value, upper, message) {
4362
throw new RangeError(outOfBoundsMessage);
4463
}
4564
}
65+
/**
66+
* Throws an error if the given value is not an integer
67+
* and in the range that can be represented in an unsigned byte
68+
* @param value The value in question
69+
*/
4670
function byteUnsignedInteger(value) {
4771
integer(value);
4872
between(0, value, 256);
4973
}
74+
/**
75+
* Throws an error with the specified message
76+
* @param message The message of the thrown error
77+
*/
5078
function fail(message) {
5179
throw new Error(message);
5280
}
53-
//Assert that a condition is met if not, throw an error with the specified message
81+
/**
82+
* Throws an error if the provided condition is false,
83+
* using the given error message.
84+
* This function is exported by this module.
85+
*/
5486
function assert(condition, message) {
5587
if (!condition)
5688
fail(message || 'Assertion failed');
5789
}
90+
/**
91+
* Throws an error if the given function does not throw
92+
* an error when executed.
93+
* Can also provide a message string given to [[errorMessage]].
94+
* @param block A function that should throw an error
95+
* @param message The optional message string to match against
96+
*/
5897
function throws(block, message) {
5998
let success = true;
6099
try {
@@ -67,6 +106,24 @@ function throws(block, message) {
67106
}
68107
assert(success, message ? 'Was expecting error: ' + message : 'Should throw an error');
69108
}
109+
/**
110+
* Throws an error if the provided values are not "equal".
111+
* This has different meanings for different types of expected values:
112+
* - If `expected` is an `Object`,
113+
* `actual[key]` should "equal" `expected[key]` for each `key` in `expected`
114+
* - If `expected` is an `Array`, the lengths should match
115+
* and corresponding elements should be "equal"
116+
* - If `expected` is a `Map`, the sizes should match and iterating
117+
* should yield "equal" corresponding keys and values
118+
* - If `expected` is a `Set`, `Array.from(actual)` should "equal" `Array.from(expected)`
119+
* - If `expected` is an `ArrayBuffer`, the lengths should match and corresponding
120+
* bytes should be "equal"
121+
* - If `expected` is a `Function`, the names should be equal
122+
* - If `expected` has an `equals()` method, that is used
123+
* - Otherwise, `===` is used
124+
* @param actual
125+
* @param expected
126+
*/
70127
function equal(actual, expected) {
71128
const error = () => new RangeError('Expected ' + util_inspect_1.inspect(expected) + ' but got ' + util_inspect_1.inspect(actual));
72129
if (expected) {
@@ -205,27 +262,24 @@ function equal(actual, expected) {
205262
throw error();
206263
}
207264
}
265+
/**
266+
* Throws an error if the given value is not an error
267+
* or its message doesn't start with the given message string
268+
* @param err The error value to test
269+
* @param message The string that the error message should start with
270+
*/
208271
function errorMessage(err, message) {
209272
instanceOf(message, String);
210273
assert(err !== null && err.message.startsWith(message), 'Message "' + (err ? err.message : 'No error thrown') + '" does not start with "' + message + '"');
211274
}
212275
//tslint:disable-next-line:prefer-object-spread
213276
exports.default = Object.assign(assert, {
214-
//Assert that the instance is an instance of the constructor, or at least one of the constructors, or a subclass
215277
instanceOf,
216-
//Assert that a number is an integer (within the +/-2^53 that can be represented precisely in a double)
217278
integer,
218-
//Assert that a number is between the specified values, with an optional message
219279
between,
220-
//Assert that a number fits in an unsigned byte
221280
byteUnsignedInteger,
222-
//Throw an error
223281
fail,
224-
//Assert that the execution of a function throws an error, and that the error message matches the specified one
225282
throws,
226-
//Assert that two values are "equal"
227-
//What this means depends a lot on the type of the expected value
228283
equal,
229-
//Assert that an error's message begins with the specified text
230284
errorMessage
231285
});

dist/lib/bit-math.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1+
/**
2+
* Efficiently computes `Math.floor(n / 8)`
3+
* @param n The number in question
4+
*/
15
export declare function dividedByEight(n: number): number;
6+
/**
7+
* Efficiently computes `n % 8`
8+
* @param n The number in question
9+
*/
210
export declare function modEight(n: number): number;

dist/lib/bit-math.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3+
/**
4+
* Efficiently computes `Math.floor(n / 8)`
5+
* @param n The number in question
6+
*/
37
function dividedByEight(n) {
4-
return n >>> 3; //efficiently divide by 8
8+
return n >>> 3;
59
}
610
exports.dividedByEight = dividedByEight;
11+
/**
12+
* Efficiently computes `n % 8`
13+
* @param n The number in question
14+
*/
715
function modEight(n) {
8-
return n & 0b111; //efficiently mod 8
16+
return n & 0b111;
917
}
1018
exports.modEight = modEight;

dist/lib/buffer-stream.d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import GrowableBuffer from './growable-buffer';
33
import { Readable } from 'stream';
44
/**
55
* A class for creating a readable stream
6-
* out of an [ArrayBuffer]{@link external:ArrayBuffer}
7-
* or a {@link GrowableBuffer}.
6+
* out of an `ArrayBuffer`
7+
* or a [[GrowableBuffer]].
88
* When dealing with very large buffers,
99
* this allows chunks to be sent as they are requested
1010
* rather than stuffing the whole buffer into the stream at once.
@@ -16,13 +16,13 @@ export default class BufferStream extends Readable {
1616
private readonly end;
1717
private offset;
1818
/**
19-
* @param {external:ArrayBuffer|GrowableBuffer} buffer
19+
* @param buffer
2020
* The buffer whose data to use.
21-
* If a {@link GrowableBuffer} is used, only the
21+
* If a [[GrowableBuffer]] is used, only the
2222
* occupied portion will be written by the stream.
23-
* Future additions to the {@link GrowableBuffer}
23+
* Future additions to the [[GrowableBuffer]]
2424
* will not be written.
25-
* If bytes inside the [ArrayBuffer]{@link external:ArrayBuffer}
25+
* If bytes inside the `ArrayBuffer`
2626
* or occupied portion are changed, behavior is undefined.
2727
*/
2828
constructor(buffer: ArrayBuffer | GrowableBuffer);

dist/lib/buffer-stream.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const growable_buffer_1 = require("./growable-buffer");
55
const stream_1 = require("stream");
66
/**
77
* A class for creating a readable stream
8-
* out of an [ArrayBuffer]{@link external:ArrayBuffer}
9-
* or a {@link GrowableBuffer}.
8+
* out of an `ArrayBuffer`
9+
* or a [[GrowableBuffer]].
1010
* When dealing with very large buffers,
1111
* this allows chunks to be sent as they are requested
1212
* rather than stuffing the whole buffer into the stream at once.
@@ -15,13 +15,13 @@ const stream_1 = require("stream");
1515
*/
1616
class BufferStream extends stream_1.Readable {
1717
/**
18-
* @param {external:ArrayBuffer|GrowableBuffer} buffer
18+
* @param buffer
1919
* The buffer whose data to use.
20-
* If a {@link GrowableBuffer} is used, only the
20+
* If a [[GrowableBuffer]] is used, only the
2121
* occupied portion will be written by the stream.
22-
* Future additions to the {@link GrowableBuffer}
22+
* Future additions to the [[GrowableBuffer]]
2323
* will not be written.
24-
* If bytes inside the [ArrayBuffer]{@link external:ArrayBuffer}
24+
* If bytes inside the `ArrayBuffer`
2525
* or occupied portion are changed, behavior is undefined.
2626
*/
2727
constructor(buffer) {

dist/lib/buffer-string.d.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
1+
/**
2+
* Converts UTF-8 bytes to a JavaScript string.
3+
* The inverse of [[fromString]].
4+
* @param buffer The binary data to convert
5+
*/
16
export declare function toString(buffer: Uint8Array): string;
7+
/**
8+
* Converts a JavaScript string to UTF-8 bytes.
9+
* The inverse of [[toString]].
10+
* @param str The string to convert
11+
*/
212
export declare function fromString(str: string): ArrayBuffer;
13+
/**
14+
* Converts bytes to a JavaScript string where
15+
* each character corresponds to one byte.
16+
* Like [[toString]] but works with bytes
17+
* that are invalid UTF-8.
18+
* Mainly used for using `ArrayBuffer`s as keys in maps.
19+
* @param buffer The binary data to convert
20+
*/
321
export declare function toBinaryString(buffer: ArrayBuffer): string;
22+
/**
23+
* Converts a string generated by [[toBinaryString]]
24+
* back into the bytes that generated it
25+
* @param str The string to convert
26+
*/
427
export declare function fromBinaryString(str: string): ArrayBuffer;

dist/lib/buffer-string.js

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ const assert_1 = require("./assert");
44
const growable_buffer_1 = require("./growable-buffer");
55
//Arbitrarily set; fairly low to be safe
66
const MAX_ARGUMENTS_LENGTH = 0x1000;
7-
//Convert bytes to a UTF-8 string
7+
/**
8+
* Converts UTF-8 bytes to a JavaScript string.
9+
* The inverse of [[fromString]].
10+
* @param buffer The binary data to convert
11+
*/
812
function toString(buffer) {
913
assert_1.default.instanceOf(buffer, Uint8Array);
1014
//Taken from https://github.com/feross/buffer/blob/da8a677bdb746ed9d6dae42ee1eaf236aad32ccb/index.js#L917-L988
@@ -75,6 +79,11 @@ function toString(buffer) {
7579
return str;
7680
}
7781
exports.toString = toString;
82+
/**
83+
* Converts a JavaScript string to UTF-8 bytes.
84+
* The inverse of [[toString]].
85+
* @param str The string to convert
86+
*/
7887
//Convert a string to UTF-8 bytes
7988
function fromString(str) {
8089
assert_1.default.instanceOf(str, String);
@@ -85,30 +94,39 @@ function fromString(str) {
8594
if (charcode < 0x80)
8695
utf8.add(charcode);
8796
else if (charcode < 0x800) {
88-
utf8.add(0xc0 | (charcode >> 6));
89-
utf8.add(0x80 | (charcode & 0x3f));
97+
utf8
98+
.add(0xc0 | (charcode >> 6))
99+
.add(0x80 | (charcode & 0x3f));
90100
}
91101
else {
92102
/*istanbul ignore else*/
93103
if (charcode < 0xd800 || charcode >= 0xe000) {
94-
utf8.add(0xe0 | (charcode >> 12));
95-
utf8.add(0x80 | ((charcode >> 6) & 0x3f));
96-
utf8.add(0x80 | (charcode & 0x3f));
104+
utf8
105+
.add(0xe0 | (charcode >> 12))
106+
.add(0x80 | ((charcode >> 6) & 0x3f))
107+
.add(0x80 | (charcode & 0x3f));
97108
}
98109
else {
99110
charcode = 0x10000 + (((charcode & 0x3ff) << 10) | (charcode & 0x3ff));
100-
utf8.add(0xf0 | (charcode >> 18));
101-
utf8.add(0x80 | ((charcode >> 12) & 0x3f));
102-
utf8.add(0x80 | ((charcode >> 6) & 0x3f));
103-
utf8.add(0x80 | (charcode & 0x3f));
111+
utf8
112+
.add(0xf0 | (charcode >> 18))
113+
.add(0x80 | ((charcode >> 12) & 0x3f))
114+
.add(0x80 | ((charcode >> 6) & 0x3f))
115+
.add(0x80 | (charcode & 0x3f));
104116
}
105117
}
106118
}
107119
return utf8.toBuffer();
108120
}
109121
exports.fromString = fromString;
110-
//Convert bytes to a string where each character represents one byte
111-
//Used for representing ArrayBuffers as keys in maps
122+
/**
123+
* Converts bytes to a JavaScript string where
124+
* each character corresponds to one byte.
125+
* Like [[toString]] but works with bytes
126+
* that are invalid UTF-8.
127+
* Mainly used for using `ArrayBuffer`s as keys in maps.
128+
* @param buffer The binary data to convert
129+
*/
112130
function toBinaryString(buffer) {
113131
assert_1.default.instanceOf(buffer, ArrayBuffer);
114132
let str = '';
@@ -119,7 +137,11 @@ function toBinaryString(buffer) {
119137
return str;
120138
}
121139
exports.toBinaryString = toBinaryString;
122-
//Convert a string generated by toBinaryString() back into bytes
140+
/**
141+
* Converts a string generated by [[toBinaryString]]
142+
* back into the bytes that generated it
143+
* @param str The string to convert
144+
*/
123145
function fromBinaryString(str) {
124146
assert_1.default.instanceOf(str, String);
125147
const buffer = new ArrayBuffer(str.length);

dist/lib/constants.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
/**
2+
* Type byte indicating what follows is a `flexInt` containing the location of the type.
3+
* The number is a negative offset from this type byte to the true location.
4+
*/
15
export declare const REPEATED_TYPE = 255;

dist/lib/constants.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
//Type byte indicating what follows is a FlexInt containing the location of the type
4-
//The number is a negative offset from this type byte to the true location
3+
/**
4+
* Type byte indicating what follows is a `flexInt` containing the location of the type.
5+
* The number is a negative offset from this type byte to the true location.
6+
*/
57
exports.REPEATED_TYPE = 0xFF;

dist/lib/constructor-registry.d.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
/**
2+
* A value that acts as a constructor
3+
*/
14
export interface Newable {
25
new (): object;
36
}
7+
/**
8+
* A function that acts as a constructor
9+
*/
410
export declare type Constructor = Function & Newable;
5-
/** @function
6-
* @name get
7-
* @desc Gets a constructor function
8-
* with the specified name. Multiple
9-
* invocations of this function with
11+
/**
12+
* Gets a constructor function with the specified name.
13+
* Multiple invocations of this function with
1014
* the same name produce the same function.
11-
* @param {Type} constructorName The name of the constructor
12-
* @return {constructor} A function that can be used
13-
* as a constructor and has the desired name
14-
* @private
15+
* @param constructorName The name of the resulting constructor
16+
* @return A function that can be used as a constructor and has the desired name
1517
*/
1618
export declare function get(constructorName: string): Constructor;

0 commit comments

Comments
 (0)