UNPKG

1.34 kBJavaScriptView Raw
1var util = require('../core').util;
2var typeOf = require('./types').typeOf;
3
4/**
5 * @api private
6 */
7var memberTypeToSetType = {
8 'String': 'String',
9 'Number': 'Number',
10 'NumberValue': 'Number',
11 'Binary': 'Binary'
12};
13
14/**
15 * @api private
16 */
17var DynamoDBSet = util.inherit({
18
19 constructor: function Set(list, options) {
20 options = options || {};
21 this.wrapperName = 'Set';
22 this.initialize(list, options.validate);
23 },
24
25 initialize: function(list, validate) {
26 var self = this;
27 self.values = [].concat(list);
28 self.detectType();
29 if (validate) {
30 self.validate();
31 }
32 },
33
34 detectType: function() {
35 this.type = memberTypeToSetType[typeOf(this.values[0])];
36 if (!this.type) {
37 throw util.error(new Error(), {
38 code: 'InvalidSetType',
39 message: 'Sets can contain string, number, or binary values'
40 });
41 }
42 },
43
44 validate: function() {
45 var self = this;
46 var length = self.values.length;
47 var values = self.values;
48 for (var i = 0; i < length; i++) {
49 if (memberTypeToSetType[typeOf(values[i])] !== self.type) {
50 throw util.error(new Error(), {
51 code: 'InvalidType',
52 message: self.type + ' Set contains ' + typeOf(values[i]) + ' value'
53 });
54 }
55 }
56 }
57
58});
59
60/**
61 * @api private
62 */
63module.exports = DynamoDBSet;