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

Commit b2da6b7

Browse files
Remove utils and @ndhoule/keys (#194)
1 parent 592dc1d commit b2da6b7

17 files changed

+349
-818
lines changed

.eslintrc

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
11
{
2-
"extends": ["@segment/eslint-config/browser/legacy", "prettier"]
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+
}
325
}

HISTORY.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# 4.1.0 / 2020-09-14
2+
3+
- Replaces `utils/clone` with `lodash.deepclone`
4+
- Replaces `utils/map` with `Array.prototype.map`
5+
- Replaces `utils/each` with `Array.prototype.each`
6+
- Removes the `utils` directory and tests
7+
18
# 4.0.4 / 2020-09-11
29

310
- Change the arguments of the main methods to be optional in the typedef to match the documentation. (#203)

lib/analytics.ts

+60-47
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
SegmentIntegration
99
} from './types';
1010

11+
import cloneDeep from 'lodash.clonedeep'
12+
1113
var _analytics = global.analytics;
1214

1315
/*
@@ -27,18 +29,14 @@ var DestinationMiddlewareChain = require('./middleware')
2729
var Page = require('segmentio-facade').Page;
2830
var Track = require('segmentio-facade').Track;
2931
var bindAll = require('bind-all');
30-
var clone = require('./utils/clone');
3132
var extend = require('extend');
3233
var cookie = require('./cookie');
3334
var metrics = require('./metrics');
3435
var debug = require('debug');
3536
var defaults = require('@ndhoule/defaults');
36-
var each = require('./utils/each');
37-
var foldl = require('@ndhoule/foldl');
3837
var group = require('./group');
3938
var is = require('is');
4039
var isMeta = require('@segment/is-meta');
41-
var keys = require('@ndhoule/keys');
4240
var memory = require('./memory');
4341
var nextTick = require('next-tick');
4442
var normalize = require('./normalize');
@@ -69,8 +67,9 @@ function Analytics() {
6967
this.log = debug('analytics.js');
7068
bindAll(this);
7169

72-
var self = this;
73-
this.on('initialize', function(settings, options) {
70+
71+
const self = this;
72+
this.on('initialize', function(_, options) {
7473
if (options.initialPageview) self.page();
7574
self._parseQuery(window.location.search);
7675
});
@@ -169,13 +168,16 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
169168

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

177176
// add integrations
178-
each(function(opts: unknown, name: string | number) {
177+
Object.keys(settings).forEach(key => {
178+
const opts = settings[key]
179+
const name = key
180+
179181
// Don't load disabled integrations
180182
if (options.integrations) {
181183
if (
@@ -186,13 +188,13 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
186188
}
187189
}
188190

189-
var Integration = self.Integrations[name];
190-
var clonedOpts = {};
191+
const Integration = self.Integrations[name];
192+
const clonedOpts = {};
191193
extend(true, clonedOpts, opts); // deep clone opts
192-
var integration = new Integration(clonedOpts);
194+
const integration = new Integration(clonedOpts);
193195
self.log('initialize %o - %o', name, opts);
194196
self.add(integration);
195-
}, settings);
197+
});
196198

197199
var integrations = this._integrations;
198200

@@ -202,7 +204,7 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
202204

203205
// make ready callback
204206
var readyCallCount = 0;
205-
var integrationCount = keys(integrations).length;
207+
var integrationCount = Object.keys(integrations).length;
206208
var ready = function() {
207209
readyCallCount++;
208210
if (readyCallCount >= integrationCount) {
@@ -219,14 +221,15 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
219221
// initialize integrations, passing ready
220222
// create a list of any integrations that did not initialize - this will be passed with all events for replay support:
221223
this.failedInitializations = [];
222-
var initialPageSkipped = false;
223-
each(function(integration) {
224+
let initialPageSkipped = false;
225+
Object.keys(integrations).forEach(key => {
226+
const integration = integrations[key]
224227
if (
225228
options.initialPageview &&
226229
integration.options.initialPageview === false
227230
) {
228231
// We've assumed one initial pageview, so make sure we don't count the first page call.
229-
var page = integration.page;
232+
let page = integration.page;
230233
integration.page = function() {
231234
if (initialPageSkipped) {
232235
return page.apply(this, arguments);
@@ -246,7 +249,7 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
246249
});
247250
integration.initialize();
248251
} catch (e) {
249-
var integrationName = integration.name;
252+
let integrationName = integration.name;
250253
metrics.increment('analytics_js.integration.invoke.error', {
251254
method: 'initialize',
252255
integration_name: integration.name
@@ -257,7 +260,7 @@ Analytics.prototype.init = Analytics.prototype.initialize = function(
257260

258261
integration.ready();
259262
}
260-
}, integrations);
263+
});
261264

262265
// backwards compat with angular plugin and used for init logic checks
263266
this.initialized = true;
@@ -466,37 +469,44 @@ Analytics.prototype.track = function(
466469
*/
467470

