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

Commit f1bfcd9

Browse files
author
Bryan Mikaelian
committed
Replace @ndhoulse/defaults with lodash.assign and ES6 spread syntax merging
1 parent 1890f82 commit f1bfcd9

10 files changed

+111
-39
lines changed

lib/analytics.ts

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
InitOptions,
66
SegmentAnalytics,
77
SegmentOpts,
8-
SegmentIntegration
8+
SegmentIntegration,
9+
PageDefaults
910
} from './types';
1011

1112
var _analytics = global.analytics;
@@ -32,7 +33,6 @@ var extend = require('extend');
3233
var cookie = require('./cookie');
3334
var metrics = require('./metrics');
3435
var debug = require('debug');
35-
var defaults = require('@ndhoule/defaults');
3636
var each = require('./utils/each');
3737
var foldl = require('@ndhoule/foldl');
3838
var group = require('./group');
@@ -50,6 +50,7 @@ var querystring = require('component-querystring');
5050
var store = require('./store');
5151
var user = require('./user');
5252
var type = require('component-type');
53+
var assign = require('lodash.assign')
5354

5455
/**
5556
* Initialize a new `Analytics` instance.
@@ -320,8 +321,13 @@ Analytics.prototype.identify = function(
320321
});
321322

322323
// Add the initialize integrations so the server-side ones can be disabled too
324+
// NOTE: We need to merge integrations, not override them with assign
325+
// since it is possible to change the initialized integrations at runtime.
323326
if (this.options.integrations) {
324-
defaults(msg.integrations, this.options.integrations);
327+
msg.integrations = {
328+
...this.options.integrations,
329+
...msg.integrations
330+
}
325331
}
326332

327333
this._invoke('identify', new Identify(msg));
@@ -376,8 +382,13 @@ Analytics.prototype.group = function(
376382
});
377383

378384
// Add the initialize integrations so the server-side ones can be disabled too
385+
// NOTE: We need to merge integrations, not override them with assign
386+
// since it is possible to change the initialized integrations at runtime.
379387
if (this.options.integrations) {
380-
defaults(msg.integrations, this.options.integrations);
388+
msg.integrations = {
389+
...this.options.integrations,
390+
...msg.integrations
391+
}
381392
}
382393

383394
this._invoke('group', new Group(msg));
@@ -441,10 +452,12 @@ Analytics.prototype.track = function(
441452
}
442453

443454
// Add the initialize integrations so the server-side ones can be disabled too
444-
defaults(
445-
msg.integrations,
446-
this._mergeInitializeAndPlanIntegrations(planIntegrationOptions)
447-
);
455+
// NOTE: We need to merge integrations, not override them with assign
456+
// since it is possible to change the initialized integrations at runtime.
457+
msg.integrations = {
458+
...this._mergeInitializeAndPlanIntegrations(planIntegrationOptions),
459+
...msg.integrations
460+
}
448461

449462
this._invoke('track', new Track(msg));
450463

@@ -590,7 +603,7 @@ Analytics.prototype.page = function(
590603
// Ensure properties has baseline spec properties.
591604
// TODO: Eventually move these entirely to `options.context.page`
592605
var defs = pageDefaults();
593-
defaults(properties, defs);
606+
assign(properties, defs)
594607

595608
// Mirror user overrides to `options.context.page` (but exclude custom properties)
596609
// (Any page defaults get applied in `this.normalize` for consistency.)
@@ -610,8 +623,13 @@ Analytics.prototype.page = function(
610623
});
611624

612625
// Add the initialize integrations so the server-side ones can be disabled too
626+
// NOTE: We need to merge integrations, not override them with assign
627+
// since it is possible to change the initialized integrations at runtime.
613628
if (this.options.integrations) {
614-
defaults(msg.integrations, this.options.integrations);
629+
msg.integrations = {
630+
...this.options.integrations,
631+
...msg.integrations
632+
}
615633
}
616634

617635
this._invoke('page', new Page(msg));
@@ -663,8 +681,13 @@ Analytics.prototype.alias = function(
663681
});
664682

665683
// Add the initialize integrations so the server-side ones can be disabled too
684+
// NOTE: We need to merge integrations, not override them with assign
685+
// since it is possible to change the initialized integrations at runtime.
666686
if (this.options.integrations) {
667-
defaults(msg.integrations, this.options.integrations);
687+
msg.integrations = {
688+
...this.options.integrations,
689+
...msg.integrations
690+
}
668691
}
669692

670693
this._invoke('alias', new Alias(msg));
@@ -958,15 +981,18 @@ Analytics.prototype._parseQuery = function(query: string): SegmentAnalytics {
958981
*/
959982

