Skip to content

Commit 2337f70

Browse files
committed
Refactor url
1 parent 76bb282 commit 2337f70

File tree

19 files changed

+37
-32
lines changed

19 files changed

+37
-32
lines changed

src/rules/url.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
'use strict'
12

2-
let isUrl = require('is-url')
3+
const normalizeUrl = require('normalize-url')
4+
const isString = require('lodash.isstring')
5+
const urlRegex = require('url-regex')
6+
7+
const isUrl = value => urlRegex().test(value)
38

49
/**
510
* Wrap a rule with validation and formatting logic.
@@ -8,26 +13,26 @@ let isUrl = require('is-url')
813
* @return {Function} wrapped
914
*/
1015

11-
function wrap(rule) {
12-
return ($) => {
13-
let value = rule($)
14-
if (typeof value != 'string') return
16+
const wrap = rule => $ => {
17+
let value = rule($)
18+
if (!isString(value)) return
1519

16-
// make sure it's a url
17-
value = value.trim()
18-
if (!isUrl(value)) return
20+
// make sure it's a url
21+
value = value.trim()
22+
if (!isUrl(value)) return
1923

20-
return value
21-
}
24+
return normalizeUrl(value, {
25+
stripWWW: false
26+
})
2227
}
2328

2429
/**
2530
* Rules.
2631
*/
2732

2833
module.exports = [
29-
wrap(($) => $('meta[property="og:url"]').attr('content')),
30-
wrap(($) => $('meta[name="twitter:url"]').attr('content')),
31-
wrap(($) => $('link[rel="canonical"]').attr('href')),
32-
wrap(($, url) => url),
34+
wrap($ => $('meta[property="og:url"]').attr('content')),
35+
wrap($ => $('meta[name="twitter:url"]').attr('content')),
36+
wrap($ => $('link[rel="canonical"]').attr('href')),
37+
wrap(($, url) => url)
3338
]

test/fixtures/cnet/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"image": "https://cnet3.cbsistatic.com/hub/i/r/2016/05/23/ee9a228a-d1d9-40b3-b401-4ba0e07e2fce/thumbnail/670x503/90dc7a2315faa0f311e2e8f43e578847/pebblecore3.jpg",
66
"publisher": "CNET",
77
"title": "Pebble 2, Pebble Time 2, Pebble Core: Pebble's fitness hat trick adds heart rate, GPS, Spotify, and 3G",
8-
"url": "http://www.cnet.com/news/pebble-2-pebble-time-2-pebble-core-announced-coming-this-year-and-2017/"
8+
"url": "http://www.cnet.com/news/pebble-2-pebble-time-2-pebble-core-announced-coming-this-year-and-2017"
99
}

test/fixtures/forbes/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"image": "http://blogs-images.forbes.com/georgeanders/files/2015/09/HackerRank-1940x827.jpg",
66
"publisher": "Forbes",
77
"title": "Facebook Veteran Grady Burnett Joins HackerRank As COO",
8-
"url": "http://www.forbes.com/sites/georgeanders/2015/09/30/facebook-veteran-grady-burnett-joins-hackerrank-as-coo/"
8+
"url": "http://www.forbes.com/sites/georgeanders/2015/09/30/facebook-veteran-grady-burnett-joins-hackerrank-as-coo"
99
}

test/fixtures/fortune/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"image": "https://fortunedotcom.files.wordpress.com/2015/09/gettyimages-1852881881.jpg?crop=1&h=570&w=820",
66
"publisher": "Fortune",
77
"title": "Why your next job search may involve solving online puzzles",
8-
"url": "http://fortune.com/2015/10/05/hackerrank-recruiting-tool/"
8+
"url": "http://fortune.com/2015/10/05/hackerrank-recruiting-tool"
99
}

test/fixtures/geek-time/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"image": "http://i0.wp.com/www.geektime.com/wp-content/uploads/2016/04/image001-e1461245271861.jpg?fit=750,501",
66
"publisher": "Geektime",
77
"title": "Fraud solutions Forter secures payment of $32M in Series C",
8-
"url": "http://www.geektime.com/2016/04/21/fraud-solutions-forter-secures-payment-of-32m-in-series-c/"
8+
"url": "http://www.geektime.com/2016/04/21/fraud-solutions-forter-secures-payment-of-32m-in-series-c"
99
}

test/fixtures/jewish-business-news/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"image": "http://jewishbusinessnews.com/wp-content/uploads/2016/01/Shlomi-Ben-Haim-JFrog-Co-Founder-and-CEO-Source-JFrog-e1453326404147.jpg",
66
"publisher": "Jewish Business News",
77
"title": "Israeli startup JFrog raises $50 million in C round",
8-
"url": "http://jewishbusinessnews.com/2016/01/20/israeli-startup-jfrog-raises-50-million-in-c-round/"
8+
"url": "http://jewishbusinessnews.com/2016/01/20/israeli-startup-jfrog-raises-50-million-in-c-round"
99
}

test/fixtures/mac-rumors/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"image": "http://cdn.macrumors.com/article-new/2016/05/amazonecho2-250x362.jpg?retina",
66
"publisher": "MacRumors",
77
"title": "Apple Working on Amazon Echo Competitor, Opening Siri Up to Developers",
8-
"url": "http://www.macrumors.com/2016/05/24/apple-siri-sdk-amazon-echo-competitor/"
8+
"url": "http://www.macrumors.com/2016/05/24/apple-siri-sdk-amazon-echo-competitor"
99
}

test/fixtures/mashable/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"image": "http://i.amz.mshcdn.com/jw9czJj9h-4ClVBegbu982-Zmb8=/1200x627/2015%2F05%2F13%2Fc6%2Frevenue_ana.65b72.jpg",
66
"publisher": "Mashable",
77
"title": "The sales cycle on steroids: 3 ways analytics power up revenue",
8-
"url": "http://mashable.com/2015/05/13/analytics-power-up-revenue/"
8+
"url": "http://mashable.com/2015/05/13/analytics-power-up-revenue"
99
}

test/fixtures/segment-academy/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"image": "https://www.segment.com/academy",
66
"publisher": "Segment",
77
"title": "When to Track on the Client vs. Server",
8-
"url": "https://www.segment.com/academy/collecting-data/when-to-track-on-the-client-vs-server/"
8+
"url": "https://www.segment.com/academy/collecting-data/when-to-track-on-the-client-vs-server"
99
}

test/fixtures/silicon-angle/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
"image": "http://siliconangle.com/files/2016/02/collab-800x432.png",
66
"publisher": "SiliconANGLE",
77
"title": "CircleCI receives $18 million in Series B funding round with Scale Venture Partners",
8-
"url": "http://siliconangle.com/blog/2016/05/16/circleci-receives-18-million-in-series-b-funding-round-with-scale-venture-partners/"
8+
"url": "http://siliconangle.com/blog/2016/05/16/circleci-receives-18-million-in-series-b-funding-round-with-scale-venture-partners"
99
}

0 commit comments

Comments
 (0)