Skip to content
This repository was archived by the owner on Jan 11, 2024. It is now read-only.

Commit 94f940c

Browse files
authored
Ma/oasis 1484 (#92)
* filter out undefined values * cleaned into one function, fixed tests * generalize tests
1 parent becf0ae commit 94f940c

File tree

2 files changed

+28
-44
lines changed

2 files changed

+28
-44
lines changed

lib/optimizely/index.js

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ Optimizely.prototype.activate = function(experimentKey, userId, attributes) {
128128
var experimentId = projectConfig.getExperimentId(this.configObj, experimentKey);
129129

130130
// remove null values from attributes
131-
attributes = this.__filterAttributes(attributes);
131+
attributes = this.__filterEmptyValues(attributes);
132132

133133
var impressionEventOptions = {
134134
attributes: attributes,
@@ -200,8 +200,8 @@ Optimizely.prototype.track = function(eventKey, userId, attributes, eventTags) {
200200
}
201201

202202
// remove null values from attributes and eventTags
203-
attributes = this.__filterAttributes(attributes);
204-
eventTags = this.__filterEventTags(eventTags);
203+
attributes = this.__filterEmptyValues(attributes);
204+
eventTags = this.__filterEmptyValues(eventTags);
205205

206206
var conversionEventOptions = {
207207
attributes: attributes,
@@ -400,31 +400,17 @@ Optimizely.prototype.__dispatchEvent = function (eventToDispatch, callback) {
400400
}
401401

402402
/**
403-
* Filters out attributes with null values
404-
* @param attributes
405-
* @returns {Object} attributes
403+
* Filters out attributes/eventTags with null or undefined values
404+
* @param map
405+
* @returns {Object} map
406406
*/
407-
Optimizely.prototype.__filterAttributes = function (attributes) {
408-
for (var key in attributes) {
409-
if (attributes.hasOwnProperty(key) && attributes[key] === null) {
410-
delete attributes[key]
407+
Optimizely.prototype.__filterEmptyValues = function (map) {
408+
for (var key in map) {
409+
if (map.hasOwnProperty(key) && (map[key] === null || map[key] === undefined)) {
410+
delete map[key]
411411
}
412412
}
413-
return attributes;
414-
}
415-
416-
/**
417-
* Filters out eventTags with null values
418-
* @param eventTags
419-
* @returns {Object} eventTags
420-
*/
421-
Optimizely.prototype.__filterEventTags = function (eventTags) {
422-
for (var key in eventTags) {
423-
if (eventTags.hasOwnProperty(key) && eventTags[key] === null) {
424-
delete eventTags[key]
425-
}
426-
}
427-
return eventTags;
413+
return map;
428414
}
429415

430416
module.exports = Optimizely;

lib/optimizely/tests.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,31 +1889,29 @@ describe('lib/optimizely', function() {
18891889
});
18901890
});
18911891

1892-
describe('__filterAttributes', function() {
1893-
it('should filter out a null value attribute', function() {
1894-
var attributes = {'test': null};
1895-
var filteredAttributes = optlyInstance.__filterAttributes(attributes);
1896-
assert.deepEqual(filteredAttributes, {});
1892+
describe('should filter out null values', function() {
1893+
it('should filter out a null value', function() {
1894+
var dict = {'test': null};
1895+
var filteredValue = optlyInstance.__filterEmptyValues(dict);
1896+
assert.deepEqual(filteredValue, {});
18971897
});
18981898

1899-
it('should filter out a null value attribute, leave a non null one', function() {
1900-
var attributes = {'test': null, 'test2': 'not_null'};
1901-
var filteredAttributes = optlyInstance.__filterAttributes(attributes);
1902-
assert.deepEqual(filteredAttributes, {'test2': 'not_null'});
1899+
it('should filter out a undefined value', function() {
1900+
var dict = {'test': undefined};
1901+
var filteredValue = optlyInstance.__filterEmptyValues(dict);
1902+
assert.deepEqual(filteredValue, {});
19031903
});
1904-
});
19051904

1906-
describe('__filterEventTags', function() {
1907-
it('should filter out a null value eventTag', function() {
1908-
var eventTags = {'test': null};
1909-
var filteredEventTags = optlyInstance.__filterEventTags(eventTags);
1910-
assert.deepEqual(filteredEventTags, {});
1905+
it('should filter out a null value, leave a non null one', function() {
1906+
var dict = {'test': null, 'test2': 'not_null'};
1907+
var filteredValue = optlyInstance.__filterEmptyValues(dict);
1908+
assert.deepEqual(filteredValue, {'test2': 'not_null'});
19111909
});
19121910

1913-
it('should filter out a null value attribute, leave a non null one', function() {
1914-
var eventTags = {'test': null, 'test2': 'not_null'};
1915-
var filteredEventTags = optlyInstance.__filterEventTags(eventTags);
1916-
assert.deepEqual(filteredEventTags, {'test2': 'not_null'});
1911+
it('should not filter out a non empty value', function() {
1912+
var dict = {'test': 'hello'};
1913+
var filteredValue = optlyInstance.__filterEmptyValues(dict);
1914+
assert.deepEqual(filteredValue, {'test': 'hello'});
19171915
});
19181916
});
19191917

0 commit comments

Comments
 (0)