960983
Analytics.prototype.normalize = function(msg: {
961-
context: { page };
984+
context: { page: Partial<PageDefaults> };
962985
anonymousId: string;
963986
}): object {
964987
msg = normalize(msg, keys(this._integrations));
965988
if (msg.anonymousId) user.anonymousId(msg.anonymousId);
966989
msg.anonymousId = user.anonymousId();
967990

968991
// Ensure all outgoing requests include page data in their contexts.
969-
msg.context.page = defaults(msg.context.page || {}, pageDefaults());
992+
msg.context.page = {
993+
...pageDefaults(),
994+
...msg.context.page
995+
};
970996

971997
return msg;
972998
};

lib/cookie.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ var bindAll = require('bind-all');
1010
var clone = require('./utils/clone');
1111
var cookie = require('@segment/cookie');
1212
var debug = require('debug')('analytics.js:cookie');
13-
var defaults = require('@ndhoule/defaults');
1413
var topDomain = require('@segment/top-domain');
14+
var assign = require('lodash.assign')
15+
16+
const MAX_AGE_ONE_YEAR = 31536000000
1517

1618
/**
1719
* Initialize a new `Cookie` with `options`.
@@ -32,16 +34,17 @@ Cookie.prototype.options = function(options?: CookieOptions) {
3234

3335
options = options || {};
3436

35-
var domain = '.' + topDomain(window.location.href);
37+
let domain = '.' + topDomain(window.location.href);
3638
if (domain === '.') domain = null;
3739

38-
this._options = defaults(options, {
39-
// default to a year
40-
maxage: 31536000000,
41-
path: '/',
40+
const defaults: CookieOptions = {
41+
maxage: MAX_AGE_ONE_YEAR,
4242
domain: domain,
43+
path: '/',
4344
sameSite: 'Lax'
44-
});
45+
}
46+
47+
this._options = assign(defaults, options);
4548

4649
// http://curl.haxx.se/rfc/cookie_spec.html
4750
// https://publicsuffix.org/list/effective_tld_names.dat

lib/entity.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { InitOptions } from './types';
99
var clone = require('./utils/clone');
1010
var cookie = require('./cookie');
1111
var debug = require('debug')('analytics:entity');
12-
var defaults = require('@ndhoule/defaults');
1312
var extend = require('@ndhoule/extend');
1413
var memory = require('./memory');
1514
var store = require('./store');
@@ -74,7 +73,10 @@ Entity.prototype.storage = function() {
7473

7574
Entity.prototype.options = function(options?: InitOptions) {
7675
if (arguments.length === 0) return this._options;
77-
this._options = defaults(options || {}, this.defaults || {});
76+
this._options = {
77+
...this.defaults,
78+
...options
79+
}
7880
};
7981

8082
/**

lib/normalize.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import { Message } from './types';
77
*/
88

99
var debug = require('debug')('analytics.js:normalize');
10-
var defaults = require('@ndhoule/defaults');
1110
var each = require('./utils/each');
1211
var includes = require('@ndhoule/includes');
1312
var map = require('./utils/map');
1413
var type = require('component-type');
1514
var uuid = require('uuid/v4');
1615
var md5 = require('spark-md5').hash;
16+
var assign = require('lodash.assign')
17+
1718

1819
/**
1920
* HOP.
@@ -92,7 +93,7 @@ function normalize(msg: Message, list: Array<any>): NormalizedMessage {
9293
delete msg.options;
9394
ret.integrations = integrations;
9495
ret.context = context;
95-
ret = defaults(ret, msg);
96+
ret = assign(msg, ret);
9697
debug('->', ret);
9798
return ret;
9899

lib/store.ts

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

3+
import { StoreOptions } from './types';
4+
35
/*
46
* Module dependencies.
57
*/
68

79
var bindAll = require('bind-all');
8-
var defaults = require('@ndhoule/defaults');
910
var store = require('@segment/store');
11+
var assign = require('lodash.assign')
1012

