Skip to content

Commit 0c37967

Browse files
committed
added tests for identify
1 parent 98ad2e2 commit 0c37967

File tree

6 files changed

+235
-70
lines changed

6 files changed

+235
-70
lines changed

amplitude.js

Lines changed: 77 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ var Request = require('./xhr');
120120
var UAParser = require('ua-parser-js');
121121
var UUID = require('./uuid');
122122
var version = require('./version');
123+
var Identify = require('./identify');
123124

124125
var log = function(s) {
125126
console.log('[Amplitude] ' + s);
@@ -159,6 +160,7 @@ var Amplitude = function() {
159160
this.options = object.merge({}, DEFAULT_OPTIONS);
160161
};
161162

163+
Amplitude.prototype._test = new Identify();
162164

163165
Amplitude.prototype._eventId = 0;
164166
Amplitude.prototype._sending = false;
@@ -630,7 +632,7 @@ Amplitude.prototype.__VERSION__ = version;
630632

631633
module.exports = Amplitude;
632634

633-
}, {"./cookie":3,"json":4,"./language":5,"./localstorage":6,"JavaScript-MD5":7,"object":8,"./xhr":9,"ua-parser-js":10,"./uuid":11,"./version":12}],
635+
}, {"./cookie":3,"json":4,"./language":5,"./localstorage":6,"JavaScript-MD5":7,"object":8,"./xhr":9,"ua-parser-js":10,"./uuid":11,"./version":12,"./identify":13}],
634636
3: [function(require, module, exports) {
635637
/*
636638
* Cookie data
@@ -757,8 +759,8 @@ module.exports = {
757759

758760
};
759761

760-
}, {"./base64":13,"json":4,"top-domain":14}],
761-
13: [function(require, module, exports) {
762+
}, {"./base64":14,"json":4,"top-domain":15}],
763+
14: [function(require, module, exports) {
762764
/* jshint bitwise: false */
763765
/* global escape, unescape */
764766

@@ -857,8 +859,8 @@ var Base64 = {
857859

858860
module.exports = Base64;
859861

860-
}, {"./utf8":15}],
861-
15: [function(require, module, exports) {
862+
}, {"./utf8":16}],
863+
16: [function(require, module, exports) {
862864
/* jshint bitwise: false */
863865

864866
/*
@@ -928,8 +930,8 @@ module.exports = parse && stringify
928930
? JSON
929931
: require('json-fallback');
930932

931-
}, {"json-fallback":16}],
932-
16: [function(require, module, exports) {
933+
}, {"json-fallback":17}],
934+
17: [function(require, module, exports) {
933935
/*
934936
json2.js
935937
2014-02-04
@@ -1419,7 +1421,7 @@ module.exports = parse && stringify
14191421
}());
14201422

14211423
}, {}],
1422-
14: [function(require, module, exports) {
1424+
15: [function(require, module, exports) {
14231425

14241426
/**
14251427
* Module dependencies.
@@ -1467,8 +1469,8 @@ function domain(url){
14671469
return match ? match[0] : '';
14681470
};
14691471

1470-
}, {"url":17}],
1471-
17: [function(require, module, exports) {
1472+
}, {"url":18}],
1473+
18: [function(require, module, exports) {
14721474

14731475
/**
14741476
* Parse the given `url`.
@@ -2063,8 +2065,8 @@ Request.prototype.send = function(callback) {
20632065

20642066
module.exports = Request;
20652067

2066-
}, {"querystring":18}],
2067-
18: [function(require, module, exports) {
2068+
}, {"querystring":19}],
2069+
19: [function(require, module, exports) {
20682070

20692071
/**
20702072
* Module dependencies.
@@ -2139,8 +2141,8 @@ exports.stringify = function(obj){
21392141
return pairs.join('&');
21402142
};
21412143

2142-
}, {"trim":19,"type":20}],
2143-
19: [function(require, module, exports) {
2144+
}, {"trim":20,"type":21}],
2145+
20: [function(require, module, exports) {
21442146

21452147
exports = module.exports = trim;
21462148

@@ -2160,7 +2162,7 @@ exports.right = function(str){
21602162
};
21612163

21622164
}, {}],
2163-
20: [function(require, module, exports) {
2165+
21: [function(require, module, exports) {
21642166
/**
21652167
* toString ref.
21662168
*/
@@ -3117,5 +3119,65 @@ module.exports = uuid;
31173119
12: [function(require, module, exports) {
31183120
module.exports = '2.3.0';
31193121

3122+
}, {}],
3123+
13: [function(require, module, exports) {
3124+
// var querystring = require('querystring');
3125+
3126+
/*
3127+
* Wrapper for a user properties JSON object that supports operations
3128+
*/
3129+
3130+
var AMP_OP_ADD = '$add';
3131+
var AMP_OP_SET = '$set';
3132+
var AMP_OP_SET_ONCE = '$setOnce';
3133+
var AMP_OP_UNSET = '$unset';
3134+
3135+
3136+
var Identify = function() {
3137+
this.userPropertiesOperations = {};
3138+
this.properties = []; // keep track of keys that have been added
3139+
};
3140+
3141+
var isNumeric = function(n) {
3142+
return !isNaN(parseFloat(n)) && isFinite(n);
3143+
};
3144+
3145+
Identify.prototype.add = function(property, value) {
3146+
if (isNumeric(value) || typeof(value) === 'string' || value instanceof String) {
3147+
this._addOperation(AMP_OP_ADD, property, value);
3148+
}
3149+
return this;
3150+
};
3151+
3152+
Identify.prototype.set = function(property, value) {
3153+
this._addOperation(AMP_OP_SET, property, value);
3154+
return this;
3155+
};
3156+
3157+
Identify.prototype.setOnce = function(property, value) {
3158+
this._addOperation(AMP_OP_SET_ONCE, property, value);
3159+
return this;
3160+
};
3161+
3162+
Identify.prototype.unset = function(property) {
3163+
this._addOperation(AMP_OP_UNSET, property, '-');
3164+
return this;
3165+
};
3166+
3167+
Identify.prototype._addOperation = function(operation, property, value) {
3168+
// check that property wasn't already used in this Identify
3169+
if (this.properties.indexOf(property) !== -1) {
3170+
return;
3171+
}
3172+
3173+
if (!(operation in this.userPropertiesOperations)){
3174+
this.userPropertiesOperations[operation] = {};
3175+
}
3176+
this.userPropertiesOperations[operation][property] = value;
3177+
this.properties.push(property);
3178+
};
3179+
3180+
module.exports = Identify;
3181+
31203182
}, {}]}, {}, {"1":""})
31213183
);

