-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
55 lines (49 loc) · 2.05 KB
/
index.js
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
const Types = require('./src/types')
const {create, fromBuffer, toBuffer} = require('./src/fcbuffer')
/**
@typedef {object} SerializerConfig
@property {boolean} [SerializerConfig.defaults = false] - Insert in defaults (like 0, false, '000...', or '') for any missing values. This helps test and inspect what a definition should look like. Do not enable in production.
@property {boolean} [SerializerConfig.debug = false] - Prints lots of HEX and field-level information to help debug binary serialization.
@property {object} [customTypes] - Add or overwrite low level types (see ./src/types.js `const types = {...}`).
*/
/**
@typedef {object} CreateStruct
@property {Array<String>} CreateStruct.errors - If any errors exists, no struts will be created.
@property {Object} CreateStruct.struct - Struct objects keyed by definition name.
@property {String} CreateStruct.struct.structName - Struct object that will serialize this type.
@property {Struct} CreateStruct.struct.struct - Struct object that will serialize this type (see ./src/struct.js).
*/
/**
@arg {object} definitions - examples https://github.com/eosjs/json/blob/master/schema/generated.json
@arg {SerializerConfig} config
@return {CreateStruct}
*/
module.exports = (definitions, config = {}) => {
if(typeof definitions !== 'object') {
throw new TypeError('definitions is a required parameter')
}
if(config.customTypes) {
definitions = Object.assign({}, definitions) //clone
for(const key in config.customTypes) { // custom types overwrite definations
delete definitions[key]
}
}
const types = Types(config)
const {errors, structs} = create(definitions, types)
const extend = (parent, child) => {
const combined = Object.assign(parent, child)
const {structs, errors} = create(combined, types)
return {
errors,
structs,
extend: child => extend(combined, child)
}
}
return {
errors,
structs,
extend: child => extend(definitions, child)
}
}
module.exports.fromBuffer = fromBuffer
module.exports.toBuffer = toBuffer