Skip to content

Commit

Permalink
Fix web.site?q=data type urls, add badges
Browse files Browse the repository at this point in the history
  • Loading branch information
kaustubhhiware committed May 29, 2017
1 parent a7eb5fe commit e50effa
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 10 deletions.
11 changes: 11 additions & 0 deletions .travis.yml
@@ -0,0 +1,11 @@
language: node_js
node_js:
- stable
install:
- npm install

script:
- npm run cover

# Send coverage data to Coveralls
after_script: "cat coverage/lcov.info | node_modules/coveralls/bin/coveralls.js"
49 changes: 43 additions & 6 deletions README.md
@@ -1,19 +1,56 @@
# urlparamify

Simply parse urls, no matter how stupid they look. Primary goal: Keep urls
modifiable, without worrying about anything else. Allow for modifying urls,
and generating a neat url with accomodated changes.
> Simply parse urls, no matter how stupid they look.
[![Build Status](https://travis-ci.org/kaustubhhiware/urlparamify.svg?branch=master)](https://travis-ci.org/kaustubhhiware/urlparamify) [![Coverage Status](https://coveralls.io/repos/github/kaustubhhiware/urlparamify/badge.svg?branch=master)](https://coveralls.io/github/kaustubhhiware/urlparamify?branch=master)

## The why

Primary goal: Keep urls modifiable, without worrying about anything else.
Allow for modifying urls, and generating a neat url with accomodated changes.

Often you might be parsing urls, modifying some parameter using strings,
and the whole system looks messy. [`urlparamify`](https://www.npmjs.com/package/urlparamify)
seeks to solve that problem. Just give it a original string, which is converted
into JSON modifiable form. Mess up any way you want to with the parameters,
playing around with the params, and just call a `Url.toString()` to get your modified url.

## Installation

npm install urlparamify
$ npm install urlparamify

## Structure

Each url is broken down into the following components:

href: The original input string
protocol: http / https
host:

## Usage

var h = Url('http://google.com/path1?q=data&d=sad#hash')
> var h = Url('http://google.com/path1?q=data&d=sad#hash')

> h
{ href: 'http://google.com/path1/?q=data&d=sad#hash',
protocol: 'http',
host: 'google.com',
baseurl: 'http://google.com',
path: 'path1',
query: 'q=data&d=sad',
queryParams: { q: 'data', d: 'sad' },
hash: 'hash',
getBaseurl: [Function],
toString: [Function] }

h.toString()
> h.toString()
'http://google.com/path1?q=data&d=sad#hash'

## Tests

npm test
npm run cover

## License

MIT © [Kaustubh Hiware](https://kaustubhhiware.github.io)
25 changes: 22 additions & 3 deletions index.js
Expand Up @@ -19,6 +19,14 @@ module.exports = function(urlString) {
}

let url = {};
// handle google.com?q=data => google.com/?q=data
// browser does this by default
let onlyquestion = emptySubstrFind(urlString, 0, '?');
if(onlyquestion !== -1) {
if(onlyquestion !== 0 && urlString[onlyquestion-1] !== '/') {
urlString = urlString.substr(0, onlyquestion) + '/' + urlString.slice(onlyquestion);
}
}
url.href = urlString;
let index = 0;

Expand All @@ -35,20 +43,30 @@ module.exports = function(urlString) {
// need google.com, youtu.be, etc
// checks for internet address, localhost
let hostindex = emptySubstrFind(urlString, index, '/');
if(hostindex !== -1 && (firstdot !== -1 || firstcolon !== -1)) {
// if(hostindex !== -1 && (firstdot !== -1 || firstcolon !== -1)) {
if(hostindex !== -1) {
url.host = urlString.substr(index, hostindex);
index += hostindex + 1;
} else {
// till the very end
url.host = urlString.slice(index);
index = urlString.length;
if(onlyquestion !== -1) {
url.host = urlString.substr(0, onlyquestion);
index = onlyquestion + 1;
} else {
url.host = urlString.slice(index);
index = urlString.length;
}
}
// remove unnecessary trailing /
url.baseurl = (urlString[index-1] === '/')? urlString.substr(0, index-1): urlString.substr(0, index);

let question = emptySubstrFind(urlString, index, '?');
if(question !== -1) {
url.path = urlString.substr(index, question);
// remove last /
// intently written twice
url.path = (url.path.substr(-1) === '/') ? url.path.slice(0, -1) : url.path;
url.path = (url.path.substr(-1) === '/') ? url.path.slice(0, -1) : url.path;
index += question + 1;
} else {
url.path = urlString.slice(index);
Expand All @@ -65,6 +83,7 @@ module.exports = function(urlString) {
let split = attribute.split('=');
url.queryParams[split[0]] = split[1];
try {
// console.log(split[0], "params", JSON.parse(split[1].replace('/', '')));
url.queryParams[split[0]] = JSON.parse(decodeURIComponent(split[1].replace('/','')));
} catch(e) {
// pass
Expand Down
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -5,10 +5,13 @@
"main": "index.js",
"devDependencies": {
"assert": "1.1.1",
"coveralls": "^2.13.1",
"istanbul": "^0.4.5",
"mocha": "^1.18.2"
},
"scripts": {
"test": "mocha --reporter spec"
"test": "mocha --reporter spec",
"cover": "node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha -- -R spec test/*"
},
"repository": {
"type": "git",
Expand Down
44 changes: 44 additions & 0 deletions test/test.js
Expand Up @@ -134,6 +134,50 @@ var Expected = {
toString: 'http://google.com/path?q=data#top'
},

'www.something.net/?q=data' : {
href: 'www.something.net/?q=data',
protocol: '',
host: 'www.something.net',
baseurl: 'www.something.net',
path: '',
query: 'q=data',
queryParams: { q: 'data' },
hash: null,
getBaseurl: 'www.something.net',
toString: 'www.something.net?q=data'
},

'google.com/path/?q=[1,2]': {
href: 'google.com/path/?q=[1,2]',
protocol: '',
host: 'google.com',
baseurl: 'google.com',
path: 'path',
query: 'q=[1,2]',
queryParams: { q: [ 1, 2 ] },
hash: null,
getBaseurl: 'google.com',
toString: 'google.com/path?q=1,2'
},

'/path?q=data' : {
href: '/path/?q=data',
protocol: '',
host: '',
baseurl: '',
path: 'path',
query: 'q=data',
queryParams: { q: 'data' },
hash: null,
getBaseurl: '',
toString: '/path?q=data'

},


// 'google.com/path?q={c:1}' : {

// }
};

Object.keys(Expected).forEach(function(u) {
Expand Down

0 comments on commit e50effa

Please sign in to comment.