Skip to content

Commit 67ba241

Browse files
committed
Use modularized imports for lodash (close snowplow#502)
1 parent 320166b commit 67ba241

12 files changed

Lines changed: 73 additions & 70 deletions

File tree

Gruntfile.js

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,6 @@ module.exports = function(grunt) {
7979

8080
subdomain: process.env.SUBDOMAIN,
8181

82-
lodash: {
83-
build: {
84-
dest: 'src/js/lib_managed/lodash.js',
85-
options: {
86-
exports: 'node',
87-
include: 'isArray, isFunction, isString, isObject, isUndefined, map, mapValues, forEach, filter, find'
88-
}
89-
}
90-
},
91-
9282
browserify: {
9383
main: {
9484
files: {
@@ -198,7 +188,6 @@ module.exports = function(grunt) {
198188
grunt.loadNpmTasks('grunt-aws');
199189
grunt.loadNpmTasks('grunt-browserify');
200190
grunt.loadNpmTasks('intern');
201-
grunt.loadNpmTasks('grunt-lodash');
202191

203192
grunt.registerTask('upload_setup', 'Read aws.json and configure upload tasks', function() {
204193
var aws = grunt.file.readJSON('aws.json');
@@ -266,11 +255,11 @@ module.exports = function(grunt) {
266255
});
267256
});
268257

269-
grunt.registerTask('default', 'Build lodash, Browserify, add banner, and minify', ['lodash', 'browserify:main', 'concat:deploy', 'min:deploy']);
270-
grunt.registerTask('publish', 'Upload to S3 and invalidate Cloudfront (full semantic version only)', ['upload_setup', 'lodash', 'browserify:main', 'concat:deploy', 'min:deploy', 's3:not_pinned', 'cloudfront:not_pinned']);
271-
grunt.registerTask('publish-pinned', 'Upload to S3 and invalidate Cloudfront (full semantic version and semantic major version)', ['upload_setup', 'lodash', 'browserify:main', 'concat:deploy', 'min:deploy', 's3', 'cloudfront']);
272-
grunt.registerTask('quick', 'Build snowplow.js, skipping building lodash and minifying', ['browserify:main', 'concat:deploy']);
258+
grunt.registerTask('default', 'Build Browserify, add banner, and minify', ['browserify:main', 'concat:deploy', 'min:deploy']);
259+
grunt.registerTask('publish', 'Upload to S3 and invalidate Cloudfront (full semantic version only)', ['upload_setup', 'browserify:main', 'concat:deploy', 'min:deploy', 's3:not_pinned', 'cloudfront:not_pinned']);
260+
grunt.registerTask('publish-pinned', 'Upload to S3 and invalidate Cloudfront (full semantic version and semantic major version)', ['upload_setup', 'browserify:main', 'concat:deploy', 'min:deploy', 's3', 'cloudfront']);
261+
grunt.registerTask('quick', 'Build snowplow.js, skipping building and minifying', ['browserify:main', 'concat:deploy']);
273262
grunt.registerTask('test', 'Intern tests', ['browserify:test', 'intern']);
274-
grunt.registerTask('travis', 'Intern tests for Travis CI', ['lodash', 'concat:test', 'browserify:test', 'intern']);
263+
grunt.registerTask('travis', 'Intern tests for Travis CI', ['concat:test', 'browserify:test', 'intern']);
275264
grunt.registerTask('tags', 'Minifiy the Snowplow invocation tag', ['min:tag', 'concat:tag']);
276265
};

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"dependencies": {
55
"browser-cookie-lite": "0.3.1",
66
"jstimezonedetect": "1.0.5",
7-
"lodash": "3.10.1",
87
"snowplow-tracker-core": "0.6.1",
98
"murmurhash": "0.0.2",
109
"uuid": "2.0.3",
@@ -16,11 +15,10 @@
1615
"grunt-aws": "0.6.2",
1716
"grunt-browserify": "5.0.0",
1817
"grunt-contrib-concat": "0.5.1",
19-
"grunt-lodash": "0.5.1",
2018
"grunt-yui-compressor": "git://github.com/snowplow/grunt-yui-compressor.git#fc70c3aa41bad8848b3569d6ea6ee70d270fd8a6",
19+
"lodash": "^4.17.11",
2120
"intern": "3.3.2",
2221
"js-base64": "2.1.9",
23-
"lodash-cli": "3.10.1",
2422
"semver": "4.3.2"
2523
},
2624
"contributors": [

src/js/errors.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3333
*/
3434

35-
var lodash = require('./lib_managed/lodash'),
35+
var isFunction = require('lodash/isFunction'),
3636
helpers = require('./lib/helpers'),
3737
object = typeof exports !== 'undefined' ? exports : this,
3838
windowAlias = window;
@@ -75,7 +75,7 @@ object.errorManager = function (core) {
7575
*/
7676
function sendError(errorEvent, commonContexts, contextsAdder) {
7777
var contexts;
78-
if (lodash.isFunction(contextsAdder)) {
78+
if (isFunction(contextsAdder)) {
7979
contexts = commonContexts.concat(contextsAdder(errorEvent));
8080
} else {
8181
contexts = commonContexts;
@@ -116,7 +116,7 @@ object.errorManager = function (core) {
116116
* @param errorEvent ErrorEvent passed to event listener
117117
*/
118118
function captureError (errorEvent) {
119-
if (lodash.isFunction(filter) && filter(errorEvent) || filter == null) {
119+
if (isFunction(filter) && filter(errorEvent) || filter == null) {
120120
sendError(errorEvent, contexts, contextsAdder)
121121
}
122122
}

src/js/forms.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3333
*/
3434

35-
var lodash = require('./lib_managed/lodash'),
35+
var forEach = require('lodash/forEach'),
36+
filter = require('lodash/filter'),
37+
find = require('lodash/find'),
3638
helpers = require('./lib/helpers'),
3739
object = typeof exports !== 'undefined' ? exports : this;
3840

@@ -65,7 +67,7 @@ object.getFormTrackingManager = function (core, trackerId, contextAdder) {
6567
* Get an identifier for a form, input, textarea, or select element
6668
*/
6769
function getFormElementName(elt) {
68-
return elt[lodash.find(['name', 'id', 'type', 'nodeName'], function (propName) {
70+
return elt[find(['name', 'id', 'type', 'nodeName'], function (propName) {
6971

7072
// If elt has a child whose name is "id", that element will be returned
7173
// instead of the actual id of elt unless we ensure that a string is returned
@@ -90,13 +92,13 @@ object.getFormTrackingManager = function (core, trackerId, contextAdder) {
9092
*/
9193
function getInnerFormElements(elt) {
9294
var innerElements = [];
93-
lodash.forEach(innerElementTags, function (tagname) {
95+
forEach(innerElementTags, function (tagname) {
9496

95-
var trackedChildren = lodash.filter(elt.getElementsByTagName(tagname), function (child) {
97+
var trackedChildren = filter(elt.getElementsByTagName(tagname), function (child) {
9698
return child.hasOwnProperty(trackingMarker);
9799
});
98100

99-
lodash.forEach(trackedChildren, function (child) {
101+
forEach(trackedChildren, function (child) {
100102
if (child.type === 'submit') {
101103
return;
102104
}
@@ -138,7 +140,7 @@ object.getFormTrackingManager = function (core, trackerId, contextAdder) {
138140
return function (e) {
139141
var elt = e.target;
140142
var innerElements = getInnerFormElements(elt);
141-
lodash.forEach(innerElements, function (innerElement) {
143+
forEach(innerElements, function (innerElement) {
142144
innerElement.value = fieldTransform(innerElement.value);
143145
});
144146
core.trackFormSubmission(getFormElementName(elt), helpers.getCssClasses(elt), innerElements, contextAdder(context));
@@ -163,11 +165,11 @@ object.getFormTrackingManager = function (core, trackerId, contextAdder) {
163165
* Add value change event listeners to all mutable inner form elements
164166
*/
165167
addFormListeners: function (context) {
166-
lodash.forEach(document.getElementsByTagName('form'), function (form) {
168+
forEach(document.getElementsByTagName('form'), function (form) {
167169
if (formFilter(form) && !form[trackingMarker]) {
168170

169-
lodash.forEach(innerElementTags, function (tagname) {
170-
lodash.forEach(form.getElementsByTagName(tagname), function (innerElement) {
171+
forEach(innerElementTags, function (tagname) {
172+
forEach(form.getElementsByTagName(tagname), function (innerElement) {
171173
if (fieldFilter(innerElement) && !innerElement[trackingMarker] && innerElement.type.toLowerCase() !== 'password') {
172174
helpers.addEventListener(innerElement, 'change', getFormChangeListener(context), false);
173175
innerElement[trackingMarker] = true;

src/js/in_queue.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
;(function() {
3636

3737
var
38-
lodash = require('./lib_managed/lodash'),
38+
map = require('lodash/map'),
39+
isUndefined = require('lodash/isUndefined'),
40+
isFunction = require('lodash/isFunction'),
3941
helpers = require('./lib/helpers'),
4042

4143
object = typeof exports !== 'undefined' ? exports : this; // For eventual node.js environment support
@@ -60,7 +62,7 @@
6062
var namedTrackers = [];
6163

6264
if (!names || names.length === 0) {
63-
namedTrackers = lodash.map(trackerDictionary);
65+
namedTrackers = map(trackerDictionary);
6466
} else {
6567
for (var i = 0; i < names.length; i++) {
6668
if (trackerDictionary.hasOwnProperty(names[i])) {
@@ -92,7 +94,7 @@
9294

9395
var name;
9496

95-
if (lodash.isUndefined(namespace)) {
97+
if (isUndefined(namespace)) {
9698
name = 'sp';
9799
} else {
98100
name = namespace;
@@ -151,7 +153,7 @@
151153
input = Array.prototype.shift.call(parameterArray);
152154

153155
// Custom callback rather than tracker method, called with trackerDictionary as the context
154-
if (lodash.isFunction(input)) {
156+
if (isFunction(input)) {
155157
input.apply(trackerDictionary, parameterArray);
156158
continue;
157159
}

src/js/lib/detectors.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434

3535
;(function() {
3636

37-
var
38-
lodash = require('../lib_managed/lodash'),
37+
var
38+
isFunction = require('lodash/isFunction'),
39+
isUndefined = require('lodash/isUndefined'),
3940
murmurhash3_32_gc = require('murmurhash').v3,
4041
tz = require('jstimezonedetect').jstz.determine(),
4142
cookie = require('browser-cookie-lite'),
@@ -98,7 +99,7 @@
9899
object.hasCookies = function(testCookieName) {
99100
var cookieName = testCookieName || 'testcookie';
100101

101-
if (lodash.isUndefined(navigatorAlias.cookieEnabled)) {
102+
if (isUndefined(navigatorAlias.cookieEnabled)) {
102103
cookie.cookie(cookieName, '1');
103104
return cookie.cookie(cookieName) === '1' ? '1' : '0';
104105
}
@@ -230,13 +231,13 @@
230231
// IE6/IE7 navigator.javaEnabled can't be aliased, so test directly
231232
if (navigatorAlias.constructor === window.Navigator &&
232233
typeof navigatorAlias.javaEnabled !== 'unknown' &&
233-
!lodash.isUndefined(navigatorAlias.javaEnabled) &&
234+
!isUndefined(navigatorAlias.javaEnabled) &&
234235
navigatorAlias.javaEnabled()) {
235236
features.java = '1';
236237
}
237238

238239
// Firefox
239-
if (lodash.isFunction(windowAlias.GearsFactory)) {
240+
if (isFunction(windowAlias.GearsFactory)) {
240241
features.gears = '1';
241242
}
242243

src/js/lib/helpers.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@
3333
*/
3434
;(function () {
3535

36-
var
37-
lodash = require('../lib_managed/lodash'),
36+
var
37+
isString = require('lodash/isString'),
38+
isUndefined = require('lodash/isUndefined'),
39+
isObject = require('lodash/isObject'),
3840
cookie = require('browser-cookie-lite'),
3941

4042
object = typeof exports !== 'undefined' ? exports : this; // For eventual node.js environment support
@@ -43,11 +45,11 @@
4345
* Cleans up the page title
4446
*/
4547
object.fixupTitle = function (title) {
46-
if (!lodash.isString(title)) {
48+
if (!isString(title)) {
4749
title = title.text || '';
4850

4951
var tmp = document.getElementsByTagName('title');
50-
if (tmp && !lodash.isUndefined(tmp[0])) {
52+
if (tmp && !isUndefined(tmp[0])) {
5153
title = tmp[0].text;
5254
}
5355
}
@@ -193,7 +195,7 @@
193195
object.getFilter = function (criterion, byClass) {
194196

195197
// If the criterion argument is not an object, add listeners to all elements
196-
if (lodash.isArray(criterion) || !lodash.isObject(criterion)) {
198+
if (Array.isArray(criterion) || !isObject(criterion)) {
197199
return function () {
198200
return true;
199201
};
@@ -204,7 +206,7 @@
204206
} else {
205207
var inclusive = criterion.hasOwnProperty('whitelist');
206208
var specifiedClasses = criterion.whitelist || criterion.blacklist;
207-
if (!lodash.isArray(specifiedClasses)) {
209+
if (!Array.isArray(specifiedClasses)) {
208210
specifiedClasses = [specifiedClasses];
209211
}
210212

@@ -232,7 +234,7 @@
232234
* @param object criterion {transform: function (elt) {return the result of transform function applied to element}
233235
*/
234236
object.getTransform = function (criterion) {
235-
if (!lodash.isObject(criterion)) {
237+
if (!isObject(criterion)) {
236238
return function(x) { return x };
237239
}
238240

src/js/links.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3333
*/
3434

35-
var lodash = require('./lib_managed/lodash'),
35+
var isUndefined = require('lodash/isUndefined'),
3636
helpers = require('./lib/helpers'),
3737
object = typeof exports !== 'undefined' ? exports : this;
3838

@@ -75,12 +75,12 @@ object.getLinkTrackingManager = function (core, trackerId, contextAdder) {
7575
elementContent;
7676

7777
while ((parentElement = sourceElement.parentNode) !== null &&
78-
!lodash.isUndefined(parentElement) && // buggy IE5.5
78+
!isUndefined(parentElement) && // buggy IE5.5
7979
((tag = sourceElement.tagName.toUpperCase()) !== 'A' && tag !== 'AREA')) {
8080
sourceElement = parentElement;
8181
}
8282

83-
if (!lodash.isUndefined(sourceElement.href)) {
83+
if (!isUndefined(sourceElement.href)) {
8484
// browsers, such as Safari, don't downcase hostname and href
8585
var originalSourceHostName = sourceElement.hostname || helpers.getHostName(sourceElement.href),
8686
sourceHostName = originalSourceHostName.toLowerCase(),

src/js/out_queue.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
;(function() {
3636

3737
var
38-
lodash = require('./lib_managed/lodash'),
38+
mapValues = require('lodash/mapValues'),
39+
isString = require('lodash/isString'),
40+
map = require('lodash/map'),
3941
localStorageAccessible = require('./lib/detectors').localStorageAccessible,
4042
helpers = require('./lib/helpers'),
4143
object = typeof exports !== 'undefined' ? exports : this; // For eventual node.js environment support
@@ -82,7 +84,7 @@
8284
}
8385

8486
// Initialize to and empty array if we didn't get anything out of localStorage
85-
if (!lodash.isArray(outQueue)) {
87+
if (!Array.isArray(outQueue)) {
8688
outQueue = [];
8789
}
8890

@@ -130,7 +132,7 @@
130132
* Convert numeric fields to strings to match payload_data schema
131133
*/
132134
function getBody(request) {
133-
var cleanedRequest = lodash.mapValues(request, function (v) {
135+
var cleanedRequest = mapValues(request, function (v) {
134136
return v.toString();
135137
});
136138
return {
@@ -214,7 +216,7 @@
214216
}
215217

216218
// Let's check that we have a Url to ping
217-
if (!lodash.isString(configCollectorUrl)) {
219+
if (!isString(configCollectorUrl)) {
218220
throw "No Snowplow collector configured, cannot track";
219221
}
220222

@@ -265,7 +267,7 @@
265267
}
266268
};
267269

268-
var batch = lodash.map(outQueue.slice(0, numberToSend), function (x) {
270+
var batch = map(outQueue.slice(0, numberToSend), function (x) {
269271
return x.evt;
270272
});
271273

src/js/snowplow.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@
7676
// Load all our modules (at least until we fully modularize & remove grunt-concat)
7777
var
7878
uuid = require('uuid'),
79-
lodash = require('./lib_managed/lodash'),
79+
forEach = require('lodash/forEach'),
80+
filter = require('lodash/filter'),
8081
helpers = require('./lib/helpers'),
8182
queue = require('./in_queue'),
8283
tracker = require('./tracker'),
@@ -127,7 +128,7 @@
127128
var now;
128129

129130
// Flush all POST queues
130-
lodash.forEach(mutSnowplowState.bufferFlushers, function (flusher) {
131+
forEach(mutSnowplowState.bufferFlushers, function (flusher) {
131132
flusher();
132133
});
133134

@@ -140,7 +141,7 @@
140141
// while (Date.now() < mutSnowplowState.expireDateTime) { }
141142
do {
142143
now = new Date();
143-
if (lodash.filter(mutSnowplowState.outQueues, function (queue) {
144+
if (filter(mutSnowplowState.outQueues, function (queue) {
144145
return queue.length > 0;
145146
}).length === 0) {
146147
break;

0 commit comments

Comments
 (0)