@@ -3295,16 +3295,19 @@ var type = require('./type');
32953295 * only the first operation will be saved, and the rest will be ignored.
32963296 */
32973297
3298- var AMP_OP_ADD = '$add' ;
3299- var AMP_OP_SET = '$set' ;
3300- var AMP_OP_SET_ONCE = '$setOnce' ;
3301- var AMP_OP_UNSET = '$unset' ;
3298+ // maps operation to identify string and proxy key
3299+ // order listed also defines order of processing
3300+ var OPERATIONS = {
3301+ 'AMP_OP_SET_ONCE' : { 'opString' : '$setOnce' , 'proxyKey' : 'so' } ,
3302+ 'AMP_OP_UNSET' : { 'opString' : '$unset' , 'proxyKey' : 'u' } ,
3303+ 'AMP_OP_SET' : { 'opString' : '$set' , 'proxyKey' : 's' } ,
3304+ 'AMP_OP_ADD' : { 'opString' : '$add' , 'proxyKey' : 'a' }
3305+ } ;
33023306
33033307var log = function ( s ) {
33043308 console . log ( '[Amplitude] ' + s ) ;
33053309} ;
33063310
3307-
33083311var Identify = function ( ) {
33093312 this . userPropertiesOperations = { } ;
33103313 this . properties = [ ] ; // keep track of keys that have been added
@@ -3314,59 +3317,59 @@ Identify.prototype.fromProxyObject = function(proxyObject) {
33143317 if ( ! ( 'p' in proxyObject ) ) {
33153318 return this ;
33163319 }
3317-
3318- this . _addOperationsFromProxyObjectDictionary ( AMP_OP_SET_ONCE , proxyObject . p , 'so' ) ;
3319- this . _addOperationsFromProxyObjectDictionary ( AMP_OP_UNSET , proxyObject . p , 'u' ) ;
3320- this . _addOperationsFromProxyObjectDictionary ( AMP_OP_SET , proxyObject . p , 's' ) ;
3321- this . _addOperationsFromProxyObjectDictionary ( AMP_OP_ADD , proxyObject . p , 'a' ) ;
3320+ Object . keys ( OPERATIONS ) . forEach ( function ( operation ) {
3321+ this . _addOperationsFromProxyObjectDictionary ( operation , proxyObject . p ) ;
3322+ } . bind ( this ) ) ;
33223323 return this ;
33233324} ;
33243325
3325- Identify . prototype . _addOperationsFromProxyObjectDictionary = function ( operation , userPropertyOperations , dictionary ) {
3326- if ( dictionary in userPropertyOperations ) {
3327- for ( var key in userPropertyOperations [ dictionary ] ) {
3328- if ( userPropertyOperations [ dictionary ] . hasOwnProperty ( key ) ) {
3329- this . _addOperation ( operation , key , userPropertyOperations [ dictionary ] [ key ] ) ;
3330- }
3331- }
3326+ Identify . prototype . _addOperationsFromProxyObjectDictionary = function ( operation , proxyObjectDict ) {
3327+ var proxyKey = OPERATIONS [ operation ] . proxyKey ;
3328+ if ( ! ( proxyKey in proxyObjectDict ) ) {
3329+ return ;
33323330 }
3331+ var userPropertiesOperations = proxyObjectDict [ proxyKey ] ;
3332+ Object . keys ( userPropertiesOperations ) . forEach ( function ( key ) {
3333+ this . _addOperation ( operation , key , userPropertiesOperations [ key ] ) ;
3334+ } . bind ( this ) ) ;
33333335} ;
33343336
33353337Identify . prototype . add = function ( property , value ) {
33363338 if ( type ( value ) === 'number' || type ( value ) === 'string' ) {
3337- this . _addOperation ( AMP_OP_ADD , property , value ) ;
3339+ this . _addOperation ( ' AMP_OP_ADD' , property , value ) ;
33383340 } else {
33393341 log ( 'Unsupported type for value: ' + type ( value ) + ', expecting number or string' ) ;
33403342 }
33413343 return this ;
33423344} ;
33433345
33443346Identify . prototype . set = function ( property , value ) {
3345- this . _addOperation ( AMP_OP_SET , property , value ) ;
3347+ this . _addOperation ( ' AMP_OP_SET' , property , value ) ;
33463348 return this ;
33473349} ;
33483350
33493351Identify . prototype . setOnce = function ( property , value ) {
3350- this . _addOperation ( AMP_OP_SET_ONCE , property , value ) ;
3352+ this . _addOperation ( ' AMP_OP_SET_ONCE' , property , value ) ;
33513353 return this ;
33523354} ;
33533355
33543356Identify . prototype . unset = function ( property ) {
3355- this . _addOperation ( AMP_OP_UNSET , property , '-' ) ;
3357+ this . _addOperation ( ' AMP_OP_UNSET' , property , '-' ) ;
33563358 return this ;
33573359} ;
33583360
33593361Identify . prototype . _addOperation = function ( operation , property , value ) {
3362+ var opString = OPERATIONS [ operation ] . opString ;
33603363 // check that property wasn't already used in this Identify
33613364 if ( this . properties . indexOf ( property ) !== - 1 ) {
3362- log ( 'User property "' + property + '" already used in this identify, skipping operation ' + operation ) ;
3365+ log ( 'User property "' + property + '" already used in this identify, skipping operation ' + opString ) ;
33633366 return ;
33643367 }
33653368
3366- if ( ! ( operation in this . userPropertiesOperations ) ) {
3367- this . userPropertiesOperations [ operation ] = { } ;
3369+ if ( ! ( opString in this . userPropertiesOperations ) ) {
3370+ this . userPropertiesOperations [ opString ] = { } ;
33683371 }
3369- this . userPropertiesOperations [ operation ] [ property ] = value ;
3372+ this . userPropertiesOperations [ opString ] [ property ] = value ;
33703373 this . properties . push ( property ) ;
33713374} ;
33723375
0 commit comments