-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboolean-array.ts
More file actions
48 lines (47 loc) · 1.53 KB
/
Copy pathboolean-array.ts
File metadata and controls
48 lines (47 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import type {AppendableBuffer} from '../lib/appendable'
import * as assert from '../lib/assert'
import * as flexInt from '../lib/flex-int'
import {BufferOffset, readBooleans, readFlexInt} from '../lib/read-util'
import {writeBooleans} from '../lib/write-util'
import AbsoluteType from './absolute'
/**
* A type storing a variable-length array of `Boolean` values.
* This type creates more efficient serializations than
* `new sb.ArrayType(new sb.BooleanType)` for boolean arrays,
* since it works with bits instead of whole bytes.
*
* Example:
* ````javascript
* let type = new sb.BooleanArrayType
* ````
*/
export class BooleanArrayType extends AbsoluteType<boolean[]> {
static get _value(): number {
return 0x32
}
/**
* Appends value bytes to an [[AppendableBuffer]] according to the type
*
* Examples:
* ````javascript
* type.writeValue(buffer, [false]) //takes up 2 bytes
* ````
* or
* ````javascript
* type.writeValue(buffer, new Array(100).fill(true)) //takes up 14 bytes
* ````
* @param buffer The buffer to which to append
* @param value The value to write
* @throws If the value doesn't match the type, e.g. `new sb.StringType().writeValue(buffer, 23)`
*/
writeValue(buffer: AppendableBuffer, value: boolean[]): void {
assert.isBuffer(buffer)
assert.instanceOf(value, Array)
buffer.addAll(flexInt.makeValueBuffer(value.length))
writeBooleans(buffer, value)
}
consumeValue(bufferOffset: BufferOffset): boolean[] {
const count = readFlexInt(bufferOffset)
return readBooleans({bufferOffset, count})
}
}