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