blob: c4bbda1e61e92228434c928d85e6ca53330a4218 (
plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
export default class CustomFieldConverter {
/**
* @param {BasicPasswordsClient} api
*/
constructor(api) {
this._api = api;
}
/**
* @param {String} string
*
* @return {(CustomFieldCollection|AbstractField)}
* @api
*/
fromJSON(string) {
let data = JSON.parse(string);
if(Array.isArray(data)) {
return this.fromArray(data);
}
if(typeof data === 'object' && data instanceof Object) {
return this.fromObject(data);
}
}
/**
* @param {Object} object
* @return {AbstractField}
* @api
*/
fromObject(object) {
let type = object.type;
return this._api.getClass(`model.${type}Field`, object);
}
/**
* @param {Object[]} array
* @return {CustomFieldCollection}
* @api
*/
fromArray(array) {
let fields = [];
for(let field of array) {
fields.push(this.fromObject(field));
}
return this._api.getClass('collection.field', fields);
}
/**
* @param {CustomFieldCollection} collection
* @return {Object[]}
*/
toArray(collection) {
let array = [];
for(let field of collection) {
array.push(this.toObject(field));
}
return array;
}
/**
* @param {AbstractField} field
* @return {Object}
*/
toObject(field) {
return field.getProperties();
}
/**
* @param {AbstractField} field
* @return {String}
*/
toJSON(field) {
return JSON.stringify(field);
}
}
|