Comparing version 0.4.1 to 0.4.5
@@ -8,4 +8,4 @@ (function (exportName) { | ||
* zswang (http://weibo.com/zswang) | ||
* @version 0.4.1 | ||
* @date 2015-11-07 | ||
* @version 0.4.5 | ||
* @date 2015-11-08 | ||
*/ | ||
@@ -294,3 +294,2 @@ function createSchema() { | ||
Schema.together = together; | ||
var guid = 0; | ||
/** | ||
@@ -327,4 +326,4 @@ * 获取对象的结构表达式 | ||
if (!obj.name) { | ||
obj.name = '_pack_fn' + (guid++); | ||
Schema.def(obj.name, obj); | ||
obj.namespace = '$fn'; | ||
return obj.namespace; | ||
} | ||
@@ -616,5 +615,5 @@ } else if (typeof obj === 'object') { | ||
if (itemSchema.array && (options.littleEndian || itemSchema.size === 1)) { | ||
var size = length === null ? buffer.byteLength : itemSchema.size * length; | ||
var offset = offsets[0]; | ||
var size = length === null ? buffer.byteLength - offset : itemSchema.size * length; | ||
/* TypeArray littleEndian is true */ | ||
var offset = offsets[0]; | ||
var arrayBuffer = new ArrayBuffer(size); | ||
@@ -624,3 +623,3 @@ var typeArray = new itemSchema.array(arrayBuffer); | ||
uint8Array.set( | ||
new Uint8Array(buffer, offset, Math.min(size, buffer.byteLength)) | ||
new Uint8Array(buffer, offset, Math.min(size, buffer.byteLength - offset)) | ||
); | ||
@@ -785,4 +784,9 @@ [].push.apply(result, typeArray); | ||
var $scope = options.$scope; | ||
options.$scope = result; | ||
options.$scope = { | ||
target: result, | ||
offsets: new objectSchema.constructor(), | ||
schema: objectSchema | ||
}; | ||
keys.forEach(function (key) { | ||
options.$scope.offsets[key] = offsets[0]; | ||
result[key] = Schema.unpack(objectSchema[key], buffer, options, offsets); | ||
@@ -795,4 +799,9 @@ }); | ||
var $scope = options.$scope; | ||
options.$scope = value; | ||
options.$scope = { | ||
target: value, | ||
offsets: new objectSchema.constructor(), | ||
schema: objectSchema | ||
}; | ||
keys.forEach(function (key) { | ||
options.$scope.offsets[key] = buffer.length; | ||
Schema.pack(objectSchema[key], value[key], options, buffer); | ||
@@ -1266,6 +1275,6 @@ }); | ||
* | ||
* @param {Array of array} patterns 数组第一元素表示命中条件,第二位类型 | ||
* @param {Array of array|Function} patterns 数组第一元素表示命中条件,第二位类型 | ||
* @return {Schema} 返回条件类型 | ||
'''<example>''' | ||
* @example casesCreator | ||
* @example casesCreator():base | ||
```js | ||
@@ -1299,26 +1308,58 @@ var _ = jpacks; | ||
``` | ||
* @example casesCreator():function | ||
```js | ||
var _ = jpacks; | ||
var _schema = { | ||
type: _.shortString, | ||
data: _.depend('type', _.cases(function(type) { | ||
switch (type) { | ||
case 'age': | ||
return _.byte; | ||
case 'name': | ||
return _.shortString; | ||
} | ||
return _.bytes(null); | ||
})) | ||
}; | ||
console.log(_.stringify(_schema)); | ||
// > {type:string('uint8'),data:depend('type',cases($fn))} | ||
var buffer = _.pack(_schema, { | ||
type: 'name', | ||
data: 'tom' | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 4 110 97 109 101 3 116 111 109 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// > {"type":"name","data":"tom"} | ||
var buffer = _.pack(_schema, { | ||
type: 'age', | ||
data: 23 | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 3 97 103 101 23 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// > {"type":"age","data":23} | ||
var buffer = _.pack(_schema, { | ||
type: 'other', | ||
data: [1, 2, 3, 4, 5] | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 5 111 116 104 101 114 1 2 3 4 5 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// > {"type":"other","data":[1,2,3,4,5]} | ||
``` | ||
'''</example>''' | ||
*/ | ||
function casesCreator(patterns, value) { | ||
/*<safe>*/ | ||
if (typeof patterns !== 'object') { | ||
throw new Error('Parameter "patterns" must be a object type.'); | ||
} | ||
if (patterns instanceof Schema) { | ||
throw new Error('Parameter "patterns" cannot be a Schema object.'); | ||
} | ||
if (!(patterns instanceof Array)) { | ||
throw new Error('Parameter "patterns" must be a array.'); | ||
} | ||
if (typeof value === 'undefined') { | ||
throw new Error('Parameter "value" is undefined.'); | ||
} | ||
/*</safe>*/ | ||
for (var i = 0; i < patterns.length; i++) { | ||
if (patterns[i][0] === value) { | ||
return patterns[i][1]; | ||
if (typeof patterns === 'function') { | ||
return patterns(value); | ||
} else if (patterns instanceof Array) { | ||
for (var i = 0; i < patterns.length; i++) { | ||
if (patterns[i][0] === value) { | ||
return patterns[i][1]; | ||
} | ||
} | ||
} | ||
} | ||
var cases = Schema.together(casesCreator, function (fn, args) { | ||
var cases = Schema.together(casesCreator, function(fn, args) { | ||
fn.namespace = 'cases'; | ||
@@ -1374,16 +1415,22 @@ fn.args = args; | ||
unpack: function _unpack(buffer, options, offsets) { | ||
/*<safe>*/ | ||
if (!options.$scope) { | ||
throw new Error('Unpack must running in object.'); | ||
} | ||
var fieldValue = options.$scope[field]; | ||
/*</safe>*/ | ||
var fieldValue = options.$scope.target[field]; | ||
/*<safe>*/ | ||
if (typeof fieldValue === 'undefined') { | ||
throw new Error('Field "' + field + '" is undefined.'); | ||
} | ||
/*</safe>*/ | ||
return Schema.unpack(schemaCreator(fieldValue), buffer, options, offsets); | ||
}, | ||
pack: function _pack(value, options, buffer) { | ||
var fieldValue = options.$scope[field]; | ||
var fieldValue = options.$scope.target[field]; | ||
/*<safe>*/ | ||
if (typeof fieldValue === 'undefined') { | ||
throw new Error('Field "' + field + '" is undefined.'); | ||
} | ||
/*</safe>*/ | ||
Schema.pack(schemaCreator(fieldValue), value, options, buffer); | ||
@@ -1550,2 +1597,86 @@ }, | ||
Schema.register('virtual', virtual); | ||
/** | ||
* 声明映射文件 | ||
* | ||
* @param {string} field 字段名,即:元素的个数 | ||
* @param {string|Schema} item 元素类型 | ||
* @return {Schema|Function} 返回数据结构 | ||
'''<example>''' | ||
* @example mapCreator():base | ||
```js | ||
var _ = jpacks; | ||
var _schema = { | ||
size1: 'uint16', | ||
size2: 'uint16', | ||
data1: _.map('size1', 'uint8'), | ||
data2: _.map('size2', 'uint8') | ||
}; | ||
console.log(_.stringify(_schema)); | ||
// > {size1:'uint16',size2:'uint16',data1:map('size1','uint8'),data2:map('size2','uint8')} | ||
var buffer = jpacks.pack(_schema, { | ||
data1: [1, 2, 3, 4], | ||
data2: [1, 2, 3, 4, 5, 6, 7, 8], | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 4 0 8 0 1 2 3 4 1 2 3 4 5 6 7 8 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// > {"size1":4,"size2":8,"data1":[1,2,3,4],"data2":[1,2,3,4,5,6,7,8]} | ||
``` | ||
'''</example>''' | ||
*/ | ||
function mapCreator(field, item) { | ||
/*<safe>*/ | ||
if (typeof field !== 'string') { | ||
throw new Error('Parameter "field" must be a string.'); | ||
} | ||
/*</safe>*/ | ||
/*<safe>*/ | ||
if (typeof item === 'undefined') { | ||
throw new Error('Parameter "item" is undefined.'); | ||
} | ||
/*</safe>*/ | ||
return new Schema({ | ||
unpack: function _unpack(buffer, options, offsets) { | ||
/*<safe>*/ | ||
if (!options.$scope) { | ||
throw new Error('Unpack must running in object.'); | ||
} | ||
/*</safe>*/ | ||
var fieldValue = options.$scope.target[field]; | ||
var itemSchema = Schema.from(item); | ||
/*<safe>*/ | ||
if (typeof fieldValue === 'undefined') { | ||
throw new Error('Field "' + field + '" is undefined.'); | ||
} | ||
/*</safe>*/ | ||
return Schema.unpack(Schema.array(itemSchema, fieldValue), buffer, options, offsets); | ||
}, | ||
pack: function _pack(value, options, buffer) { | ||
var fieldSchema = options.$scope.schema[field]; | ||
/*<safe>*/ | ||
if (typeof fieldSchema === 'undefined') { | ||
throw new Error('Field schema "' + field + '" is undefined.'); | ||
} | ||
/*</safe>*/ | ||
var itemSchema = Schema.from(item); | ||
var bytes = Schema.pack(fieldSchema, value.length, options); | ||
var offset = options.$scope.offsets[field]; | ||
for (var i = 0; i < bytes.length; i++) { | ||
buffer[offset + i] = bytes[i]; | ||
} | ||
Schema.pack(Schema.array(itemSchema, null), value, options, buffer); | ||
}, | ||
namespace: 'map', | ||
args: arguments | ||
}); | ||
} | ||
var map = Schema.together(mapCreator, function (fn, args) { | ||
fn.namespace = 'map'; | ||
fn.args = args; | ||
}); | ||
Schema.register('map', map); | ||
function mapArray(field, itemSchema) { | ||
return map(field, Schema.array(itemSchema)); | ||
} | ||
Schema.register('mapArray', mapArray); | ||
return Schema; | ||
@@ -1552,0 +1683,0 @@ } |
159
jpacks.js
@@ -8,4 +8,4 @@ (function (exportName) { | ||
* zswang (http://weibo.com/zswang) | ||
* @version 0.4.1 | ||
* @date 2015-11-07 | ||
* @version 0.4.5 | ||
* @date 2015-11-08 | ||
*/ | ||
@@ -263,3 +263,2 @@ function createSchema() { | ||
Schema.together = together; | ||
var guid = 0; | ||
/** | ||
@@ -296,4 +295,4 @@ * 获取对象的结构表达式 | ||
if (!obj.name) { | ||
obj.name = '_pack_fn' + (guid++); | ||
Schema.def(obj.name, obj); | ||
obj.namespace = '$fn'; | ||
return obj.namespace; | ||
} | ||
@@ -577,5 +576,5 @@ } else if (typeof obj === 'object') { | ||
if (itemSchema.array && (options.littleEndian || itemSchema.size === 1)) { | ||
var size = length === null ? buffer.byteLength : itemSchema.size * length; | ||
var offset = offsets[0]; | ||
var size = length === null ? buffer.byteLength - offset : itemSchema.size * length; | ||
/* TypeArray littleEndian is true */ | ||
var offset = offsets[0]; | ||
var arrayBuffer = new ArrayBuffer(size); | ||
@@ -585,3 +584,3 @@ var typeArray = new itemSchema.array(arrayBuffer); | ||
uint8Array.set( | ||
new Uint8Array(buffer, offset, Math.min(size, buffer.byteLength)) | ||
new Uint8Array(buffer, offset, Math.min(size, buffer.byteLength - offset)) | ||
); | ||
@@ -741,4 +740,9 @@ [].push.apply(result, typeArray); | ||
var $scope = options.$scope; | ||
options.$scope = result; | ||
options.$scope = { | ||
target: result, | ||
offsets: new objectSchema.constructor(), | ||
schema: objectSchema | ||
}; | ||
keys.forEach(function (key) { | ||
options.$scope.offsets[key] = offsets[0]; | ||
result[key] = Schema.unpack(objectSchema[key], buffer, options, offsets); | ||
@@ -751,4 +755,9 @@ }); | ||
var $scope = options.$scope; | ||
options.$scope = value; | ||
options.$scope = { | ||
target: value, | ||
offsets: new objectSchema.constructor(), | ||
schema: objectSchema | ||
}; | ||
keys.forEach(function (key) { | ||
options.$scope.offsets[key] = buffer.length; | ||
Schema.pack(objectSchema[key], value[key], options, buffer); | ||
@@ -1203,6 +1212,6 @@ }); | ||
* | ||
* @param {Array of array} patterns 数组第一元素表示命中条件,第二位类型 | ||
* @param {Array of array|Function} patterns 数组第一元素表示命中条件,第二位类型 | ||
* @return {Schema} 返回条件类型 | ||
'''<example>''' | ||
* @example casesCreator | ||
* @example casesCreator():base | ||
```js | ||
@@ -1236,12 +1245,58 @@ var _ = jpacks; | ||
``` | ||
* @example casesCreator():function | ||
```js | ||
var _ = jpacks; | ||
var _schema = { | ||
type: _.shortString, | ||
data: _.depend('type', _.cases(function(type) { | ||
switch (type) { | ||
case 'age': | ||
return _.byte; | ||
case 'name': | ||
return _.shortString; | ||
} | ||
return _.bytes(null); | ||
})) | ||
}; | ||
console.log(_.stringify(_schema)); | ||
// > {type:string('uint8'),data:depend('type',cases($fn))} | ||
var buffer = _.pack(_schema, { | ||
type: 'name', | ||
data: 'tom' | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 4 110 97 109 101 3 116 111 109 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// > {"type":"name","data":"tom"} | ||
var buffer = _.pack(_schema, { | ||
type: 'age', | ||
data: 23 | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 3 97 103 101 23 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// > {"type":"age","data":23} | ||
var buffer = _.pack(_schema, { | ||
type: 'other', | ||
data: [1, 2, 3, 4, 5] | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 5 111 116 104 101 114 1 2 3 4 5 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// > {"type":"other","data":[1,2,3,4,5]} | ||
``` | ||
'''</example>''' | ||
*/ | ||
function casesCreator(patterns, value) { | ||
for (var i = 0; i < patterns.length; i++) { | ||
if (patterns[i][0] === value) { | ||
return patterns[i][1]; | ||
if (typeof patterns === 'function') { | ||
return patterns(value); | ||
} else if (patterns instanceof Array) { | ||
for (var i = 0; i < patterns.length; i++) { | ||
if (patterns[i][0] === value) { | ||
return patterns[i][1]; | ||
} | ||
} | ||
} | ||
} | ||
var cases = Schema.together(casesCreator, function (fn, args) { | ||
var cases = Schema.together(casesCreator, function(fn, args) { | ||
fn.namespace = 'cases'; | ||
@@ -1289,16 +1344,7 @@ fn.args = args; | ||
unpack: function _unpack(buffer, options, offsets) { | ||
if (!options.$scope) { | ||
throw new Error('Unpack must running in object.'); | ||
} | ||
var fieldValue = options.$scope[field]; | ||
if (typeof fieldValue === 'undefined') { | ||
throw new Error('Field "' + field + '" is undefined.'); | ||
} | ||
var fieldValue = options.$scope.target[field]; | ||
return Schema.unpack(schemaCreator(fieldValue), buffer, options, offsets); | ||
}, | ||
pack: function _pack(value, options, buffer) { | ||
var fieldValue = options.$scope[field]; | ||
if (typeof fieldValue === 'undefined') { | ||
throw new Error('Field "' + field + '" is undefined.'); | ||
} | ||
var fieldValue = options.$scope.target[field]; | ||
Schema.pack(schemaCreator(fieldValue), value, options, buffer); | ||
@@ -1457,2 +1503,61 @@ }, | ||
Schema.register('virtual', virtual); | ||
/** | ||
* 声明映射文件 | ||
* | ||
* @param {string} field 字段名,即:元素的个数 | ||
* @param {string|Schema} item 元素类型 | ||
* @return {Schema|Function} 返回数据结构 | ||
'''<example>''' | ||
* @example mapCreator():base | ||
```js | ||
var _ = jpacks; | ||
var _schema = { | ||
size1: 'uint16', | ||
size2: 'uint16', | ||
data1: _.map('size1', 'uint8'), | ||
data2: _.map('size2', 'uint8') | ||
}; | ||
console.log(_.stringify(_schema)); | ||
// > {size1:'uint16',size2:'uint16',data1:map('size1','uint8'),data2:map('size2','uint8')} | ||
var buffer = jpacks.pack(_schema, { | ||
data1: [1, 2, 3, 4], | ||
data2: [1, 2, 3, 4, 5, 6, 7, 8], | ||
}); | ||
console.log(buffer.join(' ')); | ||
// > 4 0 8 0 1 2 3 4 1 2 3 4 5 6 7 8 | ||
console.log(JSON.stringify(_.unpack(_schema, buffer))); | ||
// > {"size1":4,"size2":8,"data1":[1,2,3,4],"data2":[1,2,3,4,5,6,7,8]} | ||
``` | ||
'''</example>''' | ||
*/ | ||
function mapCreator(field, item) { | ||
return new Schema({ | ||
unpack: function _unpack(buffer, options, offsets) { | ||
var fieldValue = options.$scope.target[field]; | ||
var itemSchema = Schema.from(item); | ||
return Schema.unpack(Schema.array(itemSchema, fieldValue), buffer, options, offsets); | ||
}, | ||
pack: function _pack(value, options, buffer) { | ||
var fieldSchema = options.$scope.schema[field]; | ||
var itemSchema = Schema.from(item); | ||
var bytes = Schema.pack(fieldSchema, value.length, options); | ||
var offset = options.$scope.offsets[field]; | ||
for (var i = 0; i < bytes.length; i++) { | ||
buffer[offset + i] = bytes[i]; | ||
} | ||
Schema.pack(Schema.array(itemSchema, null), value, options, buffer); | ||
}, | ||
namespace: 'map', | ||
args: arguments | ||
}); | ||
} | ||
var map = Schema.together(mapCreator, function (fn, args) { | ||
fn.namespace = 'map'; | ||
fn.args = args; | ||
}); | ||
Schema.register('map', map); | ||
function mapArray(field, itemSchema) { | ||
return map(field, Schema.array(itemSchema)); | ||
} | ||
Schema.register('mapArray', mapArray); | ||
return Schema; | ||
@@ -1459,0 +1564,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"description": "Binary data packing and unpacking.", | ||
"version": "0.4.1", | ||
"version": "0.4.5", | ||
"homepage": "http://github.com/zswang/jpacks", | ||
@@ -50,3 +50,3 @@ "main": "jpacks.js", | ||
"dist": "npm run _update_version && npm run example && npm run _dist_dev && npm run _dist && npm run _compress && npm run test", | ||
"lint": "jshint src/**/*.js src/*.js *.json" | ||
"lint": "jshint src/**/*.js src/*.js *.json schemas-extend/*.js" | ||
}, | ||
@@ -53,0 +53,0 @@ "files": [ |
@@ -250,3 +250,4 @@ var protobufjs = require('protobufjs'); | ||
var message = protoify(messager, value, options); | ||
var bytes = new Uint8Array(message.toArrayBuffer()); | ||
var bytes = []; | ||
[].push.apply(bytes, new Uint8Array(message.toArrayBuffer())); | ||
Schema.pack(Schema.bytes(size), bytes, options, buffer); | ||
@@ -253,0 +254,0 @@ }, |
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
117412
3717