Skip to content

Commit

Permalink
v1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenvachon committed Jul 6, 2017
1 parent 9c6eb47 commit 06f06fb
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ language: node_js
node_js:
- 6
- 8
script: npm test
script: npm run ci
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# incomplete-url [![NPM Version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url]
# incomplete-url [![NPM Version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependency Monitor][greenkeeper-image]][greenkeeper-url]

> Custom-remove features of a WHATWG [`URL`](https://developer.mozilla.org/en/docs/Web/API/URL) implementation.
Expand Down Expand Up @@ -43,3 +43,7 @@ When set to `true`, the output `URLSearchParams` class (and `URL#searchParams`)
[npm-url]: https://npmjs.org/package/incomplete-url
[travis-image]: https://img.shields.io/travis/stevenvachon/incomplete-url.svg
[travis-url]: https://travis-ci.org/stevenvachon/incomplete-url
[coveralls-image]: https://img.shields.io/coveralls/stevenvachon/incomplete-url.svg
[coveralls-url]: https://coveralls.io/github/stevenvachon/incomplete-url
[greenkeeper-image]: https://badges.greenkeeper.io/stevenvachon/incomplete-url.svg
[greenkeeper-url]: https://greenkeeper.io/
78 changes: 46 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,71 @@ const customizeURL = (options={}) =>
{
const IncompleteURLSearchParams = function(params)
{
if (params instanceof IncompleteURLSearchParams)
{
params = params._searchParams;
}

this._searchParams = new URLSearchParams(params);

// Support iteration and `Array.from()`
this[Symbol.iterator] = () => this.entries();

this[Symbol.toStringTag] = this._searchParams[Symbol.toStringTag];

// Extend all `URLSearchParams` methods except `sort`
// Extend all `URLSearchParams` methods except perhaps `sort`
Object.keys(URLSearchParams.prototype)
.filter(key =>
.filter(key => options.noSort ? key!=="sort" : true)
.forEach(key => this[key] = (...args) =>
{
if (options.noSort)
{
return key !== "sort";
}
else
const returnValue = this._searchParams[key](...args);

if ((key==="append" || key==="delete" || key==="set" || key==="sort") && typeof this._callback==="function")
{
return true;
this._callback();
}
})
.forEach(key => this[key] = (...args) => this._searchParams[key].call(this._searchParams, ...args));

return returnValue;
});
};

const IncompleteURL = function(url, base)
{
const setSearchParams = value =>
{
this._searchParams = new IncompleteURLSearchParams(value);

this._searchParams._callback = () =>
{
const newValue = this._searchParams.toString();
this._url.search = newValue === "" ? newValue : `?${newValue}`;
};
};

if (url instanceof IncompleteURL)
{
url = url._url;
}

if (base instanceof IncompleteURL)
{
base = base._url;
}

this._url = new URL(url, base);
this._searchParams = new IncompleteURLSearchParams(this._url.search);

setSearchParams(this._url.search);

this[Symbol.toStringTag] = this._url[Symbol.toStringTag];

// Extend all `URL` getters except perhaps `searchParams`
// Extend all `URL` getters/setters except perhaps `searchParams`
Object.keys(URL.prototype)
.filter(key =>
{
if (options.noSearchParams)
{
return key !== "searchParams";
}
else
{
return true;
}
})
.filter(key => options.noSearchParams ? key!=="searchParams" : true)
.forEach(key =>
{
if (key === "searchParams")
{
Object.defineProperty(this, key,
{
get: () => this._searchParams,
set: newValue => this._searchParams = newValue
get: () => this._searchParams
});
}
else
Expand All @@ -71,16 +85,16 @@ const customizeURL = (options={}) =>

if (key === "search")
{
this._searchParams = new IncompleteURLSearchParams(newValue);
setSearchParams(newValue);
}
else if (key === "href")
{
setSearchParams(this._url.search);
}
}
});
}
});

//this.search = "";
this.searchParams.append("body", "value")
console.log(Array.from(this.searchParams))
};

