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

Commit ac8f20a

Browse files
committed
Revert "Replace @ndhoule/defaults with ES6 spread syntax merging (#185)"
This reverts commit 7b59f34.
1 parent d1074f5 commit ac8f20a

12 files changed

+32
-148
lines changed

lib/analytics.ts

+15-48
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ import {
55
InitOptions,
66
SegmentAnalytics,
77
SegmentOpts,
8-
SegmentIntegration,
9-
PageDefaults
8+
SegmentIntegration
109
} from './types';
1110
var url = require('component-url');
1211

13-
import cloneDeep from 'lodash.clonedeep'
14-
import pick from 'lodash.pick'
12+
import cloneDeep from 'lodash.clonedeep';
13+
import pick from 'lodash.pick';
1514

1615
var _analytics = global.analytics;
1716

@@ -45,6 +44,7 @@ var normalize = require('./normalize');
4544
var on = require('component-event').bind;
4645
var pageDefaults = require('./pageDefaults');
4746
var prevent = require('@segment/prevent-default');
47+
var defaults = require('@ndhoule/defaults');
4848
var store = require('./store');
4949
var user = require('./user');
5050
var type = require('component-type');
@@ -322,13 +322,8 @@ Analytics.prototype.identify = function(
322322
});
323323

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

334329
this._invoke('identify', new Identify(msg));
@@ -383,13 +378,8 @@ Analytics.prototype.group = function(
383378
});
384379

385380
// Add the initialize integrations so the server-side ones can be disabled too
386-
// NOTE: We need to merge integrations, not override them with assign
387-
// since it is possible to change the initialized integrations at runtime.
388381
if (this.options.integrations) {
389-
msg.integrations = {
390-
...this.options.integrations,
391-
...msg.integrations
392-
};
382+
defaults(msg.integrations, this.options.integrations);
393383
}
394384

395385
this._invoke('group', new Group(msg));
@@ -453,12 +443,10 @@ Analytics.prototype.track = function(
453443
}
454444

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

463451
this._invoke('track', new Track(msg));
464452

@@ -611,15 +599,8 @@ Analytics.prototype.page = function(
611599

612600
// Ensure properties has baseline spec properties.
613601
// TODO: Eventually move these entirely to `options.context.page`
614-
// FIXME: This is purposely not overriding `defs`. There was a bug in the logic implemented by `@ndhoule/defaults`.
615-
// This bug made it so we only would overwrite values in `defs` that were set to `undefined`.
616-
// In some cases, though, pageDefaults will return defaults with values set to "" (such as `window.location.search` defaulting to "").
617-
// The decision to not fix this bus was made to preserve backwards compatibility.
618602
const defs = pageDefaults();
619-
properties = {
620-
...properties,
621-
...defs
622-
};
603+
defaults(properties, defs);
623604

624605
// Mirror user overrides to `options.context.page` (but exclude custom properties)
625606
// (Any page defaults get applied in `this.normalize` for consistency.)
@@ -639,13 +620,8 @@ Analytics.prototype.page = function(
639620
});
640621

641622
// Add the initialize integrations so the server-side ones can be disabled too
642-
// NOTE: We need to merge integrations, not override them with assign
643-
// since it is possible to change the initialized integrations at runtime.
644623
if (this.options.integrations) {
645-
msg.integrations = {
646-
...this.options.integrations,
647-
...msg.integrations
648-
};
624+
defaults(msg.integrations, this.options.integrations);
649625
}
650626

651627
this._invoke('page', new Page(msg));
@@ -697,13 +673,8 @@ Analytics.prototype.alias = function(
697673
});
698674

699675
// Add the initialize integrations so the server-side ones can be disabled too
700-
// NOTE: We need to merge integrations, not override them with assign
701-
// since it is possible to change the initialized integrations at runtime.
702676
if (this.options.integrations) {
703-
msg.integrations = {
704-
...this.options.integrations,
705-
...msg.integrations
706-
};
677+
defaults(msg.integrations, this.options.integrations);
707678
}
708679

