Skip to content

Commit 9c6eb47

Browse files
committed
v0.1.0
1 parent 6b0260f commit 9c6eb47

File tree

6 files changed

+193
-60
lines changed

6 files changed

+193
-60
lines changed

.gitignore

Lines changed: 3 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,4 @@
1-
# Logs
2-
logs
3-
*.log
4-
npm-debug.log*
5-
yarn-debug.log*
6-
yarn-error.log*
7-
8-
# Runtime data
9-
pids
10-
*.pid
11-
*.seed
12-
*.pid.lock
13-
14-
# Directory for instrumented libs generated by jscoverage/JSCover
15-
lib-cov
16-
17-
# Coverage directory used by tools like istanbul
18-
coverage
19-
20-
# nyc test coverage
21-
.nyc_output
22-
23-
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24-
.grunt
25-
26-
# Bower dependency directory (https://bower.io/)
27-
bower_components
28-
29-
# node-waf configuration
30-
.lock-wscript
31-
32-
# Compiled binary addons (http://nodejs.org/api/addons.html)
33-
build/Release
34-
35-
# Dependency directories
1+
coverage/
362
node_modules/
37-
jspm_packages/
38-
39-
# Typescript v1 declaration files
40-
typings/
41-
42-
# Optional npm cache directory
43-
.npm
44-
45-
# Optional eslint cache
46-
.eslintcache
47-
48-
# Optional REPL history
49-
.node_repl_history
50-
51-
# Output of 'npm pack'
52-
*.tgz
53-
54-
# Yarn Integrity file
55-
.yarn-integrity
56-
57-
# dotenv environment variables file
58-
.env
59-
3+
.nyc_output/
4+
package-lock.json

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- 6
4+
- 8
5+
script: npm test