return { IncompleteURL, IncompleteURLSearchParams };
Expand Down
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "incomplete-url",
"description": "Custom-remove features of a WHATWG URL implementation.",
"version": "0.1.0",
"version": "1.0.2",
"license": "MIT",
"author": "Steven Vachon <contact@svachon.com> (https://www.svachon.com/)",
"repository": "stevenvachon/incomplete-url",
Expand All @@ -19,12 +19,10 @@
},
"scripts": {
"ci": "npm run test && nyc report --reporter=text-lcov | coveralls",
"posttest": "nyc report --reporter=html && browserify index.js --global-transform [ babelify --presets [ es2015 ] ] --standalone minURL | uglifyjs --compress --mangle | gzip-size",
"posttest": "nyc report --reporter=html",
"test": "nyc --reporter=text-summary mocha test --check-leaks --bail"
},
"files": [
"index.js"
],
"files": ["index.js"],
"keywords": [
"uri",
"url",
Expand Down
93 changes: 88 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,99 @@
"use strict";
const customizeURL = require("./");
const {describe, it} = require("mocha");
const {expect} = require("chai");
const {it} = require("mocha");



it("noSearchParams = true", function()
it("behaves no differently by default", function()
{
const options = { noSearchParams:true };
const {IncompleteURL, IncompleteURLSearchParams} = customizeURL(options);
const {IncompleteURL, IncompleteURLSearchParams} = customizeURL();

const params = new IncompleteURLSearchParams("?param=value");
const url = new IncompleteURL("http://hostname?param=value");

expect(url).to.not.have.property("searchParams");
expect(params).to.have.property("sort");
expect(url).to.have.property("searchParams");
expect(url.searchParams).to.have.property("sort");

expect(params[Symbol.toStringTag]).to.equal("URLSearchParams");
expect(url[Symbol.toStringTag]).to.equal("URL");

expect(Array.from(params)).to.deep.equal([ ["param","value"] ]);
expect(Array.from(url.searchParams)).to.deep.equal([ ["param","value"] ]);

params.append("param", "value2");
const iteration = [];
for (let param of params) iteration.push(param);
expect(iteration).to.deep.equal([ ["param","value"], ["param","value2"] ]);
expect(params.toString()).to.equal("param=value&param=value2")

expect(() => url.searchParams = params).to.throw(TypeError);

url.search = "";
expect(Array.from(url.searchParams)).to.deep.equal([]);

url.searchParams.set("param", "value");
expect(url.search).to.equal("?param=value");

url.searchParams.delete("param");
expect(url.search).to.equal("");

url.search = "?p2=value2&p1=value&p2=value1";
url.searchParams.sort();
expect(url.search).to.equal("?p1=value&p2=value2&p2=value1");
expect(url.href).to.equal("http://hostname/?p1=value&p2=value2&p2=value1");

url.href = "http://hostname2/?new";
expect(Array.from(url.searchParams)).to.deep.equal([ ["new",""] ]);

url.pathname = "/path/to";
expect(url.href).to.equal("http://hostname2/path/to?new");

expect(new IncompleteURL(url)).to.be.an.instanceOf(IncompleteURL);
expect(new IncompleteURL("/path/", url)).to.be.an.instanceOf(IncompleteURL);
expect(new IncompleteURLSearchParams(params)).to.be.an.instanceOf(IncompleteURLSearchParams);
});



describe("options", function()
{
it("noSearchParams = true", function()
{
const options = { noSearchParams:true };
const {IncompleteURL, IncompleteURLSearchParams} = customizeURL(options);

const url = new IncompleteURL("http://hostname?param=value");

expect(url).to.not.have.property("searchParams");
});



it("noSort = true", function()
{
const options = { noSort:true };
const {IncompleteURL, IncompleteURLSearchParams} = customizeURL(options);

const params = new IncompleteURLSearchParams("?param=value");
const url = new IncompleteURL("http://hostname?param=value");

expect(params).to.not.have.property("sort");
expect(url.searchParams).to.not.have.property("sort");
});



it("all options true", function()
{
const options = { noSearchParams:true, noSort:true };
const {IncompleteURL, IncompleteURLSearchParams} = customizeURL(options);

const params = new IncompleteURLSearchParams("?param=value");
const url = new IncompleteURL("http://hostname?param=value");

expect(params).to.not.have.property("sort");
expect(url).to.not.have.property("searchParams");
});
});

0 comments on commit 06f06fb

Please sign in to comment.