set.js
1.46 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
68
69
70
71
var util = require('../core').util;
var typeOf = require('./types').typeOf;
/**
* @api private
*/
var memberTypeToSetType = {
'String': 'String',
'Number': 'Number',
'NumberValue': 'Number',
'Binary': 'Binary'
};
/**
* @api private
*/
var DynamoDBSet = util.inherit({
constructor: function Set(list, options) {
options = options || {};
this.wrapperName = 'Set';
this.initialize(list, options.validate);
},
initialize: function(list, validate) {
var self = this;
self.values = [].concat(list);
self.detectType();
if (validate) {
self.validate();
}
},
detectType: function() {
this.type = memberTypeToSetType[typeOf(this.values[0])];
if (!this.type) {
throw util.error(new Error(), {
code: 'InvalidSetType',
message: 'Sets can contain string, number, or binary values'
});
}
},
validate: function() {
var self = this;
var length = self.values.length;
var values = self.values;
for (var i = 0; i < length; i++) {
if (memberTypeToSetType[typeOf(values[i])] !== self.type) {
throw util.error(new Error(), {
code: 'InvalidType',
message: self.type + ' Set contains ' + typeOf(values[i]) + ' value'
});
}
}
},
/**
* Render the underlying values only when converting to JSON.
*/
toJSON: function() {
var self = this;
return self.values;
}
});
/**
* @api private
*/
module.exports = DynamoDBSet;