Skip to content

Commit 45de17e

Browse files
committed
msgpack.pack add second argument.
msgpack.pack(mix:Mix, toString:Boolean = false) Signed-off-by: uupaa <uupaa.js@gmail.com>
1 parent d415441 commit 45de17e

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

msgpack.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
/*!{id:msgpack.js,ver:1.01,license:"MIT",author:"uupaa.js@gmail.com"}*/
12

23
// === msgpack ===
34
// MessagePack -> http://msgpack.sourceforge.net/
45

56
this.msgpack || (function(globalScope) {
67

78
globalScope.msgpack = {
8-
pack: msgpackpack, // msgpack.pack(data:Mix):ByteArray
9+
pack: msgpackpack, // msgpack.pack(data:Mix, toString:Boolean = false):ByteArray/ByteString
10+
// [1][mix to String] msgpack.pack({}, true) -> "..."
11+
// [2][mix to ByteArray] msgpack.pack({}) -> [...]
912
unpack: msgpackunpack, // msgpack.unpack(data:BinaryString/ByteArray):Mix
13+
// [1][String to mix] msgpack.unpack("...") -> {}
14+
// [2][ByteArray to mix] msgpack.unpack([...]) -> {}
1015
worker: "msgpack.js", // msgpack.worker - WebWorkers script filename
1116
upload: msgpackupload, // msgpack.upload(url:String, option:Hash, callback:Function)
1217
download: msgpackdownload // msgpack.download(url:String, option:Hash, callback:Function)
@@ -31,14 +36,24 @@ self.importScripts && (onmessage = function(event) {
3136
});
3237

3338
// msgpack.pack
34-
function msgpackpack(data) { // @param Mix:
35-
// @return ByteArray:
36-
return encode([], data);
39+
function msgpackpack(data, // @param Mix:
40+
toString) { // @param Boolean(= false):
41+
// @return ByteArray/BinaryString:
42+
// [1][mix to String] msgpack.pack({}, true) -> "..."
43+
// [2][mix to ByteArray] msgpack.pack({}) -> [...]
44+
45+
var byteArray = encode([], data);
46+
47+
return toString ? String.fromCharCode.apply(null, byteArray) // toString
48+
: byteArray;
3749
}
3850

3951
// msgpack.unpack
4052
function msgpackunpack(data) { // @param BinaryString/ByteArray:
4153
// @return Mix:
54+
// [1][String to mix] msgpack.unpack("...") -> {}
55+
// [2][ByteArray to mix] msgpack.unpack([...]) -> {}
56+
4257
return { data: typeof data === "string" ? toByteArray(data)
4358
: data,
4459
index: -1, decode: decode }.decode();

0 commit comments

Comments
 (0)