Skip to content

Commit a868905

Browse files
committed
Began modularisation, starting with payload.js and half of helpers.js
1 parent e2b9ea6 commit a868905

9 files changed

Lines changed: 170 additions & 148 deletions

File tree

Gruntfile.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,9 @@ module.exports = function(grunt) {
6868
},
6969
src: ['src/js/banner.js',
7070
'src/js/init.js',
71-
'src/js/helpers.js',
72-
'src/js/cookie.js',
73-
'src/js/context.js',
74-
'src/js/payload.js',
71+
'src/js/lib/helpers.js',
72+
'src/js/lib/cookie.js',
73+
'src/js/lib/context.js',
7574
'src/js/tracker.js',
7675
'src/js/snowplow.js',
7776
'src/js/constructor.js'],

src/js/init.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
* SnowPlow namespace.
3737
* Add classes and functions in this namespace.
3838
*/
39+
40+
var Identifiers = require('../src/js/lib/identifiers.js');
41+
var JSON2 = require('JSON');
42+
3943
var SnowPlow = SnowPlow || function() {
4044
var windowAlias = window;
4145

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
SnowPlow.hasCookies = function(testCookieName) {
3939
var cookieName = testCookieName || 'testcookie';
4040

41-
if (!SnowPlow.isDefined(SnowPlow.navigatorAlias.cookieEnabled)) {
41+
if (!Identifiers.isDefined(SnowPlow.navigatorAlias.cookieEnabled)) {
4242
SnowPlow.setCookie(cookieName, '1');
4343
return SnowPlow.getCookie(cookieName) === '1' ? '1' : '0';
4444
}
@@ -156,13 +156,13 @@ SnowPlow.detectBrowserFeatures = function(testCookieName) {
156156
// Safari and Opera
157157
// IE6/IE7 navigator.javaEnabled can't be aliased, so test directly
158158
if (typeof navigator.javaEnabled !== 'unknown' &&
159-
SnowPlow.isDefined(SnowPlow.navigatorAlias.javaEnabled) &&
159+
Identifiers.isDefined(SnowPlow.navigatorAlias.javaEnabled) &&
160160
SnowPlow.navigatorAlias.javaEnabled()) {
161161
features.java = '1';
162162
}
163163

164164
// Firefox
165-
if (SnowPlow.isFunction(SnowPlow.windowAlias.GearsFactory)) {
165+
if (Identifiers.isFunction(SnowPlow.windowAlias.GearsFactory)) {
166166
features.gears = '1';
167167
}
168168

File renamed without changes.
Lines changed: 4 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* JavaScript tracker for Snowplow: helpers.js
2+
* JavaScript tracker for Snowplow: SnowPlow.js
33
*
44
* Significant portions copyright 2010 Anthon Pang. Remainder copyright
55
* 2012-2014 Snowplow Analytics Ltd. All rights reserved.
@@ -32,87 +32,6 @@
3232
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3333
*/
3434

35-
/*
36-
* Is property defined?
37-
*/
38-
SnowPlow.isDefined = function (property) {
39-
return typeof property !== 'undefined';
40-
}
41-
42-
/**
43-
* Is property null?
44-
*/
45-
SnowPlow.isNotNull = function (property) {
46-
return property !== null;
47-
}
48-
49-
/*
50-
* Is property a function?
51-
*/
52-
SnowPlow.isFunction = function (property) {
53-
return typeof property === 'function';
54-
}
55-
56-
/*
57-
* Is property an array?
58-
*/
59-
SnowPlow.isArray = ('isArray' in Array) ?
60-
Array.isArray :
61-
function (value) {
62-
return Object.prototype.toString.call(value) === '[object Array]';
63-
}
64-
65-
/*
66-
* Is property an empty array?
67-
*/
68-
SnowPlow.isEmptyArray = function (property) {
69-
return SnowPlow.isArray(property) && property.length < 1;
70-
}
71-
72-
/*
73-
* Is property an object?
74-
*
75-
* @return bool Returns true if property is null, an Object, or subclass of Object (i.e., an instanceof String, Date, etc.)
76-
*/
77-
SnowPlow.isObject = function (property) {
78-
return typeof property === 'object';
79-
}
80-
81-
/*
82-
* Is property a JSON?
83-
*/
84-
SnowPlow.isJson = function (property) {
85-
return (SnowPlow.isDefined(property) && SnowPlow.isNotNull(property) && property.constructor === {}.constructor);
86-
}
87-
88-
/*
89-
* Is property a non-empty JSON?
90-
*/
91-
SnowPlow.isNonEmptyJson = function (property) {
92-
return SnowPlow.isJson(property) && property !== {};
93-
}
94-
95-
/*
96-
* Is property a string?
97-
*/
98-
SnowPlow.isString = function (property) {
99-
return typeof property === 'string' || property instanceof String;
100-
}
101-
102-
/*
103-
* Is property a non-empty string?
104-
*/
105-
SnowPlow.isNonEmptyString = function (property) {
106-
return SnowPlow.isString(property) && property !== '';
107-
}
108-
109-
/*
110-
* Is property a date?
111-
*/
112-
SnowPlow.isDate = function (property) {
113-
return Object.prototype.toString.call(property) === "[object Date]";
114-
}
115-
11635
/*
11736
* UTF-8 encoding
11837
*/
@@ -124,11 +43,11 @@ SnowPlow.encodeUtf8 = function (argString) {
12443
* Cleans up the page title
12544
*/
12645
SnowPlow.fixupTitle = function (title) {
127-
if (!SnowPlow.isString(title)) {
46+
if (!Identifiers.isString(title)) {
12847
title = title.text || '';
12948

13049
var tmp = SnowPlow.documentAlias.getElementsByTagName('title');
131-
if (tmp && SnowPlow.isDefined(tmp[0])) {
50+
if (tmp && Identifiers.isDefined(tmp[0])) {
13251
title = tmp[0].text;
13352
}
13453
}
@@ -294,7 +213,7 @@ SnowPlow.executePluginMethod = function (methodName, callback) {
294213
for (i in SnowPlow.plugins) {
295214
if (Object.prototype.hasOwnProperty.call(SnowPlow.plugins, i)) {
296215
pluginMethod = SnowPlow.plugins[i][methodName];
297-
if (SnowPlow.isFunction(pluginMethod)) {
216+
if (Identifiers.isFunction(pluginMethod)) {
298217
result += pluginMethod(callback);
299218
}
300219
}
@@ -314,33 +233,4 @@ SnowPlow.fromQuerystring = function (field, url) {
314233
return SnowPlow.decodeWrapper(match[1].replace(/\+/g, ' '));
315234
}
316235

317-
// Base64 module
318-
SnowPlow.base64 = require('Base64');
319-
320-
/*
321-
* Base64 encode data
322-
*/
323-
SnowPlow.base64encode = SnowPlow.base64.btoa;
324-
325-
/*
326-
* Base64 decode data
327-
*/
328-
SnowPlow.base64decode = SnowPlow.base64.atob;
329-
330-
/*
331-
* Bas64 encode data with URL and Filename Safe Alphabet (base64url)
332-
*
333-
* See: http://tools.ietf.org/html/rfc4648#page-7
334-
*/
335-
SnowPlow.base64urlencode = function(data) {
336-
if (!data) return data;
337-
338-
var enc = SnowPlow.base64encode(data);
339-
return enc.replace(/=/g, '')
340-
.replace(/\+/g, '-')
341-
.replace(/\//g, '_');
342-
};
343-
344236
SnowPlow.murmurhash3_32_gc = require('murmurhash').v3;
345-
346-
SnowPlow.JSON2 = require('JSON');

src/js/lib/identifiers.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
var Identifiers = {};
2+
module.exports = Identifiers;
3+
4+
/*
5+
* Is property defined?
6+
*/
7+
Identifiers.isDefined = function (property) {
8+
return typeof property !== 'undefined';
9+
}
10+
11+
/**
12+
* Is property null?
13+
*/
14+
Identifiers.isNotNull = function (property) {
15+
return property !== null;
16+
}
17+
18+
/*
19+
* Is property a function?
20+
*/
21+
Identifiers.isFunction = function (property) {
22+
return typeof property === 'function';
23+
}
24+
25+
/*
26+
* Is property an array?
27+
*/
28+
Identifiers.isArray = ('isArray' in Array) ?
29+
Array.isArray :
30+
function (value) {
31+
return Object.prototype.toString.call(value) === '[object Array]';
32+
}
33+
34+
/*
35+
* Is property an empty array?
36+
*/
37+
Identifiers.isEmptyArray = function (property) {
38+
return Identifiers.isArray(property) && property.length < 1;
39+
}
40+
41+
/*
42+
* Is property an object?
43+
*
44+
* @return bool Returns true if property is null, an Object, or subclass of Object (i.e., an instanceof String, Date, etc.)
45+
*/
46+
Identifiers.isObject = function (property) {
47+
return typeof property === 'object';
48+
}
49+
50+
/*
51+
* Is property a JSON?
52+
*/
53+
Identifiers.isJson = function (property) {
54+
return (Identifiers.isDefined(property) && Identifiers.isNotNull(property) && property.constructor === {}.constructor);
55+
}
56+
57+
/*
58+
* Is property a non-empty JSON?
59+
*/
60+
Identifiers.isNonEmptyJson = function (property) {
61+
return Identifiers.isJson(property) && property !== {};
62+
}
63+
64+
/*
65+
* Is property a string?
66+
*/
67+
Identifiers.isString = function (property) {
68+
return typeof property === 'string' || property instanceof String;
69+
}
70+
71+
/*
72+
* Is property a non-empty string?
73+
*/
74+
Identifiers.isNonEmptyString = function (property) {
75+
return Identifiers.isString(property) && property !== '';
76+
}
77+
78+
/*
79+
* Is property a date?
80+
*/
81+
Identifiers.isDate = function (property) {
82+
return Object.prototype.toString.call(property) === "[object Date]";
83+
}

0 commit comments

Comments
 (0)