709680
this._invoke('alias', new Alias(msg));
@@ -1002,19 +973,15 @@ Analytics.prototype._parseQuery = function(query: string): SegmentAnalytics {
1002973
*/
1003974

1004975
Analytics.prototype.normalize = function(msg: {
1005-
options: { [key: string]: unknown };
1006-
context: { page: Partial<PageDefaults> };
976+
context: { page };
1007977
anonymousId: string;
1008978
}): object {
1009979
msg = normalize(msg, Object.keys(this._integrations));
1010980
if (msg.anonymousId) user.anonymousId(msg.anonymousId);
1011981
msg.anonymousId = user.anonymousId();
1012982

1013983
// Ensure all outgoing requests include page data in their contexts.
1014-
msg.context.page = {
1015-
...pageDefaults(),
1016-
...msg.context.page
1017-
};
984+
msg.context.page = defaults(msg.context.page || {}, pageDefaults());
1018985

1019986
return msg;
1020987
};

lib/cookie.ts

+7-12
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ import cloneDeep from 'lodash.clonedeep'
1010
var bindAll = require('bind-all');
1111
var cookie = require('@segment/cookie');
1212
var debug = require('debug')('analytics.js:cookie');
13+
var defaults = require('@ndhoule/defaults');
1314
var topDomain = require('@segment/top-domain');
1415

15-
const MAX_AGE_ONE_YEAR = 31536000000
16-
1716
/**
1817
* Initialize a new `Cookie` with `options`.
1918
*
@@ -33,20 +32,16 @@ Cookie.prototype.options = function(options?: CookieOptions) {
3332

3433
options = options || {};
3534

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

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

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

lib/entity.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import assignIn from 'lodash.assignin'
1010

1111
var cookie = require('./cookie');
1212
var debug = require('debug')('analytics:entity');
13+
var defaults = require('@ndhoule/defaults');
1314
var memory = require('./memory');
1415
var store = require('./store');
1516
var isodateTraverse = require('@segment/isodate-traverse');
@@ -73,10 +74,7 @@ Entity.prototype.storage = function() {
7374

7475
Entity.prototype.options = function(options?: InitOptions) {
7576
if (arguments.length === 0) return this._options;
76-
this._options = {
77-
...this.defaults,
78-
...options
79-
}
77+
this._options = defaults(options || {}, this.defaults || {});
8078
};
8179

8280
/**

lib/normalize.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import includes from 'lodash.includes'
66
*/
77

88
var debug = require('debug')('analytics.js:normalize');
9+
var defaults = require('@ndhoule/defaults');
910
var type = require('component-type');
1011
var uuid = require('uuid/v4');
1112
var md5 = require('spark-md5').hash;
1213

13-
1414
/**
1515
* HOP.
1616
*/
@@ -88,11 +88,7 @@ function normalize(msg: Message, list: Array<any>): NormalizedMessage {
8888
delete msg.options;
8989
ret.integrations = integrations;
9090
ret.context = context;
91-
ret = {
92-
...msg,
93-
...ret
94-
}
95-
91+
ret = defaults(ret, msg);
9692
debug('->', ret);
9793
return ret;
9894

lib/store.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
'use strict';
22

3-
import { StoreOptions } from './types';
4-
53
/*
64
* Module dependencies.
75
*/
86

97
var bindAll = require('bind-all');
8+
var defaults = require('@ndhoule/defaults');
109
var store = require('@segment/store');
1110

1211
/**
@@ -23,14 +22,11 @@ function Store(options?: { enabled: boolean }) {
2322
* Set the `options` for the store.
2423
*/
2524

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

2928
options = options || {};
30-
options = {
31-
enabled: true,
32-
...options
33-
};
29+
defaults(options, { enabled: true });
3430

3531
this.enabled = options.enabled && store.enabled;
3632
this._options = options;

lib/types.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export interface CookieOptions {
3737
domain?: string;
3838
path?: string;
3939
secure?: boolean;
40-
sameSite?: string
4140
}
4241

4342
export interface MetricsOptions {
@@ -47,7 +46,7 @@ export interface MetricsOptions {
4746
maxQueueSize?: number;
4847
}
4948

50-
export interface StoreOptions {
49+
interface StoreOptions {
5150
enabled?: boolean;
5251
}
5352

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@segment/analytics.js-core",
33
"author": "Segment <[email protected]>",
4-
"version": "4.1.5",
4+
"version": "4.1.4",
55
"description": "The hassle-free way to integrate analytics into any web application.",
66
"types": "lib/index.d.ts",
77
"keywords": [
@@ -30,6 +30,7 @@
3030
},
3131
"homepage": "https://github.com/segmentio/analytics.js-core#readme",
3232
"dependencies": {
33+
"@ndhoule/defaults": "^2.0.1",
3334
"@segment/canonical": "^1.0.0",
3435
"@segment/cookie": "^1.1.5",
3536
"@segment/is-meta": "^1.0.0",

test/analytics.test.ts

-40
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ var pageDefaults = require('../build/pageDefaults');
1111
var sinon = require('sinon');
1212
var tick = require('next-tick');
1313
var trigger = require('compat-trigger-event');
14-
1514
var Identify = Facade.Identify;
1615
var cookie = Analytics.cookie;
1716
var group = analytics.group();
@@ -829,22 +828,6 @@ describe('Analytics', function() {
829828
});
830829
});
831830

832-
it('should should not overwrite context.page values due to an existing bug', function() {
833-
analytics.page({ prop: true, context: { page: { search: "lol" } } }, { context: { page: { search: "" } } });
834-
var page = analytics._invoke.args[0][1];
835-
// FIXME: This assert should fail once the bug is fixed
836-
assert.notDeepEqual(page.context(), {
837-
page: {
838-
...defaults,
839-
search: "lol",
840-
},
841-
});
842-
// FIXME: This assert should fail once the bug is fixed
843-
assert.deepEqual(page.context(), {
844-
page: defaults,
845-
});
846-
});
847-
848831
it('should emit page', function(done) {
849832
analytics.once('page', function(category, name, props, opts) {
850833
assert(category === 'category');
@@ -1625,29 +1608,6 @@ describe('Analytics', function() {
16251608
assert.deepEqual(track.context(), { page: contextPage });
16261609
});
16271610

1628-
it('should support overwriting context.page fields', function() {
1629-
analytics.track(
1630-
'event',
1631-
{},
1632-
{
1633-
context: {
1634-
page: {
1635-
title: 'lol'
1636-
}
1637-
}
1638-
}
1639-
);
1640-
1641-
var track = analytics._invoke.args[0][1];
1642-
assert.deepEqual(
1643-
track.context().page,
1644-
{
1645-
...contextPage,
1646-
title: 'lol'
1647-
}
1648-
);
1649-
});
1650-
16511611
it('should accept context.traits', function() {
16521612
analytics.track('event', { prop: 1 }, { traits: { trait: true } });
16531613
var track = analytics._invoke.args[0][1];

test/cookie.test.ts

-9
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,6 @@ 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-
7566
it('should set the domain correctly', function() {
7667
cookie.options({ domain: '' });
7768
assert(cookie.options().domain === '');

test/normalize.test.ts

-12
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,6 @@ 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-
});
7967
});
8068

8169
describe('integrations', function() {

test/store.test.ts

-6
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,5 @@ 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-
});
5246
});
5347
});

0 commit comments

Comments
 (0)