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

Commit e179bd8

Browse files
author
Julio Farah
authored
Merge pull request #142 from segmentio/jfarah/remove-clone-dep
refactor: remove third party dependency ndhoule/clone and move it inside ajs-core
2 parents 8b36b99 + 7e90f12 commit e179bd8

File tree

8 files changed

+195
-6
lines changed

8 files changed

+195
-6
lines changed

HISTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 3.12.5 / 2020-05-04
2+
3+
- refactor: remove third party dependency ndhoule/clone
4+
15
# 3.12.4 / 2020-04-23
26

37
- test: add add/apply middleware stress test

lib/analytics.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var IntegrationMiddlewareChain = require('./middleware')
1717
var Page = require('segmentio-facade').Page;
1818
var Track = require('segmentio-facade').Track;
1919
var bindAll = require('bind-all');
20-
var clone = require('@ndhoule/clone');
20+
var clone = require('./utils/clone');
2121
var extend = require('extend');
2222
var cookie = require('./cookie');
2323
var metrics = require('./metrics');

lib/cookie.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
var bindAll = require('bind-all');
8-
var clone = require('@ndhoule/clone');
8+
var clone = require('./utils/clone');
99
var cookie = require('@segment/cookie');
1010
var debug = require('debug')('analytics.js:cookie');
1111
var defaults = require('@ndhoule/defaults');

lib/entity.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Module dependencies.
55
*/
66

7-
var clone = require('@ndhoule/clone');
7+
var clone = require('./utils/clone');
88
var cookie = require('./cookie');
99
var debug = require('debug')('analytics:entity');
1010
var defaults = require('@ndhoule/defaults');

lib/memory.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
var bindAll = require('bind-all');
8-
var clone = require('@ndhoule/clone');
8+
var clone = require('./utils/clone');
99

