Comparing version 0.6.12 to 0.6.17
@@ -8,4 +8,4 @@ (function (exportName) { | ||
* zswang (http://weibo.com/zswang) | ||
* @version 0.6.12 | ||
* @date 2016-03-18 | ||
* @version 0.6.17 | ||
* @date 2016-03-21 | ||
*/ | ||
@@ -185,2 +185,5 @@ function createSchema() { | ||
//</debug>*/ | ||
if (packSchema === null) { | ||
return null; | ||
} | ||
var schema = Schema.from(packSchema); | ||
@@ -215,2 +218,5 @@ /*<safe>*/ | ||
//</debug>*/ | ||
if (packSchema === null) { | ||
return null; | ||
} | ||
var schema = Schema.from(packSchema); | ||
@@ -766,2 +772,22 @@ /*<safe>*/ | ||
``` | ||
* @example objectCreator:null | ||
```js | ||
var _ = jpacks; | ||
var _schema = _.object({ | ||
n1: null, | ||
n2: null, | ||
s1: _.int8 | ||
}); | ||
console.log(_.stringify(_schema)); | ||
// > object({n1:null,n2:null,s1:'int8'}) | ||
var buffer = _.pack(_schema, { | ||
n1: 1, | ||
n2: 2, | ||
s1: 1 | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 1 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// > {"n1":null,"n2":null,"s1":1} | ||
``` | ||
'''</example>''' | ||
@@ -788,5 +814,9 @@ */ | ||
}; | ||
keys.forEach(function (key) { | ||
options.$scope.offsets[key] = offsets[0]; | ||
result[key] = Schema.unpack(objectSchema[key], buffer, options, offsets); | ||
keys.forEach(function(key) { | ||
if (options.$scope.exit) { | ||
result[key] = null; | ||
} else { | ||
options.$scope.offsets[key] = offsets[0]; | ||
result[key] = Schema.unpack(objectSchema[key], buffer, options, offsets); | ||
} | ||
}); | ||
@@ -803,5 +833,9 @@ options.$scope = $scope; | ||
}; | ||
keys.forEach(function (key) { | ||
keys.every(function(key) { | ||
if (options.$scope.exit) { | ||
return false; | ||
} | ||
options.$scope.offsets[key] = buffer.length; | ||
Schema.pack(objectSchema[key], value[key], options, buffer); | ||
return true; | ||
}); | ||
@@ -814,3 +848,3 @@ options.$scope = $scope; | ||
} | ||
var object = Schema.together(objectCreator, function (fn, args) { | ||
var object = Schema.together(objectCreator, function(fn, args) { | ||
fn.namespace = 'object'; | ||
@@ -1355,2 +1389,93 @@ fn.args = args; | ||
/** | ||
* 退出对象类型的处理过程 | ||
* | ||
'''<example>''' | ||
* @example exitCreator():base | ||
```js | ||
var _ = jpacks; | ||
var _schema = _.object({ | ||
a: _.int8, | ||
b: _.int8, | ||
c: _.exit(), | ||
d: _.int8, | ||
e: _.int8 | ||
}); | ||
console.log(_.stringify(_schema)); | ||
// > object({a:'int8',b:'int8',c:exit(),d:'int8',e:'int8'}) | ||
var buffer = _.pack(_schema, { | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
d: 4, | ||
e: 5 | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 1 2 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// > {"a":1,"b":2,"c":null,"d":null,"e":null} | ||
``` | ||
* @example exitCreator():depend | ||
```js | ||
var _ = jpacks; | ||
_.def('A', { | ||
a: _.int8, | ||
b: _.depend('a', function (a) { | ||
return a === 1 ? _.int8 : _.exit(); | ||
}), | ||
c: _.int8, | ||
}); | ||
var _schema = _.object({ | ||
f1: 'A', | ||
f2: 'A' | ||
}) | ||
console.log(_.stringify(_schema)); | ||
// > object({f1:'A',f2:'A'}) | ||
var buffer = _.pack(_schema, { | ||
f1: { | ||
a: 1, | ||
b: 1, | ||
c: 2 | ||
}, | ||
f2: { | ||
a: 0, | ||
b: 1, | ||
c: 2 | ||
} | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 1 1 2 0 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// > {"f1":{"a":1,"b":1,"c":2},"f2":{"a":0,"b":null,"c":null}} | ||
``` | ||
'''</example>''' | ||
*/ | ||
function exitCreator() { | ||
return new Schema({ | ||
unpack: function _unpack(buffer, options, offsets) { | ||
/*<safe>*/ | ||
if (!options.$scope) { | ||
throw new Error('Unpack must running in object.'); | ||
} | ||
/*</safe>*/ | ||
options.$scope.exit = true; | ||
return null; | ||
}, | ||
pack: function _pack(value, options, buffer) { | ||
/*<safe>*/ | ||
if (!options.$scope) { | ||
throw new Error('Unpack must running in object.'); | ||
} | ||
/*</safe>*/ | ||
options.$scope.exit = true; | ||
}, | ||
namespace: 'exit', | ||
args: arguments | ||
}); | ||
} | ||
var exit = Schema.together(exitCreator, function (fn, args) { | ||
fn.namespace = 'exit'; | ||
fn.args = args; | ||
}); | ||
Schema.register('exit', exit); | ||
/** | ||
* 构建解析类型,针对大小会改变的数据 | ||
@@ -1589,2 +1714,56 @@ * | ||
Schema.register('linkArray', linkArray); | ||
/** | ||
* 定义一个合并结构 | ||
* | ||
* @param {Array of (Scheam|string)} schema 数据结构 | ||
* @return {Schema} 返回构建的数据结构 | ||
'''<example>''' | ||
* @example mergeCreator:base | ||
```js | ||
var _ = jpacks; | ||
_.def('structA', { | ||
a: _.int8, | ||
b: _.int8 | ||
}); | ||
_.def('structB', { | ||
c: _.int8, | ||
d: _.int8 | ||
}); | ||
var _schema = _.merge( | ||
['structA', 'structB'] | ||
); | ||
console.log(_.stringify(_schema)) | ||
// > object({a:'int8',b:'int8',c:'int8',d:'int8'}) | ||
var buffer = _.pack(_schema, { | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
d: 4 | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 1 2 3 4 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// > {"a":1,"b":2,"c":3,"d":4} | ||
``` | ||
'''</example>''' | ||
*/ | ||
function mergeCreator(schemas) { | ||
var mergeScheam = {}; | ||
schemas.forEach(function (item) { | ||
var schema = Schema.from(item); | ||
if (!schema) { | ||
return; | ||
} | ||
var obj = schema.args[0]; | ||
Object.keys(obj).forEach(function (key) { | ||
mergeScheam[key] = obj[key]; | ||
}); | ||
}); | ||
return Schema.object(mergeScheam); | ||
} | ||
var merge = Schema.together(mergeCreator, function (fn, args) { | ||
fn.namespace = 'merge'; | ||
fn.args = args; | ||
}); | ||
Schema.register('merge', merge); | ||
return Schema; | ||
@@ -1591,0 +1770,0 @@ } |
183
jpacks.js
@@ -8,4 +8,4 @@ (function (exportName) { | ||
* zswang (http://weibo.com/zswang) | ||
* @version 0.6.12 | ||
* @date 2016-03-18 | ||
* @version 0.6.17 | ||
* @date 2016-03-21 | ||
*/ | ||
@@ -167,2 +167,5 @@ function createSchema() { | ||
function unpack(packSchema, buffer, options, offsets) { | ||
if (packSchema === null) { | ||
return null; | ||
} | ||
var schema = Schema.from(packSchema); | ||
@@ -189,2 +192,5 @@ buffer = arrayBufferFrom(buffer); // 确保是 ArrayBuffer 类型 | ||
function pack(packSchema, data, options, buffer) { | ||
if (packSchema === null) { | ||
return null; | ||
} | ||
var schema = Schema.from(packSchema); | ||
@@ -727,2 +733,22 @@ buffer = buffer || []; | ||
``` | ||
* @example objectCreator:null | ||
```js | ||
var _ = jpacks; | ||
var _schema = _.object({ | ||
n1: null, | ||
n2: null, | ||
s1: _.int8 | ||
}); | ||
console.log(_.stringify(_schema)); | ||
// > object({n1:null,n2:null,s1:'int8'}) | ||
var buffer = _.pack(_schema, { | ||
n1: 1, | ||
n2: 2, | ||
s1: 1 | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 1 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// > {"n1":null,"n2":null,"s1":1} | ||
``` | ||
'''</example>''' | ||
@@ -744,5 +770,9 @@ */ | ||
}; | ||
keys.forEach(function (key) { | ||
options.$scope.offsets[key] = offsets[0]; | ||
result[key] = Schema.unpack(objectSchema[key], buffer, options, offsets); | ||
keys.forEach(function(key) { | ||
if (options.$scope.exit) { | ||
result[key] = null; | ||
} else { | ||
options.$scope.offsets[key] = offsets[0]; | ||
result[key] = Schema.unpack(objectSchema[key], buffer, options, offsets); | ||
} | ||
}); | ||
@@ -759,5 +789,9 @@ options.$scope = $scope; | ||
}; | ||
keys.forEach(function (key) { | ||
keys.every(function(key) { | ||
if (options.$scope.exit) { | ||
return false; | ||
} | ||
options.$scope.offsets[key] = buffer.length; | ||
Schema.pack(objectSchema[key], value[key], options, buffer); | ||
return true; | ||
}); | ||
@@ -770,3 +804,3 @@ options.$scope = $scope; | ||
} | ||
var object = Schema.together(objectCreator, function (fn, args) { | ||
var object = Schema.together(objectCreator, function(fn, args) { | ||
fn.namespace = 'object'; | ||
@@ -1269,2 +1303,83 @@ fn.args = args; | ||
/** | ||
* 退出对象类型的处理过程 | ||
* | ||
'''<example>''' | ||
* @example exitCreator():base | ||
```js | ||
var _ = jpacks; | ||
var _schema = _.object({ | ||
a: _.int8, | ||
b: _.int8, | ||
c: _.exit(), | ||
d: _.int8, | ||
e: _.int8 | ||
}); | ||
console.log(_.stringify(_schema)); | ||
// > object({a:'int8',b:'int8',c:exit(),d:'int8',e:'int8'}) | ||
var buffer = _.pack(_schema, { | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
d: 4, | ||
e: 5 | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 1 2 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// > {"a":1,"b":2,"c":null,"d":null,"e":null} | ||
``` | ||
* @example exitCreator():depend | ||
```js | ||
var _ = jpacks; | ||
_.def('A', { | ||
a: _.int8, | ||
b: _.depend('a', function (a) { | ||
return a === 1 ? _.int8 : _.exit(); | ||
}), | ||
c: _.int8, | ||
}); | ||
var _schema = _.object({ | ||
f1: 'A', | ||
f2: 'A' | ||
}) | ||
console.log(_.stringify(_schema)); | ||
// > object({f1:'A',f2:'A'}) | ||
var buffer = _.pack(_schema, { | ||
f1: { | ||
a: 1, | ||
b: 1, | ||
c: 2 | ||
}, | ||
f2: { | ||
a: 0, | ||
b: 1, | ||
c: 2 | ||
} | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 1 1 2 0 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// > {"f1":{"a":1,"b":1,"c":2},"f2":{"a":0,"b":null,"c":null}} | ||
``` | ||
'''</example>''' | ||
*/ | ||
function exitCreator() { | ||
return new Schema({ | ||
unpack: function _unpack(buffer, options, offsets) { | ||
options.$scope.exit = true; | ||
return null; | ||
}, | ||
pack: function _pack(value, options, buffer) { | ||
options.$scope.exit = true; | ||
}, | ||
namespace: 'exit', | ||
args: arguments | ||
}); | ||
} | ||
var exit = Schema.together(exitCreator, function (fn, args) { | ||
fn.namespace = 'exit'; | ||
fn.args = args; | ||
}); | ||
Schema.register('exit', exit); | ||
/** | ||
* 构建解析类型,针对大小会改变的数据 | ||
@@ -1470,2 +1585,56 @@ * | ||
Schema.register('linkArray', linkArray); | ||
/** | ||
* 定义一个合并结构 | ||
* | ||
* @param {Array of (Scheam|string)} schema 数据结构 | ||
* @return {Schema} 返回构建的数据结构 | ||
'''<example>''' | ||
* @example mergeCreator:base | ||
```js | ||
var _ = jpacks; | ||
_.def('structA', { | ||
a: _.int8, | ||
b: _.int8 | ||
}); | ||
_.def('structB', { | ||
c: _.int8, | ||
d: _.int8 | ||
}); | ||
var _schema = _.merge( | ||
['structA', 'structB'] | ||
); | ||
console.log(_.stringify(_schema)) | ||
// > object({a:'int8',b:'int8',c:'int8',d:'int8'}) | ||
var buffer = _.pack(_schema, { | ||
a: 1, | ||
b: 2, | ||
c: 3, | ||
d: 4 | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 1 2 3 4 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// > {"a":1,"b":2,"c":3,"d":4} | ||
``` | ||
'''</example>''' | ||
*/ | ||
function mergeCreator(schemas) { | ||
var mergeScheam = {}; | ||
schemas.forEach(function (item) { | ||
var schema = Schema.from(item); | ||
if (!schema) { | ||
return; | ||
} | ||
var obj = schema.args[0]; | ||
Object.keys(obj).forEach(function (key) { | ||
mergeScheam[key] = obj[key]; | ||
}); | ||
}); | ||
return Schema.object(mergeScheam); | ||
} | ||
var merge = Schema.together(mergeCreator, function (fn, args) { | ||
fn.namespace = 'merge'; | ||
fn.args = args; | ||
}); | ||
Schema.register('merge', merge); | ||
return Schema; | ||
@@ -1472,0 +1641,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"description": "Binary data packing and unpacking.", | ||
"version": "0.6.12", | ||
"version": "0.6.17", | ||
"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
121339
3923