Comparing version 0.3.11 to 0.3.15
@@ -8,4 +8,4 @@ (function (exportName) { | ||
* zswang (http://weibo.com/zswang) | ||
* @version 0.3.11 | ||
* @date 2015-11-04 | ||
* @version 0.3.15 | ||
* @date 2015-11-05 | ||
*/ | ||
@@ -42,3 +42,3 @@ function createSchema() { | ||
var self = this; | ||
Object.keys(options).forEach(function (key) { | ||
Object.keys(options).forEach(function(key) { | ||
self[key] = options[key]; | ||
@@ -54,3 +54,3 @@ }); | ||
*/ | ||
Schema.register = function (name, schema) { | ||
Schema.register = function(name, schema) { | ||
/*<safe>*/ | ||
@@ -99,3 +99,3 @@ if (typeof name === 'undefined') { | ||
*/ | ||
Schema.pushPattern = function (pattern) { | ||
Schema.pushPattern = function(pattern) { | ||
/*<safe>*/ | ||
@@ -114,3 +114,3 @@ if (typeof pattern !== 'function') { | ||
*/ | ||
Schema.from = function (schema) { | ||
Schema.from = function(schema) { | ||
/*<safe>*/ | ||
@@ -155,3 +155,3 @@ if (typeof schema === 'undefined') { // 无效数据 | ||
options = options || {}; | ||
Object.keys(options).forEach(function (key) { | ||
Object.keys(options).forEach(function(key) { | ||
defaultOptions[key] = options[key]; | ||
@@ -196,3 +196,3 @@ }); | ||
offsets = offsets || [0]; | ||
Object.keys(defaultOptions).forEach(function (key) { | ||
Object.keys(defaultOptions).forEach(function(key) { | ||
if (typeof options[key] === 'undefined') { | ||
@@ -223,3 +223,3 @@ options[key] = defaultOptions[key]; | ||
options = options || {}; | ||
Object.keys(defaultOptions).forEach(function (key) { | ||
Object.keys(defaultOptions).forEach(function(key) { | ||
if (typeof options[key] === 'undefined') { | ||
@@ -305,2 +305,5 @@ options[key] = defaultOptions[key]; | ||
function scan(obj) { | ||
// if (obj === null) { | ||
// return 'null'; | ||
// } | ||
if (!obj) { | ||
@@ -311,3 +314,3 @@ return obj; | ||
if (obj.namespace === 'number') { | ||
return obj.name; | ||
return "'" + obj.name + "'"; | ||
} | ||
@@ -317,15 +320,12 @@ if (obj.args) { | ||
} | ||
return obj.namespace; | ||
return "'" + obj.namespace + "'"; | ||
} | ||
if (obj.name) { | ||
return obj.name; | ||
} | ||
if (typeof obj === 'function') { | ||
obj.name = '_pack_fn' + (guid++); | ||
Schema.define(obj.name, obj); | ||
return obj.name; | ||
} | ||
if (typeof obj === 'object') { | ||
if (!obj.name) { | ||
obj.name = '_pack_fn' + (guid++); | ||
Schema.define(obj.name, obj); | ||
} | ||
} else if (typeof obj === 'object') { | ||
var result = new obj.constructor(); | ||
Object.keys(obj).forEach(function (key) { | ||
Object.keys(obj).forEach(function(key) { | ||
result[key] = scan(obj[key]); | ||
@@ -335,2 +335,8 @@ }); | ||
} | ||
if (obj.name) { | ||
return "'" + obj.name + "'"; | ||
} | ||
if (typeof obj === 'string') { | ||
return "'" + obj + "'"; | ||
} | ||
return obj; | ||
@@ -341,3 +347,3 @@ } | ||
Schema.stringify = stringify; | ||
Schema.prototype.toString = function () { | ||
Schema.prototype.toString = function() { | ||
return stringify(this); | ||
@@ -375,3 +381,3 @@ }; | ||
console.log(_.stringify(_schema)); | ||
// > 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) | ||
// > 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, { | ||
@@ -488,3 +494,3 @@ bytes: [0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89] | ||
console.log(String(_schema)); | ||
// > array(int16,2) | ||
// > array('int16',2) | ||
var value = [12337, 12851]; | ||
@@ -502,3 +508,3 @@ var buffer = jpacks.pack(_schema, value); | ||
console.log(String(_schema)); | ||
// > array(int16,int8) | ||
// > array('int16','int8') | ||
var value = [12337, 12851]; | ||
@@ -516,3 +522,3 @@ var buffer = jpacks.pack(_schema, value); | ||
console.log(String(_schema)); | ||
// > array(int16,6) | ||
// > array('int16',6) | ||
var value = [12337, 12851]; | ||
@@ -542,7 +548,2 @@ var buffer = jpacks.pack(_schema, value); | ||
countSchema = Schema.from(count); | ||
/*<safe>*/ | ||
if (countSchema.namespace !== 'number') { | ||
throw new Error('Parameter "count" is not a numeric type.'); | ||
} | ||
/*</safe>*/ | ||
} | ||
@@ -620,3 +621,3 @@ return new Schema({ | ||
console.log(String(_schema)); | ||
// > array(uint8,6) | ||
// > array('uint8',6) | ||
var value = [0, 1, 2, 3, 4, 5]; | ||
@@ -646,3 +647,3 @@ var buffer = jpacks.pack(_schema, value); | ||
console.log(_.stringify(_schema)); | ||
// > object([string(uint8),uint16]) | ||
// > object([string('uint8'),'uint16']) | ||
var buffer = _.pack(_schema, ['zswang', 1978]); | ||
@@ -662,3 +663,3 @@ console.log(buffer.join(' ')); | ||
console.log(_.stringify(_schema)); | ||
// > object({namespace:string,args:{0:uint8}}) | ||
// > object({name:string('uint8'),year:'uint16'}) | ||
var buffer = _.pack(_schema, { | ||
@@ -739,3 +740,3 @@ name: 'zswang', | ||
console.log(_.stringify(_schema)); | ||
// > union({length:uint8,content:string(uint8)},20) | ||
// > union({length:'uint8',content:string('uint8')},20) | ||
var buffer = _.pack(_schema, { | ||
@@ -807,3 +808,3 @@ content: '0123456789' | ||
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'); | ||
@@ -827,3 +828,3 @@ console.log(buffer.join(' ')); | ||
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'); | ||
@@ -846,3 +847,3 @@ console.log(buffer.join(' ')); | ||
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); | ||
@@ -994,4 +995,2 @@ console.log(buffer.join(' ')); | ||
// > 你好世界!Hello | ||
'''<example>''' | ||
'''<example>''' | ||
* @example stringCreator():dynamic | ||
@@ -1115,3 +1114,3 @@ var _ = jpacks; | ||
console.log(_.stringify(_schema)); | ||
// > array(cstring(true),uint8) | ||
// > array(cstring(true),'uint8') | ||
var buffer = _.pack(_schema, ['abc', 'defghijk', 'g']); | ||
@@ -1180,3 +1179,3 @@ console.log(buffer.join(' ')); | ||
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, { | ||
@@ -1238,3 +1237,3 @@ type: 'name', | ||
'''<example>''' | ||
* @example dependCreator() | ||
* @example dependCreator():base | ||
```js | ||
@@ -1249,3 +1248,3 @@ var _ = jpacks; | ||
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, { | ||
@@ -1323,3 +1322,3 @@ length1: 2, | ||
console.log(_.stringify(_schema)); | ||
// > parse(_xor,_xor,float64,8) | ||
// > parse('_xor','_xor','float64',8) | ||
var buffer = _.pack(_schema, 2.94296650666094e+189); | ||
@@ -1361,2 +1360,103 @@ console.log(buffer.join(' ')); | ||
Schema.register('parse', parse); | ||
/** | ||
* 定义一个虚拟结构 | ||
* | ||
* @param {number|string} operator | ||
* @param {object} value 数据结构 | ||
'''<example>''' | ||
* @example virtualCreator:number | ||
```js | ||
var _ = jpacks; | ||
var _schema = _.object({ | ||
f1: 'uint16', | ||
v1: _.depend('f1', _.virtual(-4)) | ||
}); | ||
console.log(_.stringify(_schema)) | ||
// > object({f1:'uint16',v1:depend('f1',virtual(-4))}) | ||
var buffer = _.pack(_schema, { | ||
f1: 4 | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 4 0 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer, { littleEndian: false }))); | ||
// > {"f1":1024,"v1":1020} | ||
``` | ||
* @example virtualCreator:string | ||
```js | ||
var _ = jpacks; | ||
var _schema = _.object({ | ||
name: _.shortString, | ||
welcome: _.depend('name', _.virtual('Hello ')) | ||
}); | ||
console.log(_.stringify(_schema)) | ||
// > object({name:string('uint8'),welcome:depend('name',virtual('Hello '))}) | ||
var buffer = _.pack(_schema, { | ||
name: 'World!' | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 6 87 111 114 108 100 33 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer, { littleEndian: false }))); | ||
// > {"name":"World!","welcome":"Hello World!"} | ||
``` | ||
* @example virtualCreator:depend | ||
```js | ||
var _ = jpacks; | ||
var _schema = _.object({ | ||
name: _.shortString, | ||
welcome: _.depend('name', _.cases([ | ||
['zswang', _.depend('name', _.virtual('Hello '))], | ||
['wang', _.depend('name', _.virtual('Hi '))] | ||
])) | ||
}); | ||
console.log(_.stringify(_schema)) | ||
// > object({name:string('uint8'),welcome:depend('name',cases([['zswang',depend('name',virtual('Hello '))],['wang',depend('name',virtual('Hi '))]]))}) | ||
var buffer = _.pack(_schema, { | ||
name: 'zswang' | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 6 122 115 119 97 110 103 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer, { littleEndian: false }))); | ||
// > {"name":"zswang","welcome":"Hello zswang"} | ||
var buffer = _.pack(_schema, { | ||
name: 'wang' | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 4 119 97 110 103 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer, { littleEndian: false }))); | ||
// > {"name":"wang","welcome":"Hi wang"} | ||
``` | ||
'''</example>''' | ||
*/ | ||
function virtualCreator(operator, value) { | ||
return new Schema({ | ||
unpack: function _unpack() { | ||
if (/string|number/.test(typeof operator)) { | ||
return operator + value; | ||
} | ||
if (typeof operator === 'function') { | ||
return operator(value); | ||
} | ||
return value; | ||
}, | ||
pack: function _pack() {}, | ||
args: arguments, | ||
namespace: 'virtual' | ||
}); | ||
} | ||
var virtual = Schema.together(virtualCreator, function (fn, args) { | ||
fn.namespace = 'virtual'; | ||
fn.args = args; | ||
}); | ||
Schema.register('virtual', virtual); | ||
Schema.pushPattern(function _virtualPattern(schema) { | ||
if (typeof schema === 'virtual') { | ||
if (schema instanceof Schema) { | ||
return; | ||
} | ||
if (schema instanceof Array) { | ||
return; | ||
} | ||
return virtual(schema); | ||
} | ||
}); | ||
return Schema; | ||
@@ -1363,0 +1463,0 @@ } |
183
jpacks.js
@@ -8,4 +8,4 @@ (function (exportName) { | ||
* zswang (http://weibo.com/zswang) | ||
* @version 0.3.11 | ||
* @date 2015-11-04 | ||
* @version 0.3.15 | ||
* @date 2015-11-05 | ||
*/ | ||
@@ -42,3 +42,3 @@ function createSchema() { | ||
var self = this; | ||
Object.keys(options).forEach(function (key) { | ||
Object.keys(options).forEach(function(key) { | ||
self[key] = options[key]; | ||
@@ -54,3 +54,3 @@ }); | ||
*/ | ||
Schema.register = function (name, schema) { | ||
Schema.register = function(name, schema) { | ||
schemas[name] = schema; | ||
@@ -94,3 +94,3 @@ if (!Schema[name]) { // 避免覆盖系统方法 | ||
*/ | ||
Schema.pushPattern = function (pattern) { | ||
Schema.pushPattern = function(pattern) { | ||
schemaPatterns.push(pattern); | ||
@@ -104,3 +104,3 @@ }; | ||
*/ | ||
Schema.from = function (schema) { | ||
Schema.from = function(schema) { | ||
if (schema instanceof Schema) { | ||
@@ -140,3 +140,3 @@ return schema; | ||
options = options || {}; | ||
Object.keys(options).forEach(function (key) { | ||
Object.keys(options).forEach(function(key) { | ||
defaultOptions[key] = options[key]; | ||
@@ -178,3 +178,3 @@ }); | ||
offsets = offsets || [0]; | ||
Object.keys(defaultOptions).forEach(function (key) { | ||
Object.keys(defaultOptions).forEach(function(key) { | ||
if (typeof options[key] === 'undefined') { | ||
@@ -202,3 +202,3 @@ options[key] = defaultOptions[key]; | ||
options = options || {}; | ||
Object.keys(defaultOptions).forEach(function (key) { | ||
Object.keys(defaultOptions).forEach(function(key) { | ||
if (typeof options[key] === 'undefined') { | ||
@@ -284,2 +284,5 @@ options[key] = defaultOptions[key]; | ||
function scan(obj) { | ||
// if (obj === null) { | ||
// return 'null'; | ||
// } | ||
if (!obj) { | ||
@@ -290,3 +293,3 @@ return obj; | ||
if (obj.namespace === 'number') { | ||
return obj.name; | ||
return "'" + obj.name + "'"; | ||
} | ||
@@ -296,15 +299,12 @@ if (obj.args) { | ||
} | ||
return obj.namespace; | ||
return "'" + obj.namespace + "'"; | ||
} | ||
if (obj.name) { | ||
return obj.name; | ||
} | ||
if (typeof obj === 'function') { | ||
obj.name = '_pack_fn' + (guid++); | ||
Schema.define(obj.name, obj); | ||
return obj.name; | ||
} | ||
if (typeof obj === 'object') { | ||
if (!obj.name) { | ||
obj.name = '_pack_fn' + (guid++); | ||
Schema.define(obj.name, obj); | ||
} | ||
} else if (typeof obj === 'object') { | ||
var result = new obj.constructor(); | ||
Object.keys(obj).forEach(function (key) { | ||
Object.keys(obj).forEach(function(key) { | ||
result[key] = scan(obj[key]); | ||
@@ -314,2 +314,8 @@ }); | ||
} | ||
if (obj.name) { | ||
return "'" + obj.name + "'"; | ||
} | ||
if (typeof obj === 'string') { | ||
return "'" + obj + "'"; | ||
} | ||
return obj; | ||
@@ -320,3 +326,3 @@ } | ||
Schema.stringify = stringify; | ||
Schema.prototype.toString = function () { | ||
Schema.prototype.toString = function() { | ||
return stringify(this); | ||
@@ -354,3 +360,3 @@ }; | ||
console.log(_.stringify(_schema)); | ||
// > 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) | ||
// > 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, { | ||
@@ -467,3 +473,3 @@ bytes: [0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89] | ||
console.log(String(_schema)); | ||
// > array(int16,2) | ||
// > array('int16',2) | ||
var value = [12337, 12851]; | ||
@@ -481,3 +487,3 @@ var buffer = jpacks.pack(_schema, value); | ||
console.log(String(_schema)); | ||
// > array(int16,int8) | ||
// > array('int16','int8') | ||
var value = [12337, 12851]; | ||
@@ -495,3 +501,3 @@ var buffer = jpacks.pack(_schema, value); | ||
console.log(String(_schema)); | ||
// > array(int16,6) | ||
// > array('int16',6) | ||
var value = [12337, 12851]; | ||
@@ -585,3 +591,3 @@ var buffer = jpacks.pack(_schema, value); | ||
console.log(String(_schema)); | ||
// > array(uint8,6) | ||
// > array('uint8',6) | ||
var value = [0, 1, 2, 3, 4, 5]; | ||
@@ -611,3 +617,3 @@ var buffer = jpacks.pack(_schema, value); | ||
console.log(_.stringify(_schema)); | ||
// > object([string(uint8),uint16]) | ||
// > object([string('uint8'),'uint16']) | ||
var buffer = _.pack(_schema, ['zswang', 1978]); | ||
@@ -627,3 +633,3 @@ console.log(buffer.join(' ')); | ||
console.log(_.stringify(_schema)); | ||
// > object({namespace:string,args:{0:uint8}}) | ||
// > object({name:string('uint8'),year:'uint16'}) | ||
var buffer = _.pack(_schema, { | ||
@@ -699,3 +705,3 @@ name: 'zswang', | ||
console.log(_.stringify(_schema)); | ||
// > union({length:uint8,content:string(uint8)},20) | ||
// > union({length:'uint8',content:string('uint8')},20) | ||
var buffer = _.pack(_schema, { | ||
@@ -759,3 +765,3 @@ content: '0123456789' | ||
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'); | ||
@@ -779,3 +785,3 @@ console.log(buffer.join(' ')); | ||
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'); | ||
@@ -798,3 +804,3 @@ console.log(buffer.join(' ')); | ||
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); | ||
@@ -935,4 +941,2 @@ console.log(buffer.join(' ')); | ||
// > 你好世界!Hello | ||
'''<example>''' | ||
'''<example>''' | ||
* @example stringCreator():dynamic | ||
@@ -1056,3 +1060,3 @@ var _ = jpacks; | ||
console.log(_.stringify(_schema)); | ||
// > array(cstring(true),uint8) | ||
// > array(cstring(true),'uint8') | ||
var buffer = _.pack(_schema, ['abc', 'defghijk', 'g']); | ||
@@ -1121,3 +1125,3 @@ console.log(buffer.join(' ')); | ||
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, { | ||
@@ -1165,3 +1169,3 @@ type: 'name', | ||
'''<example>''' | ||
* @example dependCreator() | ||
* @example dependCreator():base | ||
```js | ||
@@ -1176,3 +1180,3 @@ var _ = jpacks; | ||
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, { | ||
@@ -1242,3 +1246,3 @@ length1: 2, | ||
console.log(_.stringify(_schema)); | ||
// > parse(_xor,_xor,float64,8) | ||
// > parse('_xor','_xor','float64',8) | ||
var buffer = _.pack(_schema, 2.94296650666094e+189); | ||
@@ -1272,2 +1276,103 @@ console.log(buffer.join(' ')); | ||
Schema.register('parse', parse); | ||
/** | ||
* 定义一个虚拟结构 | ||
* | ||
* @param {number|string} operator | ||
* @param {object} value 数据结构 | ||
'''<example>''' | ||
* @example virtualCreator:number | ||
```js | ||
var _ = jpacks; | ||
var _schema = _.object({ | ||
f1: 'uint16', | ||
v1: _.depend('f1', _.virtual(-4)) | ||
}); | ||
console.log(_.stringify(_schema)) | ||
// > object({f1:'uint16',v1:depend('f1',virtual(-4))}) | ||
var buffer = _.pack(_schema, { | ||
f1: 4 | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 4 0 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer, { littleEndian: false }))); | ||
// > {"f1":1024,"v1":1020} | ||
``` | ||
* @example virtualCreator:string | ||
```js | ||
var _ = jpacks; | ||
var _schema = _.object({ | ||
name: _.shortString, | ||
welcome: _.depend('name', _.virtual('Hello ')) | ||
}); | ||
console.log(_.stringify(_schema)) | ||
// > object({name:string('uint8'),welcome:depend('name',virtual('Hello '))}) | ||
var buffer = _.pack(_schema, { | ||
name: 'World!' | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 6 87 111 114 108 100 33 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer, { littleEndian: false }))); | ||
// > {"name":"World!","welcome":"Hello World!"} | ||
``` | ||
* @example virtualCreator:depend | ||
```js | ||
var _ = jpacks; | ||
var _schema = _.object({ | ||
name: _.shortString, | ||
welcome: _.depend('name', _.cases([ | ||
['zswang', _.depend('name', _.virtual('Hello '))], | ||
['wang', _.depend('name', _.virtual('Hi '))] | ||
])) | ||
}); | ||
console.log(_.stringify(_schema)) | ||
// > object({name:string('uint8'),welcome:depend('name',cases([['zswang',depend('name',virtual('Hello '))],['wang',depend('name',virtual('Hi '))]]))}) | ||
var buffer = _.pack(_schema, { | ||
name: 'zswang' | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 6 122 115 119 97 110 103 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer, { littleEndian: false }))); | ||
// > {"name":"zswang","welcome":"Hello zswang"} | ||
var buffer = _.pack(_schema, { | ||
name: 'wang' | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 4 119 97 110 103 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer, { littleEndian: false }))); | ||
// > {"name":"wang","welcome":"Hi wang"} | ||
``` | ||
'''</example>''' | ||
*/ | ||
function virtualCreator(operator, value) { | ||
return new Schema({ | ||
unpack: function _unpack() { | ||
if (/string|number/.test(typeof operator)) { | ||
return operator + value; | ||
} | ||
if (typeof operator === 'function') { | ||
return operator(value); | ||
} | ||
return value; | ||
}, | ||
pack: function _pack() {}, | ||
args: arguments, | ||
namespace: 'virtual' | ||
}); | ||
} | ||
var virtual = Schema.together(virtualCreator, function (fn, args) { | ||
fn.namespace = 'virtual'; | ||
fn.args = args; | ||
}); | ||
Schema.register('virtual', virtual); | ||
Schema.pushPattern(function _virtualPattern(schema) { | ||
if (typeof schema === 'virtual') { | ||
if (schema instanceof Schema) { | ||
return; | ||
} | ||
if (schema instanceof Array) { | ||
return; | ||
} | ||
return virtual(schema); | ||
} | ||
}); | ||
return Schema; | ||
@@ -1274,0 +1379,0 @@ } |
@@ -5,5 +5,5 @@ { | ||
"description": "Binary data packing and unpacking.", | ||
"version": "0.3.11", | ||
"version": "0.3.15", | ||
"homepage": "http://github.com/zswang/jpacks", | ||
"main": "src/jpacks.js", | ||
"main": "jpacks.js", | ||
"author": { | ||
@@ -10,0 +10,0 @@ "name": "zswang", |
@@ -29,3 +29,3 @@ var jints = require('jints'); | ||
console.log(_.stringify(_schema)) | ||
// > uint64 | ||
// > 'uint64' | ||
@@ -45,3 +45,3 @@ var buffer = _.pack(_schema, '1609531171697315243'); | ||
console.log(_.stringify(_schema)) | ||
// > uint64 | ||
// > 'uint64' | ||
@@ -61,3 +61,3 @@ var buffer = _.pack(_schema, 171697315); | ||
console.log(_.stringify(_schema)) | ||
// > uint64 | ||
// > 'uint64' | ||
@@ -77,3 +77,3 @@ var buffer = _.pack(_schema, 171697315, { littleEndian: false }); | ||
console.log(_.stringify(_schema)) | ||
// > int64 | ||
// > 'int64' | ||
@@ -101,3 +101,3 @@ var buffer = _.pack(_schema, '-1'); | ||
console.log(_.stringify(_schema)) | ||
// > int64 | ||
// > 'int64' | ||
@@ -104,0 +104,0 @@ var buffer = _.pack(_schema, -2, { littleEndian: false }); |
@@ -64,3 +64,3 @@ var protobufjs = require('protobufjs'); | ||
function jsonify(messager, json, options) { | ||
if (!json) { | ||
if (!json || !messager) { | ||
return; | ||
@@ -115,4 +115,4 @@ } | ||
* @param {string} filename 文件名 | ||
* @param {string} packagename 包名 | ||
* @param {string} messagename 信息名 | ||
* @param {string} messagepath 消息体路径 | ||
* @param {string|number|boolean} size 内容大小,为 true 则为自动大小 | ||
'''<example>''' | ||
@@ -127,3 +127,3 @@ * @example protobufCreator():base | ||
console.log(_.stringify(_schema)) | ||
// > array(protobuf(test/protoify/json.proto,js.Value,uint16),int8) | ||
// > array(protobuf('test/protoify/json.proto','js.Value','uint16'),'int8') | ||
@@ -161,3 +161,3 @@ var buffer = _.pack(_schema, [{ | ||
console.log(_.stringify(_schema)) | ||
// > array(protobuf(test/protoify/bigint.proto,bigint.Value,uint16),int8) | ||
// > array(protobuf('test/protoify/bigint.proto','bigint.Value','uint16'),'int8') | ||
@@ -184,3 +184,3 @@ var buffer = _.pack(_schema, [{ | ||
console.log(_.stringify(_schema)) | ||
// > array(protobuf(test/protoify/string.proto,str.Value,uint16),int8) | ||
// > array(protobuf('test/protoify/string.proto','str.Value','uint16'),'int8') | ||
@@ -212,4 +212,4 @@ _.setDefaultOptions({ | ||
var rs = messager.decode(bytes); | ||
var calculate = rs.calculate(); | ||
if (calculate <= 0) { | ||
var byteSize = rs.calculate(); | ||
if (byteSize <= 0) { | ||
return null; | ||
@@ -224,4 +224,4 @@ } | ||
var message = protoify(messager, value, options); | ||
var uint8Array = new Uint8Array(message.toArrayBuffer()); | ||
Schema.pack(Schema.bytes(size), uint8Array, options, buffer); | ||
var bytes = new Uint8Array(message.toArrayBuffer()); | ||
Schema.pack(Schema.bytes(size), bytes, options, buffer); | ||
}, | ||
@@ -228,0 +228,0 @@ namespace: 'protobuf', |
@@ -19,3 +19,3 @@ var zlib = require('zlib'); | ||
console.log(_.stringify(_schema)) | ||
// > object({type:uint8,data:parse(zlib.gzipSync,zlib.gunzipSync,string(uint8),uint16)}) | ||
// > object({type:'uint8',data:parse('zlib.gzipSync','zlib.gunzipSync',string('uint8'),'uint16')}) | ||
@@ -27,4 +27,6 @@ var buffer = _.pack(_schema, { | ||
console.log(buffer.join(' ')); | ||
// > 2 42 0 31 139 8 0 0 0 0 0 0 3 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(buffer.slice(14).join(' ')); | ||
// windows: 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 | ||
// linux: 2 42 0 31 139 8 0 0 0 0 0 0 3 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 | ||
// > 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 | ||
@@ -68,3 +70,3 @@ console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
console.log(_.stringify(_schema)) | ||
// > object({type:uint8,data:parse(zlib.deflateSync,zlib.inflateSync,string(uint8),uint16)}) | ||
// > object({type:'uint8',data:parse('zlib.deflateSync','zlib.inflateSync',string('uint8'),'uint16')}) | ||
@@ -71,0 +73,0 @@ var buffer = _.pack(_schema, { |
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
102673
3261