Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit 589602b

Browse files
committed
Revert "Remove utils and @ndhoule/keys (#194)"
This reverts commit b2da6b7.
1 parent 52783fe commit 589602b

17 files changed

+919
-188
lines changed

.eslintrc

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,3 @@
11
{
2-
"parser": "@typescript-eslint/parser",
3-
"plugins": [
4-
"@typescript-eslint"
5-
],
6-
"extends": [
7-
"@segment/eslint-config/browser/legacy",
8-
"prettier",
9-
"plugin:@typescript-eslint/recommended"
10-
],
11-
"rules": {
12-
"no-use-before-define": "warn",
13-
"no-var": "warn",
14-
"prefer-const": "warn",
15-
"prefer-rest-params": "warn",
16-
"prefer-spread": "warn",
17-
"strict": "warn",
18-
"@typescript-eslint/adjacent-overload-signatures": "warn",
19-
"@typescript-eslint/ban-ts-comment": "warn",
20-
"@typescript-eslint/ban-types": "warn",
21-
"@typescript-eslint/no-empty-function": "warn",
22-
"@typescript-eslint/no-this-alias": "warn",
23-
"@typescript-eslint/no-var-requires": "warn"
24-
}
2+
"extends": ["@segment/eslint-config/browser/legacy", "prettier"]
253
}

HISTORY.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
<<<<<<< HEAD
2-
<<<<<<< HEAD
3-
<<<<<<< HEAD
4-
51
# 4.1.7 / 2021-03-17
62

73
- Fix Potential DOM-based XSS via prototype pollution
@@ -15,26 +11,14 @@
1511
- Remove `@segment/canonical` in favor of `document.querySelector`
1612
- Replace @ndhoule/defaults with merging via ES6 spread syntax
1713