1010
/**
1111
* HOP.

lib/utils/clone.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'use strict';
2+
3+
var type = require('component-type');
4+
5+
/**
6+
* Deeply clone an object.
7+
*
8+
* @param {*} obj Any object.
9+
*
10+
* COPYRIGHT: https://github.com/ndhoule/clone/blob/master/LICENSE.md
11+
* The MIT License (MIT)
12+
* Copyright (c) 2015 Nathan Houle
13+
* Permission is hereby granted, free of charge, to any person obtaining a copy
14+
* of this software and associated documentation files (the "Software"), to deal
15+
* in the Software without restriction, including without limitation the rights
16+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17+
* copies of the Software, and to permit persons to whom the Software is
18+
* furnished to do so, subject to the following conditions:
19+
*
20+
* The above copyright notice and this permission notice shall be included in
21+
* all copies or substantial portions of the Software.
22+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28+
* THE SOFTWARE.
29+
*/
30+
31+
var clone = function clone(obj) {
32+
var t = type(obj);
33+
34+
var copy;
35+
if (t === 'object') {
36+
copy = {};
37+
for (var key in obj) {
38+
if (obj.hasOwnProperty(key)) {
39+
copy[key] = clone(obj[key]);
40+
}
41+
}
42+
return copy;
43+
}
44+
45+
if (t === 'array') {
46+
copy = new Array(obj.length);
47+
for (var i = 0, l = obj.length; i < l; i++) {
48+
copy[i] = clone(obj[i]);
49+
}
50+
return copy;
51+
}
52+
53+
if (t === 'regexp') {
54+
// from millermedeiros/amd-utils - MIT
55+
var flags = '';
56+
flags += obj.multiline ? 'm' : '';
57+
flags += obj.global ? 'g' : '';
58+
flags += obj.ignoreCase ? 'i' : '';
59+
return new RegExp(obj.source, flags);
60+
}
61+
62+
if (t === 'date') {
63+
return new Date(obj.getTime());
64+
}
65+
66+
// string, number, boolean, etc.
67+
return obj;
68+
};
69+
70+
module.exports = clone;

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@segment/analytics.js-core",
33
"author": "Segment <friends@segment.com>",
4-
"version": "3.12.4",
4+
"version": "3.12.5",
55
"description": "The hassle-free way to integrate analytics into any web application.",
66
"keywords": [
77
"analytics",
@@ -29,7 +29,6 @@
2929
},
3030
"homepage": "https://github.com/segmentio/analytics.js-core#readme",
3131
"dependencies": {
32-
"@ndhoule/clone": "^1.0.0",
3332
"@ndhoule/defaults": "^2.0.1",
3433
"@ndhoule/each": "^2.0.1",
3534
"@ndhoule/extend": "^2.0.0",

test/clone.test.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
'use strict';
2+
3+
var assert = require('proclaim');
4+
var clone = require('../lib/utils/clone');
5+
6+
describe('clone', function() {
7+
it('should be a function', function() {
8+
assert.equal(typeof clone, 'function');
9+
});
10+
11+
it('should have an arity of 1', function() {
12+
assert.equal(clone.length, 1);
13+
});
14+
15+
describe('object', function() {
16+
it('should return an object with the same contents as the input', function() {
17+
var input = { a: 1, b: 2, c: 3 };
18+
var cloned = clone(input);
19+
20+
assert.deepEqual(cloned, input);
21+
});
22+
23+
it('should deeply clone nested objects', function() {
24+
var date = new Date();
25+
var input = {
26+
a: {
27+
b: [1, 2, date, { hello: 'world' }]
28+
}
29+
};
30+
var cloned = clone(input);
31+
32+
assert.deepEqual(cloned, input);
33+
34+
assert.deepEqual(cloned, input);
35+
assert.notStrictEqual(cloned.a, input.a);
36+
assert.notStrictEqual(cloned.a.b, input.a.b);
37+
assert.notStrictEqual(cloned.a.b[2], input.a.b[2]);
38+
assert.strictEqual(cloned.a.b[2].getTime(), input.a.b[2].getTime());
39+
assert.deepEqual(cloned.a.b[3], input.a.b[3]);
40+
assert.notStrictEqual(cloned.a.b[3], input.a.b[3]);
41+
});
42+
43+
it('object with functions', function() {
44+
var func = function() {
45+
return 'original';
46+
};
47+
var input = { func: func };
48+
var cloned = clone(input);
49+
50+
assert.strictEqual(cloned.func, func);
51+
});
52+
});
53+
54+
describe('array', function() {
55+
it('should return an array with the same contents as the input', function() {
56+
var input = [1, 2, 3, '4'];
57+
var cloned = clone(input);
58+
59+
assert.deepEqual(cloned, input);
60+
});
61+
62+
it('should deeply clone nested complex data structures', function() {
63+
var input = [{}];
64+
var cloned = clone(input);
65+
66+
assert.notStrictEqual(cloned[0], input[0]);
67+
assert.deepEqual(cloned[0], input[0]);
68+
});
69+
70+
it('should return a new array object', function() {
71+
var input = [1, 2, 3, '4'];
72+
var cloned = clone(input);
73+
74+
assert.notStrictEqual(cloned, input);
75+
});
76+
});
77+
78+
describe('regexp', function() {
79+
it('regexp', function() {
80+
var input = /hello/i;
81+
var cloned = clone(input);
82+
83+
assert.strictEqual(cloned.toString(), input.toString());
84+
});
85+
86+
it('should return a new regexp object', function() {
87+
var input = /hello/i;
88+
var cloned = clone(input);
89+
90+
assert.notStrictEqual(cloned, input);
91+
});
92+
});
93+
94+
describe('date', function() {
95+
it('should return a date with the same time as the input date', function() {
96+
var input = new Date();
97+
var cloned = clone(input);
98+
99+
assert.strictEqual(cloned.getTime(), input.getTime());
100+
});
101+
102+
it('should return a new date object', function() {
103+
var input = new Date();
104+
var cloned = clone(input);
105+
106+
assert.notStrictEqual(cloned, input);
107+
});
108+
});
109+
110+
describe('other data types', function() {
111+
it('should pass other data types through untouched', function() {
112+
assert.strictEqual(clone('a'), 'a');
113+
assert.strictEqual(clone({ a: 1 }).a, 1);
114+
});
115+
});
116+
});

0 commit comments

Comments
 (0)