Skip to content

Commit 4b4cd6d

Browse files
merge url when new value is https
1 parent 71b9525 commit 4b4cd6d

File tree

3 files changed

+91
-10
lines changed

3 files changed

+91
-10
lines changed

lib/index.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,19 @@ function loadBlogs() {
2929
.then(blogs => blogs.filter(blog => blog && typeof blog.url === 'string'))
3030
.then(blogs => {
3131
blogs.forEach(blog => {
32-
BLOG_MAP[blog.url] = blog;
33-
BLOG_INDEX[blog.url] = 1;
32+
BLOG_MAP[urlNoProtocal(blog.url)] = blog;
33+
BLOG_INDEX[urlNoProtocal(blog.url)] = 1;
3434
BLOG_INDEX[urlAlias(blog.url)] = 1;
3535
});
3636
});
3737
}
3838

39+
function urlNoProtocal(url) {
40+
const regex = /https?:\/\//g;
41+
url = url.replace(regex, '');
42+
return url;
43+
}
44+
3945
function urlAlias(url) {
4046
const regex = /https?:\/\/|www\.|\?.+|#.+|\/$/g;
4147
url = url.replace(regex, '');
@@ -45,7 +51,7 @@ function urlAlias(url) {
4551
}
4652

4753
function findUrlInDocuments(url) {
48-
if (BLOG_MAP[url]) {
54+
if (BLOG_MAP[urlNoProtocal(url)]) {
4955
return true;
5056
}
5157

@@ -235,13 +241,20 @@ function mergeObject(obj1, obj2) {
235241
}
236242

237243
const value = obj2[key];
238-
if (value) {
239-
if (isArray(value) || isArray(obj1[key])) {
240-
const arr = unique(filterEmpties([].concat(obj1[key]).concat(value)));
241-
obj1[key] = arr;
242-
} else {
244+
if (!value) {
245+
continue;
246+
}
247+
248+
if (isArray(value) || isArray(obj1[key])) {
249+
const arr = unique(filterEmpties([].concat(obj1[key]).concat(value)));
250+
obj1[key] = arr;
251+
} else if (key === 'url') {
252+
// Copy the value of url only when https.
253+
if (value.indexOf('https') === 0) {
243254
obj1[key] = value;
244255
}
256+
} else {
257+
obj1[key] = value;
245258
}
246259
}
247260

@@ -250,7 +263,7 @@ function mergeObject(obj1, obj2) {
250263

251264
function _merge(doc) {
252265
const {url} = doc;
253-
const oldDoc = BLOG_MAP[url];
266+
const oldDoc = BLOG_MAP[urlNoProtocal(url)];
254267
if (!oldDoc) {
255268
return Promise.resolve().then(() => _save(doc));
256269
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dev-blog-directory-save",
3-
"version": "0.4.0",
3+
"version": "0.4.1",
44
"description": "API for save new blogs to dev-blog-directory.",
55
"main": "lib/index.js",
66
"scripts": {

test/merge.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,62 @@ describe('merge', () => {
4343
expect(newDoc.rss).to.be.eql('https://myblog.com/feed/');
4444
});
4545

46+
it('merge url - no overwrite', () => {
47+
const obj1 = {
48+
_id: '123456',
49+
url: 'https://myblog.com',
50+
name: 'myblog',
51+
author: null,
52+
rss: 'http://myblog.com/wrong-feed/',
53+
tags: ['foo', 'bar'],
54+
categories: ['foo', 'bar']
55+
};
56+
57+
const obj2 = {
58+
url: 'http://myblog.com',
59+
name: null,
60+
author: 'blogger',
61+
rss: 'https://myblog.com/feed/',
62+
tags: ['foo', 'bar'],
63+
categories: ['foo', 'bar']
64+
};
65+
66+
const newDoc = mergeObject(obj1, obj2);
67+
expect(newDoc._id).to.be.an('string').to.be.have.lengthOf(6);
68+
expect(newDoc.url).to.be.eql('https://myblog.com');
69+
expect(newDoc.name).to.be.eql('myblog');
70+
expect(newDoc.author).to.be.eql('blogger');
71+
expect(newDoc.rss).to.be.eql('https://myblog.com/feed/');
72+
});
73+
74+
it('merge url - overwite', () => {
75+
const obj1 = {
76+
_id: '123456',
77+
url: 'http://myblog.com',
78+
name: 'myblog',
79+
author: null,
80+
rss: 'http://myblog.com/wrong-feed/',
81+
tags: ['foo', 'bar'],
82+
categories: ['foo', 'bar']
83+
};
84+
85+
const obj2 = {
86+
url: 'https://myblog.com',
87+
name: null,
88+
author: 'blogger',
89+
rss: 'https://myblog.com/feed/',
90+
tags: ['foo', 'bar'],
91+
categories: ['foo', 'bar']
92+
};
93+
94+
const newDoc = mergeObject(obj1, obj2);
95+
expect(newDoc._id).to.be.an('string').to.be.have.lengthOf(6);
96+
expect(newDoc.url).to.be.eql('https://myblog.com');
97+
expect(newDoc.name).to.be.eql('myblog');
98+
expect(newDoc.author).to.be.eql('blogger');
99+
expect(newDoc.rss).to.be.eql('https://myblog.com/feed/');
100+
});
101+
46102
it('merge array 1', () => {
47103
const obj1 = {
48104
_id: '123456',
@@ -150,6 +206,18 @@ describe('merge', () => {
150206
return expect(_merge(doc)).to.be.fulfilled;
151207
});
152208

209+
it('merge url', () => {
210+
const doc = {
211+
url: 'http://myblog.com/',
212+
name: 'myblog',
213+
author: 'blogger',
214+
tags: ['foo', 'bar'],
215+
categories: ['foo', 'bar']
216+
};
217+
218+
return expect(_merge(doc)).to.be.fulfilled;
219+
});
220+
153221
it('save new blog', () => {
154222
const doc = {
155223
url: 'https://myblog.com/new-blog/',

0 commit comments

Comments
 (0)