Comparing version 0.4.5 to 0.5.7
@@ -8,4 +8,4 @@ (function (exportName) { | ||
* zswang (http://weibo.com/zswang) | ||
* @version 0.4.5 | ||
* @date 2015-11-08 | ||
* @version 0.5.7 | ||
* @date 2015-11-13 | ||
*/ | ||
@@ -1018,15 +1018,6 @@ function createSchema() { | ||
function encodeUTF8(str) { | ||
return String(str).replace( | ||
/[\u0080-\u07ff]/g, | ||
function (c) { | ||
var cc = c.charCodeAt(0); | ||
return String.fromCharCode(0xc0 | cc >> 6, 0x80 | cc & 0x3f); | ||
} | ||
).replace( | ||
/[\u0800-\uffff]/g, | ||
function (c) { | ||
var cc = c.charCodeAt(0); | ||
return String.fromCharCode(0xe0 | cc >> 12, 0x80 | cc >> 6 & 0x3f, 0x80 | cc & 0x3f); | ||
} | ||
); | ||
if (/[\u0080-\uffff]/.test(str)) { | ||
return unescape(encodeURIComponent(str)); | ||
} | ||
return str; | ||
} | ||
@@ -1039,15 +1030,7 @@ /** | ||
function decodeUTF8(str) { | ||
return String(str).replace( | ||
/[\u00c0-\u00df][\u0080-\u00bf]/g, | ||
function (c) { | ||
var cc = (c.charCodeAt(0) & 0x1f) << 6 | (c.charCodeAt(1) & 0x3f); | ||
return String.fromCharCode(cc); | ||
} | ||
).replace( | ||
/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g, | ||
function (c) { | ||
var cc = (c.charCodeAt(0) & 0x0f) << 12 | (c.charCodeAt(1) & 0x3f) << 6 | (c.charCodeAt(2) & 0x3f); | ||
return String.fromCharCode(cc); | ||
} | ||
); | ||
if (/[\u00c0-\u00df][\u0080-\u00bf]/.test(str) || | ||
/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/.test(str)) { | ||
return decodeURIComponent(escape(str)); | ||
} | ||
return str; | ||
} | ||
@@ -1269,97 +1252,2 @@ /** | ||
/** | ||
* 创建条件类型 | ||
* | ||
* @param {Array of array|Function} patterns 数组第一元素表示命中条件,第二位类型 | ||
* @return {Schema} 返回条件类型 | ||
'''<example>''' | ||
* @example casesCreator():base | ||
```js | ||
var _ = jpacks; | ||
var _schema = { | ||
type: _.shortString, | ||
data: _.depend('type', _.cases([ | ||
['name', _.shortString], | ||
['age', _.byte] | ||
])) | ||
}; | ||
console.log(_.stringify(_schema)); | ||
// > {type:string('uint8'),data:depend('type',cases([['name',string('uint8')],['age','uint8']]))} | ||
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} | ||
``` | ||
* @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) { | ||
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) { | ||
fn.namespace = 'cases'; | ||
fn.args = args; | ||
}); | ||
Schema.register('cases', cases); | ||
/** | ||
* 声明字段依赖结构 | ||
@@ -1546,9 +1434,13 @@ * | ||
name: _.shortString, | ||
welcome: _.depend('name', _.cases([ | ||
['zswang', _.depend('name', _.virtual('Hello '))], | ||
['wang', _.depend('name', _.virtual('Hi '))] | ||
])) | ||
welcome: _.depend('name', function (name) { | ||
switch (name) { | ||
case 'zswang': | ||
return _.depend('name', _.virtual('Hello ')); | ||
case 'wang': | ||
return _.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 '))]]))}) | ||
// > object({name:string('uint8'),welcome:depend('name',$fn)}) | ||
var buffer = _.pack(_schema, { | ||
@@ -1555,0 +1447,0 @@ name: 'zswang' |
148
jpacks.js
@@ -8,4 +8,4 @@ (function (exportName) { | ||
* zswang (http://weibo.com/zswang) | ||
* @version 0.4.5 | ||
* @date 2015-11-08 | ||
* @version 0.5.7 | ||
* @date 2015-11-13 | ||
*/ | ||
@@ -955,15 +955,6 @@ function createSchema() { | ||
function encodeUTF8(str) { | ||
return String(str).replace( | ||
/[\u0080-\u07ff]/g, | ||
function (c) { | ||
var cc = c.charCodeAt(0); | ||
return String.fromCharCode(0xc0 | cc >> 6, 0x80 | cc & 0x3f); | ||
} | ||
).replace( | ||
/[\u0800-\uffff]/g, | ||
function (c) { | ||
var cc = c.charCodeAt(0); | ||
return String.fromCharCode(0xe0 | cc >> 12, 0x80 | cc >> 6 & 0x3f, 0x80 | cc & 0x3f); | ||
} | ||
); | ||
if (/[\u0080-\uffff]/.test(str)) { | ||
return unescape(encodeURIComponent(str)); | ||
} | ||
return str; | ||
} | ||
@@ -976,15 +967,7 @@ /** | ||
function decodeUTF8(str) { | ||
return String(str).replace( | ||
/[\u00c0-\u00df][\u0080-\u00bf]/g, | ||
function (c) { | ||
var cc = (c.charCodeAt(0) & 0x1f) << 6 | (c.charCodeAt(1) & 0x3f); | ||
return String.fromCharCode(cc); | ||
} | ||
).replace( | ||
/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g, | ||
function (c) { | ||
var cc = (c.charCodeAt(0) & 0x0f) << 12 | (c.charCodeAt(1) & 0x3f) << 6 | (c.charCodeAt(2) & 0x3f); | ||
return String.fromCharCode(cc); | ||
} | ||
); | ||
if (/[\u00c0-\u00df][\u0080-\u00bf]/.test(str) || | ||
/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/.test(str)) { | ||
return decodeURIComponent(escape(str)); | ||
} | ||
return str; | ||
} | ||
@@ -1206,97 +1189,2 @@ /** | ||
/** | ||
* 创建条件类型 | ||
* | ||
* @param {Array of array|Function} patterns 数组第一元素表示命中条件,第二位类型 | ||
* @return {Schema} 返回条件类型 | ||
'''<example>''' | ||
* @example casesCreator():base | ||
```js | ||
var _ = jpacks; | ||
var _schema = { | ||
type: _.shortString, | ||
data: _.depend('type', _.cases([ | ||
['name', _.shortString], | ||
['age', _.byte] | ||
])) | ||
}; | ||
console.log(_.stringify(_schema)); | ||
// > {type:string('uint8'),data:depend('type',cases([['name',string('uint8')],['age','uint8']]))} | ||
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} | ||
``` | ||
* @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) { | ||
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) { | ||
fn.namespace = 'cases'; | ||
fn.args = args; | ||
}); | ||
Schema.register('cases', cases); | ||
/** | ||
* 声明字段依赖结构 | ||
@@ -1452,9 +1340,13 @@ * | ||
name: _.shortString, | ||
welcome: _.depend('name', _.cases([ | ||
['zswang', _.depend('name', _.virtual('Hello '))], | ||
['wang', _.depend('name', _.virtual('Hi '))] | ||
])) | ||
welcome: _.depend('name', function (name) { | ||
switch (name) { | ||
case 'zswang': | ||
return _.depend('name', _.virtual('Hello ')); | ||
case 'wang': | ||
return _.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 '))]]))}) | ||
// > object({name:string('uint8'),welcome:depend('name',$fn)}) | ||
var buffer = _.pack(_schema, { | ||
@@ -1461,0 +1353,0 @@ name: 'zswang' |
@@ -5,3 +5,3 @@ { | ||
"description": "Binary data packing and unpacking.", | ||
"version": "0.4.5", | ||
"version": "0.5.7", | ||
"homepage": "http://github.com/zswang/jpacks", | ||
@@ -8,0 +8,0 @@ "main": "jpacks.js", |
@@ -5,16 +5,2 @@ var long = require('long'); | ||
/*<define>*/ | ||
function bytes2Hex(bytes) { | ||
return bytes.map(function(byte) { | ||
return (0x100 + byte).toString(16).slice(1); | ||
}).join(''); | ||
} | ||
function hex2bytes(hex) { | ||
var bytes = []; | ||
hex.replace(/../g, function(all) { | ||
bytes.push(parseInt(all, 16)); | ||
}); | ||
return bytes; | ||
} | ||
/** | ||
@@ -21,0 +7,0 @@ * int64 类型 |
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
110444
3489