Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarneo committed Jan 20, 2017
0 parents commit d505390
Show file tree
Hide file tree
Showing 10 changed files with 1,998 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
npm-debug.log
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Bjarne Øverli

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
Extract domain from given URL
--

Performant domain extraction. No regex or array magic.

[What is a URL](https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL)

However. This package will also remove the sub domain.

Support
--
Browser and Node.

Usage
--

* urls = string|array
* returns string|array

```js
extractDomain(urls);
```

ES6
```js
import { extractDomain } from 'extract-domain';
```

```js
const extractDomain = require('extract-domain').extractDomain;
```

```js
const urls = [
'https://www.npmjs.com/package/extract-domain',
'http://www.example.com:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument',
'http://user:password@example.com:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument',
'https://npmjs.com/package/extract-domain',
'ftp://example.org/resource.txt'
];


extractDomain(urls[0]); // npmjs.com

extractDomain(urls); // [ 'npmjs.com', 'example.com', 'example.com', 'npmjs.com', 'example.org' ]

```

Tests
--
```bash
$ npm test
```

Coding style
--
```bash
$ npm run pretty
```

Contribution
------
Contributions are appreciated.

License
------
MIT-licensed. See LICENSE.
2 changes: 2 additions & 0 deletions dist/extract-domain.min.js

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

99 changes: 99 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
function throwTypeError() {
throw new TypeError('The given URL is not a string. Please verify your string|array.');
}

// Easy to read they said?
// Function has to many lines they said?
function getDomainFromUrl(url) {
if (typeof url !== 'string') {
throwTypeError();
}

let domainInc = 0;
let offsetDomain = 0;
let offsetStartSlice = 0;
let offsetPath = 0;
let len = url.length;
let i = 0;

// Find offset of the domain
while (len-- && ++i) {
if (domainInc && url[i] === '/') {
break;
}

if (url[i] !== '.') {
continue;
}

++domainInc;

offsetDomain = i;
}

i = offsetDomain;

// Find offset before domain name.
while (i--) {
// Look for sub domain or protocol
if (url[i] !== '.' && url[i] !== '/' && url[i] !== '@') {
continue;
}

offsetStartSlice = i + 1;

break;
}

i = offsetDomain;

// Get the offset path
while (i++) {
if (i >= url.length) {
break;
}

// If we hit the port, set the offsetPath and break the loop
if (url[i] === ':') {
offsetPath = i;

break;
}

// Continue until we find the start of a path
if (url[i] !== '/') {
continue;
}

offsetPath = i;

break;
}

// offsetStartSlice should always be larger than protocol
if (offsetStartSlice < 6) {
return '';
}

// It has been a wild ride
// .. slice
// Tried several approaches slicing a string. Can't get it any faster than this.
return url.slice(offsetStartSlice, offsetPath);
}

module.exports = function extractDomain(urls) {
if (typeof urls === 'string') {
return getDomainFromUrl(urls);
} else if (Array.isArray(urls)) {
const extractedUrls = [];
let len;

for (let i = 0, len = urls.length; i < len; i++) {
extractedUrls.push(getDomainFromUrl(urls[i]));
}

return extractedUrls;
} else {
throwTypeError();
}
};
72 changes: 72 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';
const assert = require('assert');
const extractDomain = require('./index');

const urls = [
'https://www.npmjs.com/package/extract-domain',
'http://www.example.com:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument',
'https://npmjs.com/package/extract-domain',
'http://example.com:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument',
'http://www.so.many.sub.domains.example.com:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument',
'http://user:password@example.com:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument',
'ftp://example.org/resource.txt'
];

const expected = [
'npmjs.com',
'example.com',
'npmjs.com',
'example.com',
'example.com',
'example.com',
'example.org'
];

describe('extract domain', () => {
it('should extract given domain from string without sub domain', () => {
assert.equal(extractDomain(urls[3]), expected[3]);
assert.equal(extractDomain(urls[1]), expected[1]);
});

it('should extract given domain from string', () => {
assert.equal(extractDomain(urls[1]), expected[1]);

assert.equal(extractDomain(urls[0]), expected[0]);
});

it('should extract given domain from an array of strings', () => {
const domains = extractDomain(urls);

domains.map(domain => assert(expected.indexOf(domain) > -1));
});

it('should return empty string if it is not a domain', () => {
assert.equal(extractDomain('/i.am/just.astring//7test'), '');
});

it('should throw syntax error exception if the argument is not string nor array', () => {
try {
extractDomain('{}');
} catch (e) {
assert.equal(e.name, 'TypeError');

assert.equal(
e.message,
'The given URL is not a string. Please verify your string|array.'
);
}
});

it('should throw syntax error exception if the array value is not a string', () => {
try {
extractDomain([ [ 'wow' ] ]);
} catch (e) {
assert.equal(e.name, 'TypeError');

assert.equal(
e.message,
'The given URL is not a string. Please verify your string|array.'
);
}
});
});
40 changes: 40 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "extract-domain",
"version": "1.0.0",
"description": "Extract domain from given string",
"main": "dist/extract-domain.min.js",
"author": "Bjarne Oeverli",
"license": "MIT",
"scripts": {
"build": "npm run pretty && NODE_ENV=production webpack",
"watch": "webpack --watch",
"test": "mocha -R spec index.test.js",
"pretty": "prettier --tab-width=4 --print-width=100 --single-quote --trailing-coma --write *.js *.js"
},
"repository": {
"type": "git",
"url": "https://github.com/bjarneo/extract-domain.git"
},
"homepage": "https://github.com/bjarneo/extract-domain",
"bugs": {
"url": "https://github.com/bjarneo/extract-domain/issues"
},
"keywords": [
"extract",
"get",
"fetch",
"string",
"url",
"domain",
"browser",
"node"
],
"devDependencies": {
"google-closure-compiler-js": "^20161201.0.0",
"mocha": "^3.2.0",
"webpack": "^1.14.0"
},
"dependencies": {
"pretty": "^1.0.0"
}
}
21 changes: 21 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const ClosureCompiler = require('google-closure-compiler-js').webpack;

module.exports = {
devtool: null,
entry: { browser: './index.js' },
output: {
path: __dirname,
filename: 'dist/extract-domain.min.js',
library: 'extractDomain',
libraryTarget: 'commonjs'
},
plugins: [
new ClosureCompiler({
options: {
languageIn: 'ECMASCRIPT6',
languageOut: 'ECMASCRIPT3',
compilationLevel: 'ADVANCED'
}
})
]
};

0 comments on commit d505390

Please sign in to comment.