Skip to content

Commit 98ad2e2

Browse files
committed
adding identify class
1 parent 4d968ce commit 98ad2e2

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

src/identify.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// var querystring = require('querystring');
2+
3+
/*
4+
* Wrapper for a user properties JSON object that supports operations
5+
*/
6+
7+
var AMP_OP_ADD = '$add';
8+
var AMP_OP_SET = '$set';
9+
var AMP_OP_SET_ONCE = '$setOnce';
10+
var AMP_OP_UNSET = '$unset';
11+
12+
13+
var Identify = function() {
14+
this.userPropertiesOperations = {};
15+
this.properties = []; // keep track of keys that have been added
16+
};
17+
18+
Identify.prototype.add = function(property, value) {
19+
// validate value is number??
20+
this._addOperation(AMP_OP_ADD, property, value);
21+
return this;
22+
};
23+
24+
Identify.prototype.set = function(property, value) {
25+
this._addOperation(AMP_OP_SET, property, value);
26+
return this;
27+
};
28+
29+
Identify.prototype.setOnce = function(property, value) {
30+
this._addOperation(AMP_OP_SET_ONCE, value);
31+
return this;
32+
};
33+
34+
Identify.prototype.unset = function(property) {
35+
this._addOperation(AMP_OP_UNSET, property, '-');
36+
return this;
37+
};
38+
39+
Identify.prototype._addOperation = function(operation, property, value) {
40+
// check that property wasn't already used in this Identify
41+
if (this.properties.indexOf(property) !== -1) {
42+
return;
43+
}
44+
45+
if (!(operation in this.userPropertiesOperations)){
46+
this.userPropertiesOperations[operation] = {};
47+
}
48+
this.userPropertiesOperations[operation][property] = value;
49+
this.properties.push(property);
50+
};
51+
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+
};

test/identify.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
var Identify = require('../src/identify.js');
2+
3+
describe('Identify', function() {
4+
5+
var encodeCases = [
6+
['', ''],
7+
['f', 'Zg=='],
8+
['fo', 'Zm8='],
9+
['foo', 'Zm9v'],
10+
['foob', 'Zm9vYg=='],
11+
['fooba', 'Zm9vYmE='],
12+
['foobar', 'Zm9vYmFy'],
13+
['\u2661', '4pmh']
14+
];
15+
16+
var decodeCases = [
17+
['', ''],
18+
['Zg', 'f'],
19+
['Zg==', 'f'],
20+
['Zm8', 'fo'],
21+
['Zm8=', 'fo'],
22+
['Zm9v', 'foo'],
23+
['Zm9vYg', 'foob'],
24+
['Zm9vYg==', 'foob'],
25+
['Zm9vYmE', 'fooba'],
26+
['Zm9vYmE=', 'fooba'],
27+
['Zm9vYmFy', 'foobar'],
28+
['4pmh', '\u2661']
29+
];
30+
31+
it('should unset properties', function () {
32+
var property1 = 'testProperty1';
33+
var property2 = 'testProperty2';
34+
var identify = new Identify.Identify().unset(property1).unset(property2);
35+
36+
console.log(Identify.AMP_OP_UNSET);
37+
38+
var expected = {};
39+
expected[Identify.AMP_OP_UNSET] = {
40+
property1: '-',
41+
property2: '-'
42+
};
43+
assert.equal(expected, identify.userPropertiesOperations);
44+
});
45+
46+
/*
47+
it('should encode properly', function() {
48+
for (var i = 0; i < encodeCases.length; i++) {
49+
assert.equal(encodeCases[i][1], Base64.encode(encodeCases[i][0]));
50+
}
51+
});
52+
53+
it('should decode properly', function() {
54+
for (var i = 0; i < decodeCases.length; i++) {
55+
assert.equal(decodeCases[i][1], Base64.decode(decodeCases[i][0]));
56+
}
57+
});
58+
*/
59+
});

0 commit comments

Comments
 (0)