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

Commit e042373

Browse files
author
Bryan Mikaelian
committed
Replace @ndhoulse/defaults with Object.assign and ES6 object merging
1 parent 7d9fdc9 commit e042373

10 files changed

+105
-39
lines changed

lib/analytics.ts

+38-13
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');
@@ -320,8 +320,13 @@ Analytics.prototype.identify = function(
320320
});
321321

322322
// Add the initialize integrations so the server-side ones can be disabled too
323+
// NOTE: We need to merge integrations, not override them with Object.assign
324+
// since it is possible to change the initialized integrations at runtime.
323325
if (this.options.integrations) {
324-
defaults(msg.integrations, this.options.integrations);
326+
msg.integrations = {
327+
...this.options.integrations,
328+
...msg.integrations
329+
}
325330
}
326331

327332
this._invoke('identify', new Identify(msg));
@@ -376,8 +381,13 @@ Analytics.prototype.group = function(
376381
});
377382

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

383393
this._invoke('group', new Group(msg));
@@ -441,10 +451,12 @@ Analytics.prototype.track = function(
441451
}
442452

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

449461
this._invoke('track', new Track(msg));
450462

@@ -590,7 +602,7 @@ Analytics.prototype.page = function(
590602
// Ensure properties has baseline spec properties.
591603
// TODO: Eventually move these entirely to `options.context.page`
592604
var defs = pageDefaults();
593-
defaults(properties, defs);
605+
Object.assign(properties, defs)
594606

595607
// Mirror user overrides to `options.context.page` (but exclude custom properties)
596608
// (Any page defaults get applied in `this.normalize` for consistency.)
@@ -610,8 +622,13 @@ Analytics.prototype.page = function(
610622
});
611623

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

617634
this._invoke('page', new Page(msg));
@@ -663,8 +680,13 @@ Analytics.prototype.alias = function(
663680
});
664681

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

670692
this._invoke('alias', new Alias(msg));
@@ -958,15 +980,18 @@ Analytics.prototype._parseQuery = function(query: string): SegmentAnalytics {
958980
*/
959981

960982
Analytics.prototype.normalize = function(msg: {
961-
context: { page };
983+
context: { page: Partial<PageDefaults> };
962984
anonymousId: string;
963985
}): object {
964986
msg = normalize(msg, keys(this._integrations));
965987
if (msg.anonymousId) user.anonymousId(msg.anonymousId);
966988
msg.anonymousId = user.anonymousId();
967989

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

971996
return msg;
972997
};

lib/cookie.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -10,9 +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');
1514

15+
const MAX_AGE_ONE_YEAR = 31536000000
16+
1617
/**
1718
* Initialize a new `Cookie` with `options`.
1819
*
@@ -32,16 +33,17 @@ Cookie.prototype.options = function(options?: CookieOptions) {
3233

3334
options = options || {};
3435

35-
var domain = '.' + topDomain(window.location.href);
36+
let domain = '.' + topDomain(window.location.href);
3637
if (domain === '.') domain = null;
3738

38-
this._options = defaults(options, {
39-
// default to a year
40-
maxage: 31536000000,
41-
path: '/',
39+
const defaults: CookieOptions = {
40+
maxage: MAX_AGE_ONE_YEAR,
4241
domain: domain,
42+
path: '/',
4343
sameSite: 'Lax'
44-
});
44+
}
45+
46+
this._options = Object.assign(defaults, options);
4547

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

lib/entity.ts

+4-2
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

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ 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');
@@ -92,7 +91,7 @@ function normalize(msg: Message, list: Array<any>): NormalizedMessage {
9291
delete msg.options;
9392
ret.integrations = integrations;
9493
ret.context = context;
95-
ret = defaults(ret, msg);
94+
ret = Object.assign(msg, ret);
9695
debug('->', ret);
9796
return ret;
9897

lib/store.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
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');
1011

1112
/**
@@ -22,11 +23,11 @@ function Store(options?: { enabled: boolean }) {
2223
* Set the `options` for the store.
2324
*/
2425

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

2829
options = options || {};
29-
defaults(options, { enabled: true });
30+
options = Object.assign({ enabled: true }, options);
3031

3132
this.enabled = options.enabled && store.enabled;
3233
this._options = options;

lib/types.ts

+2-1
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

+20-11
Original file line numberDiff line numberDiff line change
@@ -774,17 +774,6 @@ describe('Analytics', function() {
774774
assert.deepEqual(page.options('AdRoll'), { opt: true });
775775
});
776776

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-
788777
it('should be able to override the initialize .integrations option', function() {
789778
analytics.initialize(settings, {
790779
integrations: {
@@ -1611,6 +1600,26 @@ describe('Analytics', function() {
16111600
assert.deepEqual(track.context(), { page: contextPage });
16121601
});
16131602

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

test/cookie.test.js

+9
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

+12
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

+6
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)