amplitude.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/amplitude.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var Request = require('./xhr');
88
var UAParser = require('ua-parser-js');
99
var UUID = require('./uuid');
1010
var version = require('./version');
11+
var Identify = require('./identify');
1112

1213
var log = function(s) {
1314
console.log('[Amplitude] ' + s);
@@ -47,6 +48,7 @@ var Amplitude = function() {
4748
this.options = object.merge({}, DEFAULT_OPTIONS);
4849
};
4950

51+
Amplitude.prototype._test = new Identify();
5052

5153
Amplitude.prototype._eventId = 0;
5254
Amplitude.prototype._sending = false;

src/identify.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ var Identify = function() {
1515
this.properties = []; // keep track of keys that have been added
1616
};
1717

18+
var isNumeric = function(n) {
19+
return !isNaN(parseFloat(n)) && isFinite(n);
20+
};
21+
1822
Identify.prototype.add = function(property, value) {
19-
// validate value is number??
20-
this._addOperation(AMP_OP_ADD, property, value);
23+
if (isNumeric(value) || typeof(value) === 'string' || value instanceof String) {
24+
this._addOperation(AMP_OP_ADD, property, value);
25+
}
2126
return this;
2227
};
2328

@@ -27,7 +32,7 @@ Identify.prototype.set = function(property, value) {
2732
};
2833

2934
Identify.prototype.setOnce = function(property, value) {
30-
this._addOperation(AMP_OP_SET_ONCE, value);
35+
this._addOperation(AMP_OP_SET_ONCE, property, value);
3136
return this;
3237
};
3338

@@ -49,11 +54,4 @@ Identify.prototype._addOperation = function(operation, property, value) {
4954
this.properties.push(property);
5055
};
5156

52-
53-
module.exports = {
54-
Identify: Identify,
55-
AMP_OP_ADD: AMP_OP_ADD,
56-
AMP_OP_SET: AMP_OP_SET,
57-
AMP_OP_SET_ONCE: AMP_OP_SET_ONCE,
58-
AMP_OP_UNSET: AMP_OP_UNSET
59-
};
57+
module.exports = Identify;

0 commit comments

Comments
 (0)