node: Add url.URL and url.URLSearchParams introduced in NodeJs 7.5.0

This commit is contained in:
Flarna
2017-02-10 12:47:24 +01:00
parent ba67ea95fe
commit 287db19b16
2 changed files with 103 additions and 0 deletions

33
node/index.d.ts vendored
View File

@@ -1837,6 +1837,39 @@ declare module "url" {
export function parse(urlStr: string, parseQueryString?: boolean, slashesDenoteHost?: boolean): Url;
export function format(url: Url): string;
export function resolve(from: string, to: string): string;
export class URLSearchParams implements Iterable<string[]> {
constructor(init?: URLSearchParams | string);
append(name: string, value: string): void;
delete(name: string): void;
entries(): Iterator<string[]>;
forEach(callback: (value: string, name: string) => void): void;
get(name: string): string | null;
getAll(name: string): string[];
has(name: string): boolean;
keys(): Iterator<string>;
set(name: string, value: string): void;
toString(): string;
values(): Iterator<string>;
[Symbol.iterator](): Iterator<string[]>;
}
export class URL {
constructor(input: string, base?: string | URL);
hash: string;
host: string;
hostname: string;
href: string;
readonly origin: string;
password: string;
pathname: string;
port: string;
protocol: string;
search: string;
readonly searchParams: URLSearchParams;
username: string;
toString(): string;
}
}
declare module "dns" {

View File

@@ -420,6 +420,76 @@ namespace url_tests {
var helloUrl = url.parse('http://example.com/?hello=world', true)
assert.equal(helloUrl.query.hello, 'world');
}
{
let myURL = new url.URL('https://theuser:thepwd@example.org:81/foo/path?query=string#bar');
assert.equal(myURL.hash, '#bar');
assert.equal(myURL.host, 'example.org:81');
assert.equal(myURL.hostname, 'example.org');
assert.equal(myURL.href, 'https://theuser:thepwd@example.org:81/foo/path?query=string#bar');
assert.equal(myURL.origin, 'https://example.org:81');
assert.equal(myURL.password, 'thepwd');
assert.equal(myURL.username, 'theuser');
assert.equal(myURL.pathname, '/foo/path');
assert.equal(myURL.port, "81");
assert.equal(myURL.protocol, "https:");
assert.equal(myURL.search, "?query=string");
assert.equal(myURL.toString(), 'https://theuser:thepwd@example.org:81/foo/path?query=string#bar');
assert(myURL.searchParams instanceof url.URLSearchParams);
myURL.host = 'example.org:82';
myURL.hostname = 'example.com';
myURL.href = 'http://other.com';
myURL.hash = 'baz';
myURL.password = "otherpwd";
myURL.username = "otheruser";
myURL.pathname = "/otherPath";
myURL.port = "82";
myURL.protocol = "http";
myURL.search = "a=b";
assert.equal(myURL.href, 'http://otheruser:otherpwd@other.com:82/otherPath?a=b#baz');
myURL = new url.URL('/foo', 'https://example.org/');
assert.equal(myURL.href, 'https://example.org/foo');
}
{
const searchParams = new url.URLSearchParams('abc=123');
assert.equal(searchParams.toString(), 'abc=123');
searchParams.forEach((value: string, name: string): void => {
assert.equal(name, 'abc');
assert.equal(value, '123');
});
assert.equal(searchParams.get('abc'), '123');
searchParams.append('abc', 'xyz');
assert.deepEqual(searchParams.getAll('abc'), ['123', 'xyz']);
const entries = searchParams.entries();
assert.deepEqual(entries.next(), { value: ["abc", "123"], done: false});
assert.deepEqual(entries.next(), { value: ["abc", "xyz"], done: false});
assert.deepEqual(entries.next(), { value: undefined, done: true});
const keys = searchParams.keys();
assert.deepEqual(keys.next(), { value: "abc", done: false});
assert.deepEqual(keys.next(), { value: "abc", done: false});
assert.deepEqual(keys.next(), { value: undefined, done: true});
const values = searchParams.values();
assert.deepEqual(values.next(), { value: "123", done: false});
assert.deepEqual(values.next(), { value: "xyz", done: false});
assert.deepEqual(values.next(), { value: undefined, done: true});
searchParams.set('abc', 'b');
assert.deepEqual(searchParams.getAll('abc'), ['b']);
searchParams.delete('a');
assert(!searchParams.has('a'));
assert.equal(searchParams.get('a'), null);
}
}
/////////////////////////////////////////////////////