Comparing version 0.2.1 to 0.3.0
180
jpacks.js
@@ -8,3 +8,3 @@ (function (exportName) { | ||
* zswang (http://weibo.com/zswang) | ||
* @version 0.2.1 | ||
* @version 0.3.0 | ||
* @date 2015-11-03 | ||
@@ -122,12 +122,17 @@ */ | ||
*/ | ||
var defaultLittleEndian = true; | ||
var defaultOptions = { | ||
littleEndian: true | ||
}; | ||
/** | ||
* 设置默认低字节序 | ||
* 设置默认配置 | ||
* | ||
* @param {boolean} value 默认值 | ||
* @param {boolean} options 默认值 | ||
*/ | ||
function setDefaultLittleEndian(value) { | ||
defaultLittleEndian = value; | ||
function setDefaultOptions(options) { | ||
options = options || {}; | ||
Object.keys(options).forEach(function (key) { | ||
defaultOptions[key] = options[key]; | ||
}); | ||
} | ||
Schema.setDefaultLittleEndian = setDefaultLittleEndian; | ||
Schema.setDefaultOptions = setDefaultOptions; | ||
/** | ||
@@ -158,5 +163,2 @@ * 确保是 ArrayBuffer 类型 | ||
function unpack(packSchema, buffer, options, offsets) { | ||
/*<debug> | ||
console.log('unpack(packSchema: %j, buffer: %j)', packSchema, buffer); | ||
//</debug>*/ | ||
var schema = Schema.from(packSchema); | ||
@@ -169,5 +171,7 @@ if (!schema) { | ||
offsets = offsets || [0]; | ||
if (typeof options.littleEndian === 'undefined') { | ||
options.littleEndian = defaultLittleEndian; | ||
} | ||
Object.keys(defaultOptions).forEach(function (key) { | ||
if (typeof options[key] === 'undefined') { | ||
options[key] = defaultOptions[key]; | ||
} | ||
}); | ||
return schema.unpack(buffer, options, offsets); // 解码 | ||
@@ -185,5 +189,2 @@ } | ||
function pack(packSchema, data, options, buffer) { | ||
/*<debug> | ||
console.log('pack(schema: %j)', packSchema); | ||
//</debug>*/ | ||
var schema = Schema.from(packSchema); | ||
@@ -195,5 +196,7 @@ if (!schema) { | ||
options = options || {}; | ||
if (typeof options.littleEndian === 'undefined') { | ||
options.littleEndian = defaultLittleEndian; | ||
} | ||
Object.keys(defaultOptions).forEach(function (key) { | ||
if (typeof options[key] === 'undefined') { | ||
options[key] = defaultOptions[key]; | ||
} | ||
}); | ||
schema.pack(data, options, buffer); | ||
@@ -237,3 +240,3 @@ return buffer; | ||
console.log(t(1)(2).schema); | ||
// > f(1,2) | ||
// -> f(1,2) | ||
``` | ||
@@ -335,3 +338,5 @@ '''</example>''' | ||
var _ = jpacks; | ||
var _map = {}; | ||
var _map = { | ||
bytes: _.bytes(8) | ||
}; | ||
'int8,int16,int32,uint8,uint16,uint32,float32,float64,shortint,smallint,longint,byte,word,longword'.split(/,/).forEach(function (item) { | ||
@@ -342,10 +347,10 @@ _map[item] = item; | ||
console.log(_.stringify(_schema)); | ||
// -> union({int8:int8,int16:int16,int32:int32,uint8:uint8,uint16:uint16,uint32:uint32,float32:float32,float64:float64,shortint:shortint,smallint:smallint,longint:longint,byte:byte,word:word,longword:longword},8) | ||
// > union({bytes:array(uint8,8),int8:int8,int16:int16,int32:int32,uint8:uint8,uint16:uint16,uint32:uint32,float32:float32,float64:float64,shortint:shortint,smallint:smallint,longint:longint,byte:byte,word:word,longword:longword},8) | ||
var buffer = _.pack(_schema, { | ||
float64: 2.94296650666094e+189 | ||
bytes: [0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89] | ||
}); | ||
console.log(buffer.join(' ')); | ||
// -> 16 52 86 120 1 35 69 103 | ||
// > 18 35 52 69 86 103 120 137 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// -> {"int8":16,"int16":13328,"int32":2018915344,"uint8":16,"uint16":13328,"uint32":2018915344,"float32":1.7378241885569425e+34,"float64":2.94296650666094e+189,"shortint":16,"smallint":13328,"longint":2018915344,"byte":16,"word":13328,"longword":2018915344} | ||
// > {"bytes":[18,35,52,69,86,103,120,137],"int8":18,"int16":8978,"int32":1161044754,"uint8":18,"uint16":8978,"uint32":1161044754,"float32":2882.19189453125,"float64":-4.843717058781651e-263,"shortint":18,"smallint":8978,"longint":1161044754,"byte":18,"word":8978,"longword":1161044754} | ||
``` | ||
@@ -459,3 +464,3 @@ '''</example>''' | ||
console.log(buffer.join(' ')); | ||
// > 48 49 50 51 | ||
// > 49 48 51 50 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
@@ -475,3 +480,3 @@ // > [12337,12851] | ||
console.log(buffer.join(' ')); | ||
// > 2 48 49 50 51 | ||
// > 2 49 48 51 50 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
@@ -489,3 +494,3 @@ // > [12337,12851] | ||
console.log(buffer.join(' ')); | ||
// > 48 49 50 51 0 0 0 0 0 0 0 0 | ||
// > 49 48 51 50 0 0 0 0 0 0 0 0 | ||
console.log(JSON.stringify(jpacks.unpack(_schema, buffer))); | ||
@@ -497,5 +502,2 @@ // > [12337,12851,0,0,0,0] | ||
function arrayCreator(itemSchema, count) { | ||
/*<debug> | ||
console.log('arrayCreator()', Schema.stringify(itemSchema, count)); | ||
//</debug>*/ | ||
var size; | ||
@@ -603,8 +605,8 @@ var countSchema; | ||
console.log(_.stringify(_schema)); | ||
// -> object([string(uint8),uint16]) | ||
// > object([string(uint8),uint16]) | ||
var buffer = _.pack(_schema, ['zswang', 1978]); | ||
console.log(buffer.join(' ')); | ||
// -> 6 122 115 119 97 110 103 186 7 | ||
// > 6 122 115 119 97 110 103 186 7 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// -> ["zswang",1978] | ||
// > ["zswang",1978] | ||
``` | ||
@@ -621,3 +623,3 @@ '''</example>''' | ||
console.log(_.stringify(_schema)); | ||
// -> object({namespace:string,args:{0:uint8}}) | ||
// > object({namespace:string,args:{0:uint8}}) | ||
var buffer = _.pack(_schema, { | ||
@@ -628,5 +630,5 @@ name: 'zswang', | ||
console.log(buffer.join(' ')); | ||
// -> 6 122 115 119 97 110 103 186 7 | ||
// > 6 122 115 119 97 110 103 186 7 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// -> {"name":"zswang","year":1978} | ||
// > {"name":"zswang","year":1978} | ||
``` | ||
@@ -694,3 +696,3 @@ '''</example>''' | ||
console.log(_.stringify(_schema)); | ||
// -> union({length:uint8,content:string(uint8)},20) | ||
// > union({length:uint8,content:string(uint8)},20) | ||
var buffer = _.pack(_schema, { | ||
@@ -700,5 +702,5 @@ content: '0123456789' | ||
console.log(buffer.join(' ')); | ||
// -> 10 48 49 50 51 52 53 54 55 56 57 0 0 0 0 0 0 0 0 0 | ||
// > 10 48 49 50 51 52 53 54 55 56 57 0 0 0 0 0 0 0 0 0 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// -> {"length":10,"content":"0123456789"} | ||
// > {"length":10,"content":"0123456789"} | ||
``` | ||
@@ -755,8 +757,8 @@ '''</example>''' | ||
console.log(_.stringify(_schema)); | ||
// -> enums({Sun:0,Mon:1,Tues:2,Wed:3,Thur:4,Fri:5,Sat:6},uint8) | ||
// > enums({Sun:0,Mon:1,Tues:2,Wed:3,Thur:4,Fri:5,Sat:6},uint8) | ||
var buffer = _.pack(_schema, 'Tues'); | ||
console.log(buffer.join(' ')); | ||
// -> 2 | ||
// > 2 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// -> "Tues" | ||
// > "Tues" | ||
``` | ||
@@ -777,8 +779,8 @@ '''</example>''' | ||
console.log(_.stringify(_schema)); | ||
// -> enums({Unknown:-1,Continue:100,Processing:100,OK:200,Created:201,NotFound:404},int8) | ||
// > enums({Unknown:-1,Continue:100,Processing:100,OK:200,Created:201,NotFound:404},int8) | ||
var buffer = _.pack(_schema, 'Unknown'); | ||
console.log(buffer.join(' ')); | ||
// -> 255 | ||
// > 255 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// -> "Unknown" | ||
// > "Unknown" | ||
``` | ||
@@ -798,8 +800,8 @@ '''</example>''' | ||
console.log(_.stringify(_schema)); | ||
// -> enums({Unknown:-1,Continue:100,Processing:100,OK:200,Created:201,NotFound:404},int8) | ||
// > enums({Unknown:-1,Continue:100,Processing:100,OK:200,Created:201,NotFound:404},int8) | ||
var buffer = _.pack(_schema, 2); | ||
console.log(buffer.join(' ')); | ||
// -> 2 | ||
// > 2 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// -> 2 | ||
// > 2 | ||
'''</example>''' | ||
@@ -898,3 +900,3 @@ */ | ||
* @param {string} value 字符串内容 | ||
* @param {string=} encoding 编码类型,仅在 NodeJS 下生效,默认 'utf-8' | ||
* @param {string=} options.encoding 编码类型,仅在 NodeJS 下生效,默认 'utf-8' | ||
* @return {Array} 返回字节数组 | ||
@@ -910,5 +912,5 @@ * @return {Schema} 返回解析类型 | ||
*/ | ||
function stringBytes(value, encoding) { | ||
if (typeof Buffer !== 'undefined') { // NodeJS | ||
return new Buffer(value, encoding); | ||
function stringBytes(value, options) { | ||
if (!options.browser && typeof Buffer !== 'undefined') { // NodeJS | ||
return new Buffer(value, options.encoding); | ||
} else { | ||
@@ -931,8 +933,8 @@ return encodeUTF8(value).split('').map(function (item) { | ||
console.log(_.stringify(_schema)); | ||
// -> string(25) | ||
// > string(25) | ||
var buffer = _.pack(_schema, '你好世界!Hello'); | ||
console.log(buffer.join(' ')); | ||
// -> 228 189 160 229 165 189 228 184 150 231 149 140 239 188 129 72 101 108 108 111 0 0 0 0 0 | ||
// > 228 189 160 229 165 189 228 184 150 231 149 140 239 188 129 72 101 108 108 111 0 0 0 0 0 | ||
console.log(_.unpack(_schema, buffer)); | ||
// -> 你好世界!Hello | ||
// > 你好世界!Hello | ||
'''<example>''' | ||
@@ -944,8 +946,8 @@ '''<example>''' | ||
console.log(_.stringify(_schema)); | ||
// -> string('int8') | ||
// > string('int8') | ||
var buffer = _.pack(_schema, '你好世界!Hello'); | ||
console.log(buffer.join(' ')); | ||
// -> 20 228 189 160 229 165 189 228 184 150 231 149 140 239 188 129 72 101 108 108 111 | ||
// > 20 228 189 160 229 165 189 228 184 150 231 149 140 239 188 129 72 101 108 108 111 | ||
console.log(_.unpack(_schema, buffer)); | ||
// -> 你好世界!Hello | ||
// > 你好世界!Hello | ||
'''<example>''' | ||
@@ -959,4 +961,4 @@ */ | ||
var stringBuffer = Schema.unpack(schema, buffer, options, offsets); | ||
if (typeof Buffer !== 'undefined') { // NodeJS | ||
return new Buffer(stringBuffer).toString(); | ||
if (!options.browser && typeof Buffer !== 'undefined') { // NodeJS | ||
return new Buffer(stringBuffer).toString(options.encoding); | ||
} | ||
@@ -966,3 +968,3 @@ return decodeUTF8(String.fromCharCode.apply(String, stringBuffer)); | ||
pack: function _pack(value, options, buffer) { | ||
Schema.pack(schema, stringBytes(value), options, buffer); | ||
Schema.pack(schema, stringBytes(value, options), options, buffer); | ||
}, | ||
@@ -988,8 +990,8 @@ namespace: 'string', | ||
console.log(_.stringify(_schema)); | ||
// -> string(uint8) | ||
// > string(uint8) | ||
var buffer = _.pack(_schema, 'shortString'); | ||
console.log(buffer.join(' ')); | ||
// -> 11 115 104 111 114 116 83 116 114 105 110 103 | ||
// > 11 115 104 111 114 116 83 116 114 105 110 103 | ||
console.log(_.unpack(_schema, buffer)); | ||
// -> shortString | ||
// > shortString | ||
``` | ||
@@ -1009,8 +1011,8 @@ '''</example>''' | ||
console.log(_.stringify(_schema)); | ||
// -> string(uint16) | ||
// > string(uint16) | ||
var buffer = _.pack(_schema, 'smallString'); | ||
console.log(buffer.join(' ')); | ||
// -> 0 11 115 109 97 108 108 83 116 114 105 110 103 | ||
// > 0 11 115 109 97 108 108 83 116 114 105 110 103 | ||
console.log(_.unpack(_schema, buffer)); | ||
// -> smallString | ||
// > smallString | ||
``` | ||
@@ -1030,8 +1032,8 @@ '''</example>''' | ||
console.log(_.stringify(_schema)); | ||
// -> string(uint32) | ||
// > string(uint32) | ||
var buffer = _.pack(_schema, 'longString'); | ||
console.log(buffer.join(' ')); | ||
// -> 0 0 0 10 108 111 110 103 83 116 114 105 110 103 | ||
// > 0 0 0 10 108 111 110 103 83 116 114 105 110 103 | ||
console.log(_.unpack(_schema, buffer)); | ||
// -> longString | ||
// > longString | ||
``` | ||
@@ -1052,8 +1054,8 @@ '''</example>''' | ||
console.log(_.stringify(_schema)); | ||
// -> cstring(32) | ||
// > cstring(32) | ||
var buffer = _.pack(_schema, 'Hello 你好!'); | ||
console.log(buffer.join(' ')); | ||
// -> 72 101 108 108 111 32 228 189 160 229 165 189 239 188 129 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | ||
// > 72 101 108 108 111 32 228 189 160 229 165 189 239 188 129 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// -> "Hello 你好!" | ||
// > "Hello 你好!" | ||
``` | ||
@@ -1067,8 +1069,8 @@ '''</example>''' | ||
console.log(_.stringify(_schema)); | ||
// -> array(cstring(true),uint8) | ||
// > array(cstring(true),uint8) | ||
var buffer = _.pack(_schema, ['abc', 'defghijk', 'g']); | ||
console.log(buffer.join(' ')); | ||
// -> 3 97 98 99 0 100 101 102 103 104 105 106 107 0 103 0 | ||
// > 3 97 98 99 0 100 101 102 103 104 105 106 107 0 103 0 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// -> ["abc","defghijk","g"] | ||
// > ["abc","defghijk","g"] | ||
``` | ||
@@ -1098,3 +1100,3 @@ '''</example>''' | ||
var bytes = [0]; | ||
[].unshift.apply(bytes, Schema.stringBytes(value)); | ||
[].unshift.apply(bytes, Schema.stringBytes(value, options)); | ||
if (size === true) { // 自动大小 | ||
@@ -1133,3 +1135,3 @@ Schema.pack(Schema.bytes(bytes.length), bytes, options, buffer); | ||
console.log(_.stringify(_schema)); | ||
// -> {type:string(uint8),data:depend(type,cases([[name,string(uint8)],[age,uint8]]))} | ||
// > {type:string(uint8),data:depend(type,cases([[name,string(uint8)],[age,uint8]]))} | ||
var buffer = _.pack(_schema, { | ||
@@ -1140,5 +1142,5 @@ type: 'name', | ||
console.log(buffer.join(' ')); | ||
// -> 4 110 97 109 101 3 116 111 109 | ||
// > 4 110 97 109 101 3 116 111 109 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// -> {"type":"name","data":"tom"} | ||
// > {"type":"name","data":"tom"} | ||
var buffer = _.pack(_schema, { | ||
@@ -1149,5 +1151,5 @@ type: 'age', | ||
console.log(buffer.join(' ')); | ||
// -> 3 97 103 101 23 | ||
// > 3 97 103 101 23 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// -> {"type":"age","data":23} | ||
// > {"type":"age","data":23} | ||
``` | ||
@@ -1189,3 +1191,3 @@ '''</example>''' | ||
console.log(_.stringify(_schema)); | ||
// -> object({length1:int8,length2:int8,data1:depend(length1,bytes),data2:depend(length2,array(string(uint8)))}) | ||
// > object({length1:int8,length2:int8,data1:depend(length1,bytes),data2:depend(length2,array(string(uint8)))}) | ||
var buffer = _.pack(_schema, { | ||
@@ -1198,5 +1200,5 @@ length1: 2, | ||
console.log(buffer.join(' ')); | ||
// -> 2 3 1 2 3 231 148 178 3 228 185 153 3 228 184 153 | ||
// > 2 3 1 2 3 231 148 178 3 228 185 153 3 228 184 153 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// -> {"length1":2,"length2":3,"data1":[1,2],"data2":["甲","乙","丙"]} | ||
// > {"length1":2,"length2":3,"data1":[1,2],"data2":["甲","乙","丙"]} | ||
``` | ||
@@ -1256,8 +1258,8 @@ '''</example>''' | ||
console.log(_.stringify(_schema)); | ||
// -> parse(_xor,_xor,float64,8) | ||
// > parse(_xor,_xor,float64,8) | ||
var buffer = _.pack(_schema, 2.94296650666094e+189); | ||
console.log(buffer.join(' ')); | ||
// -> 111 75 41 7 126 92 58 24 | ||
// > 111 75 41 7 126 92 58 24 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// -> 2.94296650666094e+189 | ||
// > 2.94296650666094e+189 | ||
``` | ||
@@ -1264,0 +1266,0 @@ '''</example>''' |
@@ -5,3 +5,3 @@ { | ||
"description": "Binary data packing and unpacking.", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"homepage": "http://github.com/zswang/jpacks", | ||
@@ -29,2 +29,5 @@ "main": "jpacks.js", | ||
"readmeFilename": "README.md", | ||
"dependencies": { | ||
"jints": "^0.0.3" | ||
}, | ||
"devDependencies": { | ||
@@ -40,3 +43,3 @@ "mocha": "^2.0.1", | ||
"_dist_dev": "jdists src/jpacks.js -o jpacks.dev.js -r deubg,test,remove", | ||
"_dist": "jdists src/jpacks.js -o jpacks.js -r deubg,test,remove,safe", | ||
"_dist": "jdists src/jpacks.js -o jpacks.js -r debug,test,remove,safe", | ||
"_compress": "uglifyjs jpacks.js -o jpacks.min.js -p 5 -c -m", | ||
@@ -46,4 +49,4 @@ | ||
"test": "istanbul cover --hook-run-in-context node_modules/mocha/bin/_mocha -- -R spec", | ||
"mocha": "mocha", | ||
"dist": "npm run example && npm run _dist && npm run _compress && npm run test", | ||
"mocha": "jdists example.jdists.js -o test/example.js && mocha", | ||
"dist": "npm run example && npm run _dist_dev && npm run _dist && npm run _compress && npm run test", | ||
"lint": "jshint src/**/*.js src/*.js *.json" | ||
@@ -53,4 +56,5 @@ }, | ||
"jpacks.js", | ||
"jpacks.dev.js", | ||
"schemas-extend" | ||
] | ||
} |
@@ -19,2 +19,3 @@ var zlib = require('zlib'); | ||
console.log(_.stringify(_schema)) | ||
// > object({type:uint8,data:parse(zlib.gzipSync,zlib.gunzipSync,string(uint8),uint16)}) | ||
@@ -27,3 +28,6 @@ var buffer = _.pack(_schema, { | ||
console.log(buffer.join(' ')); | ||
console.log(_.unpack(_schema, buffer)); | ||
// > 2 42 0 31 139 8 0 0 0 0 0 0 11 19 121 178 119 193 211 165 123 159 236 152 246 124 106 207 251 61 141 30 169 57 57 249 0 183 181 133 147 21 0 0 0 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// > {"type":2,"data":"你好世界!Hello"} | ||
``` | ||
@@ -64,2 +68,3 @@ '''</example>''' | ||
console.log(_.stringify(_schema)) | ||
// > object({type:uint8,data:parse(zlib.deflateSync,zlib.inflateSync,string(uint8),uint16)}) | ||
@@ -72,3 +77,6 @@ var buffer = _.pack(_schema, { | ||
console.log(buffer.join(' ')); | ||
console.log(_.unpack(_schema, buffer)); | ||
// > 2 30 0 120 156 19 121 178 119 193 211 165 123 159 236 152 246 124 106 207 251 61 141 30 169 57 57 249 0 152 20 12 247 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// > {"type":2,"data":"你好世界!Hello"} | ||
``` | ||
@@ -75,0 +83,0 @@ '''</example>''' |
87035
6
2789
1
+ Addedjints@^0.0.3
+ Addedjints@0.0.3(transitive)