Comparing version 0.0.2 to 0.0.4
285
jpacks.js
@@ -8,3 +8,3 @@ (function (exportName) { | ||
* zswang (http://weibo.com/zswang) | ||
* @version 0.0.2 | ||
* @version 0.0.4 | ||
* @date 2015-10-30 | ||
@@ -70,2 +70,13 @@ */ | ||
* @return {Schema} 返回数据结构 | ||
* @example 调用示例 1 | ||
```js | ||
var _ = jpacks; | ||
var _schema = _.array(_.byte, 10); | ||
var ab = _.pack(_.array(_.byte, 10), [1, 2, 3, 4]); | ||
var u8a = new Uint8Array(ab); | ||
console.log(u8a); | ||
// -> [1, 2, 3, 4, 0, 0, 0, 0, 0, 0] | ||
console.log(_.unpack(_schema, u8a)); | ||
// -> [1, 2, 3, 4, 0, 0, 0, 0, 0, 0] | ||
``` | ||
*/ | ||
@@ -87,2 +98,23 @@ function array(schema, count) { | ||
/** | ||
* 声明字节数组类型 | ||
* | ||
* @param {number} count 元素个数 | ||
* @return {Schema} 返回数据结构 | ||
* @example 调用示例 | ||
```js | ||
var _ = jpacks; | ||
var _schema = _.bytes(10); | ||
var ab = _.pack(_schema, [1, 2, 3, 4]); | ||
var u8a = new Uint8Array(ab); | ||
console.log(u8a); | ||
// -> [1, 2, 3, 4, 0, 0, 0, 0, 0, 0] | ||
console.log(_.unpack(_schema, u8a)); | ||
// -> [1, 2, 3, 4, 0, 0, 0, 0, 0, 0] | ||
``` | ||
*/ | ||
function bytes(count) { | ||
return array('uint8', count); | ||
} | ||
exports.bytes = bytes; | ||
/** | ||
* 声明指定长度的数组类型 | ||
@@ -93,2 +125,24 @@ * | ||
* @return {Schema} 返回数据结构 | ||
* @example 调用示例 1 | ||
```js | ||
var _ = jpacks; | ||
var _schema = _.lengthArray(_.byte, _.byte); | ||
var ab = _.pack(_schema, [1, 2, 3, 4]); | ||
var u8a = new Uint8Array(ab); | ||
console.log(u8a); | ||
// -> [4, 1, 2, 3, 4] | ||
console.log(_.unpack(_schema, u8a)); | ||
// -> [1, 2, 3, 4] | ||
``` | ||
* @example 调用示例 2 | ||
```js | ||
var _ = jpacks; | ||
var _schema = _.lengthArray(_.word, _.byte); | ||
var ab = _.pack(_schema, [1, 2, 3, 4]); | ||
var u8a = new Uint8Array(ab); | ||
console.log(u8a); | ||
// -> [0, 0, 0, 4, 1, 2, 3, 4] | ||
console.log(_.unpack(_schema, u8a)); | ||
// -> [1, 2, 3, 4] | ||
``` | ||
*/ | ||
@@ -123,2 +177,13 @@ function lengthArray(lengthSchema, itemSchema) { | ||
* @return {Schema} 返回数据结构 | ||
* @example 调用示例 | ||
```js | ||
var _ = jpacks; | ||
var _schema = _.shortArray(_.byte); | ||
var ab = _.pack(_schema, [1, 2, 3, 4]); | ||
var u8a = new Uint8Array(ab); | ||
console.log(u8a); | ||
// -> [4, 1, 2, 3, 4] | ||
console.log(_.unpack(_schema, u8a)); | ||
// -> [1, 2, 3, 4] | ||
``` | ||
*/ | ||
@@ -134,2 +199,13 @@ function shortArray(itemSchema) { | ||
* @return {Schema} 返回数据结构 | ||
* @example 调用示例 | ||
```js | ||
var _ = jpacks; | ||
var _schema = _.longArray(_.byte); | ||
var ab = _.pack(_schema, [1, 2, 3, 4]); | ||
var u8a = new Uint8Array(ab); | ||
console.log(u8a); | ||
// -> [0, 4, 1, 2, 3, 4] | ||
console.log(_.unpack(_schema, u8a)); | ||
// -> [1, 2, 3, 4] | ||
``` | ||
*/ | ||
@@ -149,15 +225,17 @@ function longArray(itemSchema) { | ||
return new Schema(function _unpack(buffer, options, offsets) { | ||
var bytes = unpack(schema, buffer, options, offsets); | ||
var stringBuffer = unpack(schema, buffer, options, offsets); | ||
if (typeof Buffer !== 'undefined') { // NodeJS | ||
return new Buffer(bytes).toString(); | ||
return new Buffer(stringBuffer).toString(); | ||
} | ||
return decodeUTF8(String.fromCharCode(bytes)); | ||
return decodeUTF8(String.fromCharCode.apply(String, stringBuffer)); | ||
}, function _pack(value, options, buffer) { | ||
var bytes; | ||
var stringBuffer; | ||
if (typeof Buffer !== 'undefined') { // NodeJS | ||
bytes = new Buffer(value); | ||
stringBuffer = new Buffer(value); | ||
} else { | ||
bytes = encodeUTF8(value).split(''); | ||
stringBuffer = encodeUTF8(value).split('').map(function (item) { | ||
return item.charCodeAt(); | ||
}); | ||
} | ||
pack(schema, new Buffer(value), options, buffer); | ||
pack(schema, stringBuffer, options, buffer); | ||
}); | ||
@@ -167,2 +245,15 @@ } | ||
* 短字符串类型 | ||
* | ||
* @return {Schema} 返回数据结构 | ||
* @example 调用示例 | ||
```js | ||
var _ = jpacks; | ||
var _schema = _.shortString; | ||
var ab = _.pack(_schema, '你好 World!'); | ||
var u8a = new Uint8Array(ab); | ||
console.log(u8a); | ||
// -> [13, 228, 189, 160, 229, 165, 189, 32, 87, 111, 114, 108, 100, 33] | ||
console.log(_.unpack(_schema, u8a)); | ||
// -> 你好 World! | ||
``` | ||
*/ | ||
@@ -172,7 +263,159 @@ exports.shortString = lengthString('uint8'); | ||
* 长字符串类型 | ||
* | ||
* @return {Schema} 返回数据结构 | ||
* @example 调用示例 | ||
```js | ||
var _ = jpacks; | ||
var _schema = _.longString; | ||
var ab = _.pack(_schema, '你好 World!'); | ||
var u8a = new Uint8Array(ab); | ||
console.log(u8a); | ||
// -> [0, 13, 228, 189, 160, 229, 165, 189, 32, 87, 111, 114, 108, 100, 33] | ||
console.log(_.unpack(_schema, u8a)); | ||
// -> 你好 World! | ||
``` | ||
*/ | ||
exports.longString = lengthString('uint16'); | ||
/** | ||
* 创建联合类型 | ||
* | ||
* @param {number} size 联合类型总大小 | ||
* @param {Object} schema 联合类型中出现的字段 | ||
* @return {Schema} 返回联合类型 | ||
* @example 调用示例 | ||
```js | ||
var _ = jpacks; | ||
var _schema = _.union(20, { | ||
length: _.byte, | ||
content: _.shortString | ||
}); | ||
var ab = _.pack(_schema, { | ||
content: '0123456789' | ||
}); | ||
var u8a = new Uint8Array(ab); | ||
console.log(u8a); | ||
// -> [10, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 0, 0, 0, 0, 0, 0, 0, 0, 0] | ||
console.log(_.unpack(_schema, u8a)); | ||
// -> Object {length: 10, content: "0123456789"} | ||
``` | ||
*/ | ||
function union(size, schemas) { | ||
if (typeof size !== 'number') { | ||
throw new Error('Parameter "size" must be a numeric type.'); | ||
} | ||
if (typeof schemas !== 'object') { | ||
throw new Error('Parameter "schemas" must be a object type.'); | ||
} | ||
if (schemas instanceof Schema) { | ||
throw new Error('Parameter "schemas" cannot be a Scheam object.'); | ||
} | ||
var keys = Object.keys(schemas); | ||
return new Schema(function _unpack(buffer, options, offsets) { | ||
var beginOffset = offsets[0]; | ||
var result = {}; | ||
keys.forEach(function (key) { | ||
offsets[0] = beginOffset; | ||
result[key] = unpack(schemas[key], buffer, options, offsets); | ||
}); | ||
offsets[0] += size; | ||
return result; | ||
}, function _pack(value, options, buffer) { | ||
var arrayBuffer = new ArrayBuffer(size); | ||
var uint8Array = new Uint8Array(arrayBuffer); | ||
keys.forEach(function (key) { | ||
var temp = []; | ||
pack(schemas[key], value[key], options, temp); | ||
uint8Array.set(temp); | ||
}); | ||
[].push.apply(buffer, uint8Array); | ||
}); | ||
} | ||
exports.union = union; | ||
/** | ||
* 创建条件类型 | ||
* | ||
* @param {Object} schemas 条件类型结构,第一个字段为条件字段,其他字段为数组。数组第一元素表示命中条件,第二位类型 | ||
* @return {Schema} 返回联合类型 | ||
* @example 调用示例 1 | ||
```js | ||
var _ = jpacks; | ||
var _schema = _.cases({ | ||
type: _.shortString, | ||
name: ['name', _.shortString], | ||
age: ['age', _.byte] | ||
}); | ||
var ab = _.pack(_schema, { | ||
type: 'name', | ||
name: 'tom' | ||
}); | ||
var u8a = new Uint8Array(ab); | ||
console.log(u8a); | ||
// -> [4, 110, 97, 109, 101, 3, 116, 111, 109] | ||
console.log(_.unpack(_schema, u8a)); | ||
// -> Object {type: "name", name: "tom"} | ||
var ab2 = _.pack(_schema, { | ||
type: 'age', | ||
age: 23 | ||
}); | ||
var u8a2 = new Uint8Array(ab2); | ||
console.log(u8a2); | ||
// -> [3, 97, 103, 101, 23] | ||
console.log(_.unpack(_schema, u8a2)); | ||
// -> Object {type: "age", age: 23} | ||
``` | ||
*/ | ||
function cases(schemas) { | ||
if (typeof schemas !== 'object') { | ||
throw new Error('Parameter "schemas" must be a object type.'); | ||
} | ||
if (schemas instanceof Schema) { | ||
throw new Error('Parameter "schemas" cannot be a Scheam object.'); | ||
} | ||
var keys = Object.keys(schemas); | ||
var patternName = keys[0]; | ||
var patternSchema = schemas[patternName]; | ||
keys = keys.slice(1); | ||
return new Schema(function _unpack(buffer, options, offsets) { | ||
var result = {}; | ||
var patternValue = unpack(patternSchema, buffer, options, offsets); | ||
result[patternName] = patternValue; | ||
keys.every(function (key) { | ||
if (patternValue === schemas[key][0]) { | ||
result[key] = unpack(schemas[key][1], buffer, options, offsets); | ||
return false; | ||
} | ||
return true; | ||
}); | ||
return result; | ||
}, function _pack(value, options, buffer) { | ||
var patternValue = value[patternName]; | ||
pack(patternSchema, patternValue, options, buffer); | ||
keys.every(function (key) { | ||
if (patternValue === schemas[key][0]) { | ||
pack(schemas[key][1], value[key], options, buffer); | ||
return false; | ||
} | ||
return true; | ||
}); | ||
}); | ||
} | ||
exports.cases = cases; | ||
/** | ||
* 数据结构 | ||
* | ||
* @param {Function} unpack 数据解析的方法 | ||
* [[[ | ||
* @param {ArrayBuffer|Buffer|Array} buffer 数据缓存 | ||
* @param {Object=} options 配置项 | ||
* @param {Array=} offsets 第一个原始为偏移,数值类型是为了能修改值 | ||
* @return {Any} 返回解析后的对象 | ||
* ]]] | ||
* function unpack(buffer, options, offsets) {} | ||
* @param {Function} pack 数据打包的方法 | ||
* [[[ | ||
* @param {Any} value 需要打包的对象 | ||
* @param {Object=} options 配置项 | ||
* @param {buffer=} {Array} 目标字节数组 | ||
* ]]] | ||
* function pack(value, options, buffer) {} | ||
* @constructor 构造数据结构类型 | ||
@@ -245,3 +488,2 @@ */ | ||
* @param {Object} schema 数据结构 | ||
* @return {Object} 返回 schema 参数 | ||
*/ | ||
@@ -280,4 +522,2 @@ function register(name, schema) { | ||
} | ||
options = options || {}; | ||
offsets = offsets || [0]; | ||
if (!(schema instanceof Schema)) { | ||
@@ -294,2 +534,7 @@ if (typeof schema === 'string') { | ||
} | ||
options = options || {}; | ||
offsets = offsets || [0]; | ||
if (typeof options.littleEndian === 'undefined') { | ||
options.littleEndian = defaultLittleEndian; | ||
} | ||
if (typeof schema.unpack === 'function') { | ||
@@ -334,2 +579,5 @@ return schema.unpack(buffer, options, offsets); | ||
options = options || {}; | ||
if (typeof options.littleEndian === 'undefined') { | ||
options.littleEndian = defaultLittleEndian; | ||
} | ||
if (typeof schema.pack === 'function') { | ||
@@ -347,2 +595,17 @@ schema.pack(data, options, buffer); | ||
exports.pack = pack; | ||
/** | ||
* 默认低字节序 | ||
* | ||
* @type {boolean} | ||
*/ | ||
var defaultLittleEndian; | ||
/** | ||
* 设置默认低字节序 | ||
* | ||
* @param {boolean} value 默认值 | ||
*/ | ||
function setDefaultLittleEndian(value) { | ||
defaultLittleEndian = value; | ||
} | ||
exports.setDefaultLittleEndian = setDefaultLittleEndian; | ||
if (typeof define === 'function') { | ||
@@ -349,0 +612,0 @@ if (define.amd || define.cmd) { |
@@ -5,3 +5,3 @@ { | ||
"description": "Binary data packing and unpacking.", | ||
"version": "0.0.2", | ||
"version": "0.0.4", | ||
"homepage": "http://github.com/zswang/jpacks", | ||
@@ -8,0 +8,0 @@ "main": "jpacks.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20745
607