18-
# 4.1.4 / 2020-09-16
19-
20-
- Replace `@ndhoule/includes` with `lodash.includes`
21-
22-
=======
23-
24-
> > > > > > > parent of c70c3af... Replace @ndhoule/includes with lodash.includes (#211)
25-
2614
# 4.1.3 / 2020-09-16
2715

2816
- Replace `@ndhoule/pick` with `lodash.pick`
2917

30-
=======
31-
>>>>>>> parent of 6e33d3d... Replace @ndhoule/pick with lodash.pick (#210)
3218
# 4.1.2 / 2020-09-16
3319

3420
- Replaces `@ndhoule/extend` with `lodash.assignin`
3521

36-
=======
37-
>>>>>>> parent of 017124b... Replace @ndhoule/extend with lodash.assignin (#206)
3822
# 4.1.1 / 2020-09-15
3923

4024
- Enable `esModuleInterop` in `tsconfig.json`

lib/analytics.ts

Lines changed: 48 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import {
99
} from './types';
1010
var url = require('component-url');
1111

12-
import cloneDeep from 'lodash.clonedeep';
13-
1412
var _analytics = global.analytics;
1513

1614
/*
@@ -30,13 +28,17 @@ var DestinationMiddlewareChain = require('./middleware')
3028
var Page = require('segmentio-facade').Page;
3129
var Track = require('segmentio-facade').Track;
3230
var bindAll = require('bind-all');
31+
var clone = require('./utils/clone');
3332
var extend = require('extend');
3433
var cookie = require('./cookie');
3534
var metrics = require('./metrics');
3635
var debug = require('debug');
36+
var defaults = require('@ndhoule/defaults');
37+
var each = require('./utils/each');
3738
var group = require('./group');
3839
var is = require('is');
3940
var isMeta = require('@segment/is-meta');
41+
var keys = require('@ndhoule/keys');
4042
var memory = require('./memory');
4143
var nextTick = require('next-tick');
4244
var normalize = require('./normalize');
@@ -67,8 +69,8 @@ function Analytics() {
6769
this.log = debug('analytics.js');
6870
bindAll(this);
6971

70-
const self = this;
71-
this.on('initialize', function(_, options) {
72+
var self = this;
73+
this.on('initialize', function(settings, options) {
7274
if (options.initialPageview) self.page();
7375
self._parseQuery(window.location.search);
7476
});
@@ -167,16 +169,13 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
167169

168170
// clean unknown integrations from settings
169171
var self = this;
170-
Object.keys(settings).forEach(key => {
171-
var Integration = self.Integrations[key];
172-
if (!Integration) delete settings[key];
173-
});
172+
each(function(_opts: unknown, name: string | number) {
173+
var Integration = self.Integrations[name];
174+
if (!Integration) delete settings[name];
175+
}, settings);
174176

175177
// add integrations
176-
Object.keys(settings).forEach(key => {
177-
const opts = settings[key];
178-
const name = key;
179-
178+
each(function(opts: unknown, name: string | number) {
180179
// Don't load disabled integrations
181180
if (options.integrations) {
182181
if (
@@ -187,13 +186,13 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
187186
}
188187
}
189188

190-
const Integration = self.Integrations[name];
191-
const clonedOpts = {};
189+
var Integration = self.Integrations[name];
190+
var clonedOpts = {};
192191
extend(true, clonedOpts, opts); // deep clone opts
193-
const integration = new Integration(clonedOpts);
192+
var integration = new Integration(clonedOpts);
194193
self.log('initialize %o - %o', name, opts);
195194
self.add(integration);
196-
});
195+
}, settings);
197196

198197
var integrations = this._integrations;
199198

@@ -203,7 +202,7 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
203202

204203
// make ready callback
205204
var readyCallCount = 0;
206-
var integrationCount = Object.keys(integrations).length;
205+
var integrationCount = keys(integrations).length;
207206
var ready = function() {
208207
readyCallCount++;
209208
if (readyCallCount >= integrationCount) {
@@ -220,15 +219,14 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
220219
// initialize integrations, passing ready
221220
// create a list of any integrations that did not initialize - this will be passed with all events for replay support:
222221
this.failedInitializations = [];
223-
let initialPageSkipped = false;
224-
Object.keys(integrations).forEach(key => {
225-
const integration = integrations[key];
222+
var initialPageSkipped = false;
223+
each(function(integration) {
226224
if (
227225
options.initialPageview &&
228226
integration.options.initialPageview === false
229227
) {
230228
// We've assumed one initial pageview, so make sure we don't count the first page call.
231-
let page = integration.page;
229+
var page = integration.page;
232230
integration.page = function() {
233231
if (initialPageSkipped) {
234232
return page.apply(this, arguments);
@@ -248,7 +246,7 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
248246
});
249247
integration.initialize();
250248
} catch (e) {
251-
let integrationName = integration.name;
249+
var integrationName = integration.name;
252250
metrics.increment('analytics_js.integration.invoke.error', {
253251
method: 'initialize',
254252
integration_name: integration.name
@@ -259,7 +257,7 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
259257

260258
integration.ready();
261259
}
262-
});
260+
}, integrations);
263261

264262
// backwards compat with angular plugin and used for init logic checks
265263
this.initialized = true;
@@ -468,44 +466,37 @@ Analytics.prototype.track = function(
468466
*/
469467

470468
Analytics.prototype.trackClick = Analytics.prototype.trackLink = function(
471-
links: Element | Array<Element> | JQuery,
469+
links: Element | Array<unknown>,
472470
event: any,
473471
properties?: any
474472
): SegmentAnalytics {
475-
let elements: Array<Element> = [];
476473
if (!links) return this;
477474
// always arrays, handles jquery
478-
if (links instanceof Element) {
479-
elements = [links];
480-
} else if ('toArray' in links) {
481-
elements = links.toArray();
482-
} else {
483-
elements = links as Array<Element>;
484-
}
475+
if (type(links) === 'element') links = [links];
485476

486-
elements.forEach(el => {
477+
var self = this;
478+
each(function(el) {
487479
if (type(el) !== 'element') {
488480
throw new TypeError('Must pass HTMLElement to `analytics.trackLink`.');
489481
}
490-
on(el, 'click', e => {
491-
const ev = is.fn(event) ? event(el) : event;
492-
const props = is.fn(properties) ? properties(el) : properties;
493-
const href =
482+
on(el, 'click', function(e) {
483+
var ev = is.fn(event) ? event(el) : event;
484+
var props = is.fn(properties) ? properties(el) : properties;
485+
var href =
494486
el.getAttribute('href') ||
495487
el.getAttributeNS('http://www.w3.org/1999/xlink', 'href') ||
496488
el.getAttribute('xlink:href');
497489

498-
this.track(ev, props);
490+
self.track(ev, props);
499491

500-
// @ts-ignore
501492
if (href && el.target !== '_blank' && !isMeta(e)) {
502493
prevent(e);
503-
this._callback(function() {
494+
self._callback(function() {
504495
window.location.href = href;
505496
});
506497
}
507498
});
508-
});
499+
}, links);
509500

510501
return this;
511502
};
@@ -531,22 +522,21 @@ Analytics.prototype.trackSubmit = Analytics.prototype.trackForm = function(
531522
// always arrays, handles jquery
532523
if (type(forms) === 'element') forms = [forms];
533524

534-
const elements = forms as Array<unknown>;
535-
536-
elements.forEach((el: { submit: () => void }) => {
525+
var self = this;
526+
each(function(el: { submit: () => void }) {
537527
if (type(el) !== 'element')
538528
throw new TypeError('Must pass HTMLElement to `analytics.trackForm`.');
539-
const handler = e => {
529+
function handler(e) {
540530
prevent(e);
541531

542-
const ev = is.fn(event) ? event(el) : event;
543-
const props = is.fn(properties) ? properties(el) : properties;
544-
this.track(ev, props);
532+
var ev = is.fn(event) ? event(el) : event;
533+
var props = is.fn(properties) ? properties(el) : properties;
534+
self.track(ev, props);
545535

546-
this._callback(function() {
536+
self._callback(function() {
547537
el.submit();
548538
});
549-
};
539+
}
550540

551541
// Support the events happening through jQuery or Zepto instead of through
552542
// the normal DOM API, because `el.submit` doesn't bubble up events...
@@ -556,7 +546,7 @@ Analytics.prototype.trackSubmit = Analytics.prototype.trackForm = function(
556546
} else {
557547
on(el, 'submit', handler);
558548
}
559-
});
549+
}, forms);
560550

561551
return this;
562552
};
@@ -593,7 +583,7 @@ Analytics.prototype.page = function(
593583
(name = category), (category = null);
594584
/* eslint-enable no-unused-expressions, no-sequences */
595585

596-
properties = cloneDeep(properties) || {};
586+
properties = clone(properties) || {};
597587
if (name) properties.name = name;
598588
if (category) properties.category = category;
599589

@@ -605,7 +595,7 @@ Analytics.prototype.page = function(
605595
// Mirror user overrides to `options.context.page` (but exclude custom properties)
606596
// (Any page defaults get applied in `this.normalize` for consistency.)
607597
// Weird, yeah--moving special props to `context.page` will fix this in the long term.
608-
var overrides = pick(Object.keys(defs), properties);
598+
var overrides = pick(keys(defs), properties);
609599
if (!is.empty(overrides)) {
610600
options = options || {};
611601
options.context = options.context || {};
@@ -806,11 +796,9 @@ Analytics.prototype._invoke = function(
806796
return this;
807797

808798
function applyIntegrationMiddlewares(facade) {
809-
let failedInitializations = self.failedInitializations || [];
810-
Object.keys(self._integrations).forEach(key => {
811-
const integration = self._integrations[key];
812-
const { name } = integration;
813-
const facadeCopy = extend(true, new Facade({}), facade);
799+
var failedInitializations = self.failedInitializations || [];
800+
each(function(integration, name) {
801+
var facadeCopy = extend(true, new Facade({}), facade);
814802

815803
if (!facadeCopy.enabled(name)) return;
816804
// Check if an integration failed to initialize.
@@ -894,7 +882,7 @@ Analytics.prototype._invoke = function(
894882
);
895883
}
896884
}
897-
});
885+
}, self._integrations);
898886
}
899887
};
900888

@@ -976,7 +964,7 @@ Analytics.prototype.normalize = function(msg: {
976964
context: { page };
977965
anonymousId: string;
978966
}): object {
979-
msg = normalize(msg, Object.keys(this._integrations));
967+
msg = normalize(msg, keys(this._integrations));
980968
if (msg.anonymousId) user.anonymousId(msg.anonymousId);
981969
msg.anonymousId = user.anonymousId();
982970

lib/cookie.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'use strict';
22

33
import { CookieOptions } from './types';
4-
import cloneDeep from 'lodash.clonedeep'
54

65
/**
76
* Module dependencies.
87
*/
98

109
var bindAll = require('bind-all');
10+
var clone = require('./utils/clone');
1111
var cookie = require('@segment/cookie');
1212
var debug = require('debug')('analytics.js:cookie');
1313
var defaults = require('@ndhoule/defaults');
@@ -65,7 +65,7 @@ Cookie.prototype.options = function(options?: CookieOptions) {
6565
Cookie.prototype.set = function(key: string, value?: object | string): boolean {
6666
try {
6767
value = window.JSON.stringify(value);
68-
cookie(key, value === 'null' ? null : value, cloneDeep(this._options));
68+
cookie(key, value === 'null' ? null : value, clone(this._options));
6969
return true;
7070
} catch (e) {
7171
return false;
@@ -92,7 +92,7 @@ Cookie.prototype.get = function(key: string): object {
9292

9393
Cookie.prototype.remove = function(key: string): boolean {
9494
try {
95-
cookie(key, null, cloneDeep(this._options));
95+
cookie(key, null, clone(this._options));
9696
return true;
9797
} catch (e) {
9898
return false;

lib/entity.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
22

33
import { InitOptions } from './types';
4-
import cloneDeep from 'lodash.clonedeep'
54

65
/*
76
* Module dependencies.
87
*/
98

9+
var clone = require('./utils/clone');
1010
var cookie = require('./cookie');
1111
var debug = require('debug')('analytics:entity');
1212
var defaults = require('@ndhoule/defaults');
@@ -198,7 +198,7 @@ Entity.prototype._getTraits = function(): object {
198198
var ret = this._options.persist
199199
? store.get(this._options.localStorage.key)
200200
: this._traits;
201-
return ret ? isodateTraverse(cloneDeep(ret)) : {};
201+
return ret ? isodateTraverse(clone(ret)) : {};
202202
};
203203

204204
/**

0 commit comments

Comments
 (0)