forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec-base.js
More file actions
67 lines (49 loc) · 1.19 KB
/
codec-base.js
File metadata and controls
67 lines (49 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// codec-base.js
var IS_ARRAY = require("isarray");
exports.createCodec = createCodec;
exports.install = install;
exports.filter = filter;
var Bufferish = require("./bufferish");
function Codec(options) {
if (!(this instanceof Codec)) return new Codec(options);
this.options = options;
this.init();
}
Codec.prototype.init = function() {
var options = this.options;
if (options && options.uint8array) {
this.bufferish = Bufferish.Uint8Array;
}
return this;
};
function install(props) {
for (var key in props) {
Codec.prototype[key] = add(Codec.prototype[key], props[key]);
}
}
function add(a, b) {
return (a && b) ? ab : (a || b);
function ab() {
a.apply(this, arguments);
return b.apply(this, arguments);
}
}
function join(filters) {
filters = filters.slice();
return function(value) {
return filters.reduce(iterator, value);
};
function iterator(value, filter) {
return filter(value);
}
}
function filter(filter) {
return IS_ARRAY(filter) ? join(filter) : filter;
}
// @public
// msgpack.createCodec()
function createCodec(options) {
return new Codec(options);
}
// default shared codec
exports.preset = createCodec({preset: true});