1113
/**
1214
* Initialize a new `Store` with `options`.
@@ -22,11 +24,11 @@ function Store(options?: { enabled: boolean }) {
2224
* Set the `options` for the store.
2325
*/
2426

25-
Store.prototype.options = function(options?: { enabled?: boolean }) {
27+
Store.prototype.options = function(options?: StoreOptions) {
2628
if (arguments.length === 0) return this._options;
2729

2830
options = options || {};
29-
defaults(options, { enabled: true });
31+
options = assign({ enabled: true }, options);
3032

3133
this.enabled = options.enabled && store.enabled;
3234
this._options = options;

lib/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface CookieOptions {
1515
domain?: string;
1616
path?: string;
1717
secure?: boolean;
18+
sameSite?: string
1819
}
1920

2021
export interface MetricsOptions {
@@ -24,7 +25,7 @@ export interface MetricsOptions {
2425
maxQueueSize?: number;
2526
}
2627

27-
interface StoreOptions {
28+
export interface StoreOptions {
2829
enabled?: boolean;
2930
}
3031

test/analytics.test.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var pageDefaults = require('../build/pageDefaults');
1313
var sinon = require('sinon');
1414
var tick = require('next-tick');
1515
var trigger = require('compat-trigger-event');
16+
var assign = require('lodash.assign');
1617

1718
var Identify = Facade.Identify;
1819
var cookie = Analytics.cookie;
@@ -774,17 +775,6 @@ describe('Analytics', function() {
774775
assert.deepEqual(page.options('AdRoll'), { opt: true });
775776
});
776777

777-
it('should use the initialize .integrations option', function() {
778-
analytics.initialize(settings, {
779-
integrations: {
780-
Test: false
781-
}
782-
});
783-
analytics.page({ prop: true });
784-
var page = analytics._invoke.args[0][1];
785-
assert.deepEqual(page.obj.integrations, { Test: false });
786-
});
787-
788778
it('should be able to override the initialize .integrations option', function() {
789779
analytics.initialize(settings, {
790780
integrations: {
@@ -1611,6 +1601,26 @@ describe('Analytics', function() {
16111601
assert.deepEqual(track.context(), { page: contextPage });
16121602
});
16131603

1604+
it('should support overwriting context.page fields', function() {
1605+
analytics.track(
1606+
'event',
1607+
{},
1608+
{
1609+
context: {
1610+
page: {
1611+
title: 'lol'
1612+
}
1613+
}
1614+
}
1615+
);
1616+
1617+
var track = analytics._invoke.args[0][1];
1618+
assert.deepEqual(
1619+
track.context().page,
1620+
assign(contextPage, { title: 'lol' })
1621+
);
1622+
});
1623+
16141624
it('should accept context.traits', function() {
16151625
analytics.track('event', { prop: 1 }, { traits: { trait: true } });
16161626
var track = analytics._invoke.args[0][1];

test/cookie.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ describe('cookie', function() {
6363
assert(cookie.options().maxage === 31536000000);
6464
});
6565

66+
it('should have default options', function() {
67+
cookie.options({ domain: '' });
68+
69+
assert(cookie.options().maxage === 31536000000);
70+
assert(cookie.options().path === '/');
71+
assert(cookie.options().domain === '');
72+
assert(cookie.options().sameSite === 'Lax');
73+
});
74+
6675
it('should set the domain correctly', function() {
6776
cookie.options({ domain: '' });
6877
assert(cookie.options().domain === '');

test/normalize.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ describe('normalize', function() {
6464
}
6565
});
6666
});
67+
68+
it('should merge with defaults', function() {
69+
opts.context = { foo: 5 };
70+
var out = normalize(msg, list);
71+
assert.deepEqual(out.integrations, {});
72+
assert.deepEqual(out.context, { foo: 5 });
73+
74+
msg.options = { integrations: { Segment: true }, context: { foo: 6 } };
75+
out = normalize(msg, list);
76+
assert.deepEqual(out.integrations, { Segment: true });
77+
assert.deepEqual(out.context, { foo: 6 });
78+
});
6779
});
6880

6981
describe('integrations', function() {

test/store.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,11 @@ describe('store', function() {
4343
assert(store.options().enabled === false);
4444
assert(store.enabled === false);
4545
});
46+
47+
it('should have default options', function() {
48+
store.options({});
49+
50+
assert(store.options().enabled);
51+
});
4652
});
4753
});

0 commit comments

Comments
 (0)