Skip to content

Commit c2faf47

Browse files
committed
Added documentation for BinaryWriter
1 parent 3224be2 commit c2faf47

File tree

8 files changed

+280
-92
lines changed

8 files changed

+280
-92
lines changed

README.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ Add it to your project via:
1414
npm i --save csharp-binary-stream
1515
```
1616

17+
1718
### Documentation
1819

1920
The full documentation can be found [here](https://evidentlycube.github.io/chsarp-binary-stream/index.html).
2021

21-
Use the reader like this:
22+
The reader can be used like this:
2223

2324
```
2425
import {BinaryReader, Encoding} from `csharp-binary-stream`;
@@ -28,21 +29,35 @@ const reader = new BinaryReader(existingArrayBuffer);
2829
const reader = new BinaryReader(existingUint8Array);
2930
// or
3031
const reader = new BinaryReader(new Uint8Array(otherTypedArray.buffer, otherTypedArray.byteOffset, otherTypedArray.byteLength));
31-
reader.readByte();
32-
reader.readChar(Encoding.Utf8);
33-
reader.readFloat();
32+
console.log(reader.readByte());
33+
console.log(reader.readChar(Encoding.Utf8));
34+
console.log(reader.readFloat());
35+
```
36+
37+
While the writer like this:
38+
39+
```
40+
import {BinaryWriter, Encoding} from `csharp-binary-stream`;
41+
42+
const writer = new BinaryWriter();
43+
// or
44+
const writer = new BinaryWriter(existingNumberArray);
45+
//or
46+
const writer = new BinaryWriter(existingUint8Array);
47+
writer.writeByte(7);
48+
writer.writeChars("Writing some text", Encoding.Utf8);
49+
writer.writeLong("12345678900012");
3450
```
3551

3652
## Details
3753

3854
The validity of the compatibility with C# is achieved by comparing the data against fixtures generated in C#. Specifically, the file `test/fixtureSource.cs` is responsible for generating all the files in the directory `test/fixture/*`. Then the tests in `test/BinaryReader.fixtures.*.test.ts` attempt to read the files and compare the value provideded with static values stored in the test file.
3955

40-
At the moment only `BinaryReader` is supported, but `BinaryWriter` is soon to come.
41-
4256
There are some small well-document quirks to be aware of:
4357

4458
- Using `readLong` and `readUnsignedLong` risks losing precision with really big numbers because JavaScript only supports `double` type.
45-
59+
- Using `writeLong` and `writeUnsignedLong` with numbers instead of strings risks losing precision with really big numbers because JavaScript only supports `double` type.
60+
4661
## License
4762

4863
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details

src/BinaryReader.ts

Lines changed: 42 additions & 42 deletions
Large diffs are not rendered by default.

src/BinaryWriter.ts

Lines changed: 195 additions & 25 deletions
Large diffs are not rendered by default.

src/Numbers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* @ignore
3+
*/
14
export const Numbers = {
25
BYTE: {
36
MIN: 0,

src/Utf8.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ interface Utf8ReadResult
3030
finalPosition: number;
3131
}
3232

33+
/**
34+
* @ignore
35+
*/
3336
export function writeUtf8StringFromCodePoints(buffer: number[], position: number, dataToWrite: number[] | string): number
3437
{
3538
if (typeof dataToWrite === "string") {

test/BinaryWriter.misc.common.test.ts

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {expect} from 'chai';
22
import {BinaryWriter} from "../src";
33
import {OutOfBoundsError} from "../src/errors/OutOfBoundsError";
44

5-
describe("BinaryWriter, string UTF-8 encoding misc tests", () =>
5+
describe("BinaryWriter, common misc tests", () =>
66
{
77
describe("get position", () =>
88
{
@@ -73,26 +73,13 @@ describe("BinaryWriter, string UTF-8 encoding misc tests", () =>
7373
});
7474
});
7575

76-
describe("get capacity", () =>
77-
{
78-
[0, 10, 128, 5000].forEach(capacity =>
79-
{
80-
it(`Should return capacity provided in constructor (${capacity})`, () =>
81-
{
82-
const writer = new BinaryWriter(capacity);
83-
84-
expect(writer.capacity).to.equal(capacity);
85-
});
86-
});
87-
});
88-
8976
describe("constructor", () =>
9077
{
91-
it("constructor(number) - Should set capacity", () =>
78+
it("constructor() - Should create empty writer", () =>
9279
{
93-
const writer = new BinaryWriter(13);
80+
const writer = new BinaryWriter();
9481

95-
expect(writer.capacity).to.equal(13);
82+
expect(writer.length).to.equal(0);
9683
expect(writer.toArray()).to.deep.equal([]);
9784
});
9885

test/BinaryWriter.misc.utf8.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,14 @@ describe("BinaryWriter, string UTF-8 encoding misc tests", () =>
104104
});
105105
});
106106
});
107+
108+
describe("Misc", () => {
109+
it("writeChar writes only the first character of the provided string", () => {
110+
const writer = new BinaryWriter();
111+
writer.writeChar("O HAI", Encoding.Utf8);
112+
113+
expect(writer.length).to.equal(1);
114+
expect(writer.toArray()).to.deep.equal(['O'.codePointAt(0)]);
115+
});
116+
})
107117
});

test/BinaryWriter.negatives.string.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {expect} from 'chai';
22
import {BinaryWriter, Encoding} from "../src";
33
import {InvalidArgumentError} from "../src/errors/InvalidArgumentError";
44

5-
describe("BinaryWriter, string UTF-8 negative tests", () =>
5+
describe("BinaryWriter, string negative tests", () =>
66
{
77
describe("Invalid arguments", () =>
88
{

0 commit comments

Comments
 (0)