README.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,45 @@
1-
# incomplete-url
2-
Custom-remove features of a WHATWG URL implementation for testing.
1+
# incomplete-url [![NPM Version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url]
2+
3+
> Custom-remove features of a WHATWG [`URL`](https://developer.mozilla.org/en/docs/Web/API/URL) implementation.
4+
5+
6+
This is useful when simulating the incomplete `URL` implementations available in some of today's modern web browsers.
7+
8+
9+
## Installation
10+
11+
[Node.js](http://nodejs.org/) `>= 6` is required. To install, type this at the command line:
12+
```shell
13+
npm install incomplete-url
14+
```
15+
16+
17+
## Usage
18+
19+
```js
20+
const customizeURL = require('incomplete-url');
21+
22+
const {IncompleteURL, IncompleteURLSearchParams} = customizeURL(options);
23+
24+
const url = new IncompleteURL('http://domain/');
25+
const params = new IncompleteURLSearchParams('?param=value');
26+
```
27+
28+
29+
## Options
30+
31+
### `noSearchParams`
32+
Type: `Boolean`
33+
Default value: `false`
34+
When set to `true`, the output `URL` class will not expose a `searchParams` property when instantiated.
35+
36+
### `noSort`
37+
Type: `Boolean`
38+
Default value: `false`
39+
When set to `true`, the output `URLSearchParams` class (and `URL#searchParams`) will not expose a `sort` method when instantiated.
40+
41+
42+
[npm-image]: https://img.shields.io/npm/v/incomplete-url.svg
43+
[npm-url]: https://npmjs.org/package/incomplete-url
44+
[travis-image]: https://img.shields.io/travis/stevenvachon/incomplete-url.svg
45+
[travis-url]: https://travis-ci.org/stevenvachon/incomplete-url

index.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
"use strict";
2+
const {URL, URLSearchParams} = require("universal-url");
3+
4+
5+
6+
const customizeURL = (options={}) =>
7+
{
8+
const IncompleteURLSearchParams = function(params)
9+
{
10+
this._searchParams = new URLSearchParams(params);
11+
12+
// Support iteration and `Array.from()`
13+
this[Symbol.iterator] = () => this.entries();
14+
15+
this[Symbol.toStringTag] = this._searchParams[Symbol.toStringTag];
16+
17+
// Extend all `URLSearchParams` methods except `sort`
18+
Object.keys(URLSearchParams.prototype)
19+
.filter(key =>
20+
{
21+
if (options.noSort)
22+
{
23+
return key !== "sort";
24+
}
25+
else
26+
{
27+
return true;
28+
}
29+
})
30+
.forEach(key => this[key] = (...args) => this._searchParams[key].call(this._searchParams, ...args));
31+
};
32+
33+
const IncompleteURL = function(url, base)
34+
{
35+
this._url = new URL(url, base);
36+
this._searchParams = new IncompleteURLSearchParams(this._url.search);
37+
38+
this[Symbol.toStringTag] = this._url[Symbol.toStringTag];
39+
40+
// Extend all `URL` getters except perhaps `searchParams`
41+
Object.keys(URL.prototype)
42+
.filter(key =>
43+
{
44+
if (options.noSearchParams)
45+
{
46+
return key !== "searchParams";
47+
}
48+
else
49+
{
50+
return true;
51+
}
52+
})
53+
.forEach(key =>
54+
{
55+
if (key === "searchParams")
56+
{
57+
Object.defineProperty(this, key,
58+
{
59+
get: () => this._searchParams,
60+
set: newValue => this._searchParams = newValue
61+
});
62+
}
63+
else
64+
{
65+
Object.defineProperty(this, key,
66+
{
67+
get: () => this._url[key],
68+
set: newValue =>
69+
{
70+
this._url[key] = newValue;
71+
72+
if (key === "search")
73+
{
74+
this._searchParams = new IncompleteURLSearchParams(newValue);
75+
}
76+
}
77+
});
78+
}
79+
});
80+
81+
//this.search = "";
82+
this.searchParams.append("body", "value")
83+
console.log(Array.from(this.searchParams))
84+
};
85+
86+
return { IncompleteURL, IncompleteURLSearchParams };
87+
};
88+
89+
90+
91+
module.exports = customizeURL;

package.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "incomplete-url",
3+
"description": "Custom-remove features of a WHATWG URL implementation.",
4+
"version": "0.1.0",
5+
"license": "MIT",
6+
"author": "Steven Vachon <[email protected]> (https://www.svachon.com/)",
7+
"repository": "stevenvachon/incomplete-url",
8+
"dependencies": {
9+
"universal-url": "^1.0.0"
10+
},
11+
"devDependencies": {
12+
"chai": "^4.0.2",
13+
"coveralls": "^2.13.1",
14+
"mocha": "^3.4.2",
15+
"nyc": "^11.0.3"
16+
},
17+
"engines": {
18+
"node": ">= 6"
19+
},
20+
"scripts": {
21+
"ci": "npm run test && nyc report --reporter=text-lcov | coveralls",
22+
"posttest": "nyc report --reporter=html && browserify index.js --global-transform [ babelify --presets [ es2015 ] ] --standalone minURL | uglifyjs --compress --mangle | gzip-size",
23+
"test": "nyc --reporter=text-summary mocha test --check-leaks --bail"
24+
},
25+
"files": [
26+
"index.js"
27+
],
28+
"keywords": [
29+
"uri",
30+
"url",
31+
"whatwg"
32+
]
33+
}

test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict";
2+
const customizeURL = require("./");
3+
const {expect} = require("chai");
4+
const {it} = require("mocha");
5+
6+
7+
8+
it("noSearchParams = true", function()
9+
{
10+
const options = { noSearchParams:true };
11+
const {IncompleteURL, IncompleteURLSearchParams} = customizeURL(options);
12+
13+
const url = new IncompleteURL("http://hostname?param=value");
14+
15+
expect(url).to.not.have.property("searchParams");
16+
});

0 commit comments

Comments
 (0)