buffer-codec
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -101,4 +101,5 @@ (function() { | ||
} | ||
return this.bufferSize = aBufferSize; | ||
this.bufferSize = aBufferSize; | ||
} | ||
return this.buffer; | ||
}; | ||
@@ -128,3 +129,3 @@ | ||
len = this.buffer.write(str); | ||
return this._decodeBuffer(this.buffer, 0, len); | ||
return this.decodeBuffer(this.buffer, 0, len); | ||
} else { | ||
@@ -182,6 +183,65 @@ throw new NotImplementedError(); | ||
Codec.prototype.encode = Codec.prototype.encodeString; | ||
Codec.prototype.ensureEncodeBuffer = function(buffer, encoding) { | ||
var destBuffer, len; | ||
if (encoding == null) { | ||
encoding = 'utf8'; | ||
} | ||
len = this.encodeBuffer(buffer, null, 0, encoding); | ||
destBuffer = this.init(len); | ||
len = this.encodeBuffer(buffer, destBuffer, 0, encoding); | ||
return destBuffer.slice(0, len); | ||
}; | ||
Codec.prototype.decode = Codec.prototype.decodeString; | ||
Codec.prototype.encode = function(value, options) { | ||
options || (options = {}); | ||
if (options.buffer) { | ||
if (options.buffer === true) { | ||
return this.ensureEncodeBuffer(value, options.bufferEncoding); | ||
} else { | ||
return this.encodeBuffer(value, options.buffer, options.bufferOffset, options.bufferEncoding); | ||
} | ||
} else { | ||
return this.encodeString(value); | ||
} | ||
}; | ||
Codec.prototype.decode = function(value, options) { | ||
options || (options = {}); | ||
if (isBuffer(value)) { | ||
return this.decodeBuffer(value, options.bufferStart, options.bufferEnd, options.bufferEncoding); | ||
} else { | ||
return this.decodeString(value); | ||
} | ||
}; | ||
Codec.encode = function(value, options) { | ||
var encoding; | ||
if (!(options && options.encoding)) { | ||
return value; | ||
} | ||
encoding = options.encoding; | ||
if (!(encoding instanceof Codec)) { | ||
encoding = Codec(encoding); | ||
if (!encoding) { | ||
return value; | ||
} | ||
} | ||
return encoding.encode(value, options); | ||
}; | ||
Codec.decode = function(value, options) { | ||
var encoding; | ||
if (!(options && options.encoding)) { | ||
return value; | ||
} | ||
encoding = options.encoding; | ||
if (!(encoding instanceof Codec)) { | ||
encoding = Codec(encoding); | ||
if (!encoding) { | ||
return value; | ||
} | ||
} | ||
return encoding.decode(value, options); | ||
}; | ||
Codec.getNameFromClass = function(aCodecClass) { | ||
@@ -188,0 +248,0 @@ var codecName, len; |
@@ -105,6 +105,6 @@ (function() { | ||
BinaryCodec.prototype._decodeBuffer = function(data, start, end) { | ||
if ((data == null) || !((start != null) && end) || !data.slice) { | ||
if ((data != null) && (start != null) && end && data.slice) { | ||
return data.slice(start, end); | ||
} else { | ||
return data; | ||
} else { | ||
return data.slice(start, end); | ||
} | ||
@@ -111,0 +111,0 @@ }; |
{ | ||
"name": "buffer-codec", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "add the codec ability to abstract-nosql database.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/snowyu/node-buffer-codec", |
@@ -11,9 +11,24 @@ # AbstractCodec | ||
* name: the codec name. | ||
* encode(value, options): encode the value. | ||
* return the encoded string, or encoded buffer if options.buffer is true | ||
* note: the return encoded buffer is a global buffer instance on the codec. | ||
* return the byte length of encoded value if options.buffer is true or is a Buffer. | ||
* options.encoding *(string or codec instance)*: return the value directly if no encoding | ||
* options.buffer: the destBuffer or true. | ||
* options.bufferEncoding *(string)*: the Buffer encoding used via Buffer. defaults to 'utf8' | ||
* options.bufferOffset *(int)*: the offset of destBuffer. defaults to 0. if options.buffer is a Buffer. | ||
* decode(value, options): decode the value. | ||
* return the decoded value. | ||
* options.encoding *(string or codec instance)*: return the value directly if no encoding | ||
* if value is Buffer: | ||
* options.bufferEncoding *(string)*: the Buffer encoding used via value is Buffer. defaults to 'utf8' | ||
* options.bufferStart *(int)*: the start of value. defaults to 0. | ||
* options.bufferEnd *(int)*: the end of value. defaults to value.length - options.bufferStart. | ||
* encodeString(value): encode the value. return the encoded string. | ||
* decodeString(aString): decode the string(value). return the decoded value. | ||
* encodeBuffer(value, destBuffer, offset=0, encoding='utf8'): | ||
* encodeBuffer(value, destBuffer, offset=0, encoding='utf8'): | ||
* encode value to the destBuffer. return the encoded length. | ||
* it just return the encoded byte length if the destBuffer is null | ||
* the default start is 0 offset of destBuffer. | ||
* decodeBufer(buffer, start, end, encoding='utf8'): | ||
* decodeBuffer(buffer, start, end, encoding='utf8'): | ||
* decode the buffer. return the decoded value. | ||
@@ -20,0 +35,0 @@ * the default start is 0, end is buffer.length - start. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
53017
460
139