Skip to content

Commit

Permalink
Add example as browser
Browse files Browse the repository at this point in the history
  • Loading branch information
zswang committed Feb 29, 2016
1 parent 30d2f1f commit b4ccff6
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 11 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ jpacks 一款强大的二进制结构编解码工具
e.g.

```js
jpacks.register('User', {
// 定义结构
jpacks.def('User', {
age: 'uint8',
token: jpacks.array('byte', 10),
name: jpacks.shortString,
note: jpacks.longString,
contacts: jpacks.shortArray('User')
});

var user = {
age: 6,
token: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
Expand All @@ -43,19 +45,32 @@ var user = {
contacts: []
}]
};

// 组包
var buffer = jpacks.pack('User', user);

// 解包
var user2 = jpacks.unpack('User', buffer);

console.log(JSON.stringify(user2, null, ' '));
```

## feature
## Features 功能性

+ Supports running in NodeJS and Browser. 支持运行在 NodeJS 和浏览器环境。
+ Can be nested definition. 支持嵌套定义。
+ Commonly used numerical types, including signed and unsigned. 常用的数值类型,包括有符号和无符号。
+ Supports array types, including fixed length or indefinite. 支持数组类型,包括定长或不定长。
+ Int64. 大整数处理。
+ Protocol Buffers. 支持处理 PB 协议。
+ Data compression and decompression. 数据压缩和解压。

## Dependencies 依赖

+ [protobufjs](https://github.com/dcodeIO/protobuf.js)
+ [long](https://github.com/dcodeIO/long.js)

## License
## License 许可协议

MIT © [zswang](http://weibo.com/zswang)

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jpacks",
"description": "Binary data packing and unpacking.",
"version": "0.6.2",
"version": "0.6.3",
"homepage": "https://github.com/zswang/jpacks",
"authors": {
"name": "zswang",
Expand Down
34 changes: 34 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jpacks</title>
</head>
<body>
<div></div>
<div></div>
</body>
<script src="../jpacks.js"></script>
<script>
(function () {
var _ = jpacks;
_.def('Point', {
x: _.int32,
y: _.int32,
tag: _.cstring(null)
});

var buffer = _.pack('Point', {
x: 30,
y: 190,
tag: '北京 BeiJing'
});

document.querySelector('div:nth-of-type(1)').innerHTML = JSON.stringify(buffer);

var data = _.unpack('Point', buffer);

document.querySelector('div:nth-of-type(2)').innerHTML = JSON.stringify(data);
})();
</script>
</html>
4 changes: 2 additions & 2 deletions jpacks.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* Binary data packing and unpacking.
* @author
* zswang (http://weibo.com/zswang)
* @version 0.6.2
* @date 2015-11-15
* @version 0.6.3
* @date 2016-02-29
*/
function createSchema() {
/**
Expand Down
4 changes: 2 additions & 2 deletions jpacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* Binary data packing and unpacking.
* @author
* zswang (http://weibo.com/zswang)
* @version 0.6.2
* @date 2015-11-15
* @version 0.6.3
* @date 2016-02-29
*/
function createSchema() {
/**
Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "jpacks",
"title": "c struct packer",
"description": "Binary data packing and unpacking.",
"version": "0.6.2",
"version": "0.6.3",
"homepage": "http://github.com/zswang/jpacks",
"main": "jpacks.js",
"author": {
Expand All @@ -16,9 +16,14 @@
"keywords": [
"c",
"struct",
"serialization",
"serialize",
"pack",
"unpack"
"unpack",
"binary",
"parse",
"structure",
"packets",
"schema"
],
"bugs": {
"url": "https://github.com/zswang/jpacks/issues"
Expand Down

0 comments on commit b4ccff6

Please sign in to comment.