468471
Analytics.prototype.trackClick = Analytics.prototype.trackLink = function(
469-
links: Element | Array<unknown>,
472+
links: Element | Array<Element> | JQuery,
470473
event: any,
471474
properties?: any
472475
): SegmentAnalytics {
476+
let elements: Array<Element> = []
473477
if (!links) return this;
474478
// always arrays, handles jquery
475-
if (type(links) === 'element') links = [links];
479+
if (links instanceof Element) {
480+
elements = [links]
481+
} else if ("toArray" in links) {
482+
elements = links.toArray()
483+
} else {
484+
elements = links as Array<Element>
485+
}
476486

477-
var self = this;
478-
each(function(el) {
487+
elements.forEach(el => {
479488
if (type(el) !== 'element') {
480489
throw new TypeError('Must pass HTMLElement to `analytics.trackLink`.');
481490
}
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 =
491+
on(el, 'click', (e) => {
492+
const ev = is.fn(event) ? event(el) : event;
493+
const props = is.fn(properties) ? properties(el) : properties;
494+
const href =
486495
el.getAttribute('href') ||
487496
el.getAttributeNS('http://www.w3.org/1999/xlink', 'href') ||
488497
el.getAttribute('xlink:href');
489498

490-
self.track(ev, props);
499+
this.track(ev, props);
491500

501+
// @ts-ignore
492502
if (href && el.target !== '_blank' && !isMeta(e)) {
493503
prevent(e);
494-
self._callback(function() {
504+
this._callback(function() {
495505
window.location.href = href;
496506
});
497507
}
498508
});
499-
}, links);
509+
});
500510

501511
return this;
502512
};
@@ -522,18 +532,19 @@ Analytics.prototype.trackSubmit = Analytics.prototype.trackForm = function(
522532
// always arrays, handles jquery
523533
if (type(forms) === 'element') forms = [forms];
524534

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

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

536-
self._callback(function() {
547+
this._callback(function() {
537548
el.submit();
538549
});
539550
}
@@ -546,7 +557,7 @@ Analytics.prototype.trackSubmit = Analytics.prototype.trackForm = function(
546557
} else {
547558
on(el, 'submit', handler);
548559
}
549-
}, forms);
560+
});
550561

551562
return this;
552563
};
@@ -583,7 +594,7 @@ Analytics.prototype.page = function(
583594
(name = category), (category = null);
584595
/* eslint-enable no-unused-expressions, no-sequences */
585596

586-
properties = clone(properties) || {};
597+
properties = cloneDeep(properties) || {};
587598
if (name) properties.name = name;
588599
if (category) properties.category = category;
589600

@@ -595,7 +606,7 @@ Analytics.prototype.page = function(
595606
// Mirror user overrides to `options.context.page` (but exclude custom properties)
596607
// (Any page defaults get applied in `this.normalize` for consistency.)
597608
// Weird, yeah--moving special props to `context.page` will fix this in the long term.
598-
var overrides = pick(keys(defs), properties);
609+
var overrides = pick(Object.keys(defs), properties);
599610
if (!is.empty(overrides)) {
600611
options = options || {};
601612
options.context = options.context || {};
@@ -796,9 +807,11 @@ Analytics.prototype._invoke = function(
796807
return this;
797808

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

803816
if (!facadeCopy.enabled(name)) return;
804817
// Check if an integration failed to initialize.
@@ -882,7 +895,7 @@ Analytics.prototype._invoke = function(
882895
);
883896
}
884897
}
885-
}, self._integrations);
898+
});
886899
}
887900
};
888901

@@ -957,7 +970,7 @@ Analytics.prototype.normalize = function(msg: {
957970
context: { page };
958971
anonymousId: string;
959972
}): object {
960-
msg = normalize(msg, keys(this._integrations));
973+
msg = normalize(msg, Object.keys(this._integrations));
961974
if (msg.anonymousId) user.anonymousId(msg.anonymousId);
962975
msg.anonymousId = user.anonymousId();
963976

lib/cookie.ts

+3-3
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'
45

56
/**
67
* Module dependencies.
78
*/
89

910
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, clone(this._options));
68+
cookie(key, value === 'null' ? null : value, cloneDeep(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, clone(this._options));
95+
cookie(key, null, cloneDeep(this._options));
9696
return true;
9797
} catch (e) {
9898
return false;

lib/entity.ts

+2-2
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'
45

56
/*
67
* Module dependencies.
78
*/
89

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(clone(ret)) : {};
201+
return ret ? isodateTraverse(cloneDeep(ret)) : {};
202202
};
203203

204204
/**

lib/memory.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
* Module Dependencies.
55
*/
66

7+
import cloneDeep from 'lodash.clonedeep'
8+
79
var bindAll = require('bind-all');
8-
var clone = require('./utils/clone');
910

1011
/**
1112
* HOP.
@@ -32,7 +33,7 @@ function Memory() {
3233
*/
3334

3435
Memory.prototype.set = function(key: string, value: unknown): boolean {
35-
this.store[key] = clone(value);
36+
this.store[key] = cloneDeep(value);
3637
return true;
3738
};
3839

@@ -42,7 +43,7 @@ Memory.prototype.set = function(key: string, value: unknown): boolean {
4243

4344
Memory.prototype.get = function(key: string): unknown | undefined {
4445
if (!has.call(this.store, key)) return;
45-
return clone(this.store[key]);
46+
return cloneDeep(this.store[key]);
4647
};
4748

4849
/**

0 commit comments

Comments
 (0)