Skip to content

Commit

Permalink
Merge pull request #16 from Bartozzz/development
Browse files Browse the repository at this point in the history
Bump version 1.0.3
  • Loading branch information
Bartozzz authored Apr 3, 2018
2 parents 9385467 + 2cbd82c commit 3558c13
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "get-link",
"version": "1.0.2",
"version": "1.0.3",
"keywords": [
"url",
"link",
Expand Down
37 changes: 23 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@ import {
} from "url";

// eslint-disable-next-line
const REGEX_URL: RegExp = /^(?:https?:\/\/)?(?:www\.)?([-a-zA-Z0-9_.=]{2,255}\.+(?:[a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|local|internal)\b)(\/[-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)?/gi;
const REGEX_URL: RegExp = /^(?:https?:\/\/)?(?:www\.)?([-a-zA-Z0-9_.=]{2,255}\.+(?:[a-z]{2,6})\b)(\/[-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)?/gi;
const REGEX_DYNAMIC: RegExp = /^(?:[a-z]+\:[^\/]\S{1,})|(?:#\S{1,})/gi;
const REGEX_ABSOLUTE: RegExp = /^https?:\/\//i;

/**
* Parse a link and return its "normalized" (without the `https://www.` prefix)
* domain and path.
*
*
* @param {string} link Link to parse
* @return {object} Normalized domain and path
* @throws {Error} When domain could not be resolved
*/
function parseLink(link: string): Object {
const match: Object = new RegExp(REGEX_URL).exec(link);

// 0 = link; 1 = domain; 2 = path
if (!("1" in match)) {
throw new Error(`Invalid domain ${link}`);
}

return {
domain: match[1],
path: match[2],
Expand Down Expand Up @@ -79,21 +84,25 @@ export default function(base: string, link: string): null|string|boolean {

// Link is absolute:
if (link.match(REGEX_ABSOLUTE)) {
const parsedBase: Object = parseLink(base);
const parsedLink: Object = parseLink(link);
try {
const parsedBase: Object = parseLink(base);
const parsedLink: Object = parseLink(link);

// Both `base` and `link` are on different domains:
if (parsedBase.domain !== parsedLink.domain) {
return false;
}
// Both `base` and `link` are on different domains:
if (parsedBase.domain !== parsedLink.domain) {
return false;
}

// Absolute path from same domain:
if (parsedLink.path) {
return resolveURL(base, parsedLink.path);
}
// Absolute path from same domain:
if (parsedLink.path) {
return resolveURL(base, parsedLink.path);
}

// Same domains without path:
return parseURL(base).href;
// Same domains without path:
return parseURL(base).href;
} catch (err) {
return false;
}
}

// Relative stuff:
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ describe("Test", function() {
assert.equal(false, getLink(TEST_URL_1, "http://www.example.gov/"));
assert.equal(false, getLink(TEST_URL_1, "https://example.gov/"));
assert.equal(false, getLink(TEST_URL_1, "http://example.gov/"));
assert.equal(false, getLink("https://google.com/", "https://abc.xyz/investor/"));
});
});

0 comments on commit 3558c13

Please sign in to comment.