1 | # struct
|
2 |
|
3 | [![npm version](https://badge.fury.io/js/%40chengaoyuan%2Fstruct.svg)](https://badge.fury.io/js/%40chengaoyuan%2Fstruct)
|
4 | [![install size](https://packagephobia.now.sh/badge?p=@chengaoyuan/struct)](https://packagephobia.now.sh/result?p=@chengaoyuan/struct)
|
5 | [![NPM Downloads](https://img.shields.io/npm/dm/@chengaoyuan/struct.svg?style=flat)](https://npmcharts.com/compare/@chengaoyuan/struct?minimal=true)
|
6 | [![Build Status](https://travis-ci.org/GithubCGY/struct.svg?branch=master)](https://travis-ci.org/GithubCGY/struct)
|
7 | [![Coverage Status](https://coveralls.io/repos/github/GithubCGY/struct/badge.svg?branch=master)](https://coveralls.io/github/GithubCGY/struct?branch=master)
|
8 |
|
9 | ## Installation
|
10 |
|
11 | $ npm install @chengaoyuan/struct
|
12 |
|
13 | ## Usage
|
14 |
|
15 | ```ts
|
16 | import * as assert from "assert";
|
17 | import Struct from "@chengaoyuan/struct";
|
18 | const struct = new Struct([
|
19 | ["DWORD", "len"],
|
20 | ["UINT8", "data", 0]
|
21 | ]);
|
22 |
|
23 | const arr = [1, 2, 2, 2, 3, 3, 4, 2, 3, 34, 5, 42, 2, 21, 87, 4, 3, 2];
|
24 | const oldObj = {
|
25 | len: arr.length,
|
26 | data: arr
|
27 | };
|
28 |
|
29 | {
|
30 | const ab = struct.serialize(oldObj);
|
31 | const newObj = struct.unserialize(ab);
|
32 | assert.deepEqual(newObj, oldObj);
|
33 | }
|
34 |
|
35 | {
|
36 | const size = struct.getRealSize(oldObj);
|
37 | const buf = new ArrayBuffer(size);
|
38 | const bufView = new DataView(buf);
|
39 | struct.encode(bufView, 0, data);
|
40 | const newObj = struct.decode(bufView, 0);
|
41 | assert.deepEqual(newObj, oldObj);
|
42 | }
|
43 | ```
|
44 |
|
45 | ## Testing
|
46 |
|
47 | $ npm test
|