Skip to content

Commit

Permalink
follow the pattern established in denoland#1062
Browse files Browse the repository at this point in the history
  • Loading branch information
ztplz committed Oct 25, 2018
1 parent 7b09eb6 commit 3805944
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 92 deletions.
22 changes: 20 additions & 2 deletions js/dom_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export interface ProgressEventInit extends EventInit {
total?: number;
}

export interface URLSearchParams {
export interface URLSearchParams extends DomIterable<string, string>{
/**
* Appends a specified key/value pair as a new search parameter.
*/
Expand All @@ -100,6 +100,11 @@ export interface URLSearchParams {
* from the list of all search parameters.
*/
delete(name: string): void;
/** Returns an iterator allowing to go through all key/value pairs
* contained in this URLSearchParams object. The both the key and
* value of each pairs are ByteString objects.
*/
entries(): IterableIterator<[string, string]>;
/**
* Returns the first value associated to the given search parameter.
*/
Expand All @@ -112,6 +117,10 @@ export interface URLSearchParams {
* Returns a Boolean indicating if such a search parameter exists.
*/
has(name: string): boolean;
/** Returns an iterator allowing to go through all keys contained in
* this URLSearchParams object. The keys are ByteString objects.
*/
keys(): IterableIterator<string>;
/**
* Sets the value associated to a given search parameter to the given value.
* If there were several values, delete the others.
Expand All @@ -127,17 +136,26 @@ export interface URLSearchParams {
* Returns a query string suitable for use in a URL.
*/
toString(): string;
/** Returns an iterator allowing to go through all values contained in
* this URLSearchParams object. The values are ByteString objects.
*/
values(): IterableIterator<string>;
/**
* Iterates over each name-value pair in the query
* and invokes the given function.
*/
forEach(
callbackfn: (value: string, key: string, parent: URLSearchParams) => void,
callbackfn: (value: string, key: string, parent: this) => void,
// tslint:disable-next-line:no-any
thisArg?: any
): void;
}

export interface URLSearchParamsConstructor {
new (init?: URLSearchParamsInit): URLSearchParams;
prototype: URLSearchParams;
}

interface EventListener {
(evt: Event): void;
}
Expand Down
6 changes: 3 additions & 3 deletions js/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { globalEval } from "./global_eval";
import { libdeno } from "./libdeno";
import * as textEncoding from "./text_encoding";
import * as timers from "./timers";
import * as urlSearchParams from "./url_search_params";
import * as urlSearchParams_ from "./url_search_params";
import * as domTypes from "./dom_types";

// During the build process, augmentations to the variable `window` in this
Expand Down Expand Up @@ -35,11 +35,11 @@ window.TextDecoder = textEncoding.TextDecoder;
window.atob = textEncoding.atob;
window.btoa = textEncoding.btoa;

window.URLSearchParams = urlSearchParams.URLSearchParams;

window.fetch = fetch_.fetch;

// using the `as` keyword to mask the internal types when generating the
// runtime library
window.Headers = fetch_.Headers as domTypes.HeadersConstructor;
window.Blob = blob.DenoBlob;
// tslint:disable-next-line:max-line-length
window.URLSearchParams = urlSearchParams_.URLSearchParams as domTypes.URLSearchParamsConstructor;
8 changes: 4 additions & 4 deletions js/mixins/dom_iterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ export function DomIterableMixin<K, V, TBase extends Constructor>(
// tslint:disable-next-line:variable-name
const DomIterable = class extends Base {
*entries(): IterableIterator<[K, V]> {
for (const entry of (this as any)[dataSymbol].entries()) {
for (const entry of (this as any)[dataSymbol]) {
yield entry;
}
}

*keys(): IterableIterator<K> {
for (const key of (this as any)[dataSymbol].keys()) {
for (const [key,] of (this as any)[dataSymbol]) {
yield key;
}
}

*values(): IterableIterator<V> {
for (const value of (this as any)[dataSymbol].values()) {
for (const [, value] of (this as any)[dataSymbol]) {
yield value;
}
}
Expand All @@ -47,7 +47,7 @@ export function DomIterableMixin<K, V, TBase extends Constructor>(
thisArg?: any
): void {
callbackfn = callbackfn.bind(thisArg == null ? window : Object(thisArg));
for (const [key, value] of (this as any)[dataSymbol].entries()) {
for (const [key, value] of (this as any)[dataSymbol]) {
callbackfn(value, key, this);
}
}
Expand Down
105 changes: 22 additions & 83 deletions js/url_search_params.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
import { CreateIterableIterator } from "./util";
import { DomIterableMixin } from "./mixins/dom_iterable";

export class URLSearchParams {
private params: Array<[string, string]> = [];
const params = Symbol("url_search_params array");

class URLSearchParamsBase {
private [params]: Array<[string, string]> = [];

constructor(init: string | string[][] | Record<string, string> = "") {
if (typeof init === "string") {
Expand Down Expand Up @@ -42,7 +44,7 @@ export class URLSearchParams {
* searchParams.append('name', 'second');
*/
append(name: string, value: string): void {
this.params.push([name, value]);
this[params].push([name, value]);
}

/** Deletes the given search parameter and its associated value,
Expand All @@ -52,9 +54,9 @@ export class URLSearchParams {
*/
delete(name: string): void {
let i = 0;
while (i < this.params.length) {
if (this.params[i][0] === name) {
this.params.splice(i, 1);
while (i < this[params].length) {
if (this[params][i][0] === name) {
this[params].splice(i, 1);
} else {
i++;
}
Expand All @@ -68,7 +70,7 @@ export class URLSearchParams {
*/
getAll(name: string): string[] {
const values = [];
for (const entry of this.params) {
for (const entry of this[params]) {
if (entry[0] === name) {
values.push(entry[1]);
}
Expand All @@ -82,7 +84,7 @@ export class URLSearchParams {
* searchParams.get('name');
*/
get(name: string): string | null {
for (const entry of this.params) {
for (const entry of this[params]) {
if (entry[0] === name) {
return entry[1];
}
Expand All @@ -97,7 +99,7 @@ export class URLSearchParams {
* searchParams.has('name');
*/
has(name: string): boolean {
return this.params.some(entry => entry[0] === name);
return this[params].some(entry => entry[0] === name);
}

/** Sets the value associated with a given search parameter to the
Expand All @@ -119,92 +121,29 @@ export class URLSearchParams {
* searchParams.sort();
*/
sort(): void {
this.params = this.params.sort(
this[params] = this[params].sort(
(a, b) => (a[0] === b[0] ? 0 : a[0] > b[0] ? 1 : -1)
);
}

/** Calls a function for each element contained in this object in
* place and return undefined. Optionally accepts an object to use
* as this when executing callback as second argument.
*
* searchParams.forEach((value, key, parent) => {
* console.log(value, key, parent);
* });
*
*/
forEach(
callbackfn: (value: string, key: string, parent: URLSearchParams) => void,
// tslint:disable-next-line:no-any
thisArg?: any
) {
if (typeof thisArg !== "undefined") {
callbackfn = callbackfn.bind(thisArg);
}
for (const [key, value] of this.entries()) {
callbackfn(value, key, this);
}
}

/** Returns an iterator allowing to go through all keys contained
* in this object.
*
* for (const key of searchParams.keys()) {
* console.log(key);
* }
*/
keys(): IterableIterator<string> {
const list = this.params.map(param => param[0]);
const iterators = list.values();
return new CreateIterableIterator(iterators);
}

/** Returns an iterator allowing to go through all values contained
* in this object.
*
* for (const value of searchParams.values()) {
* console.log(value);
* }
*/
values(): IterableIterator<string> {
const list = this.params.map(param => param[1]);
const iterators = list.values();
return new CreateIterableIterator(iterators);
}

/** Returns an iterator allowing to go through all key/value
* pairs contained in this object.
*
* for (const [key, value] of searchParams.entries()) {
* console.log(key, value);
* }
*/
entries(): IterableIterator<[string, string]> {
const iterators = this.params.values();
return new CreateIterableIterator(iterators);
}

/** Returns an iterator allowing to go through all key/value
* pairs contained in this object.
*
* for (const [key, value] of searchParams[Symbol.iterator]()) {
* console.log(key, value);
* }
*/
[Symbol.iterator](): IterableIterator<[string, string]> {
return this.entries();
}

/** Returns a query string suitable for use in a URL.
*
* searchParams.toString();
*/
toString(): string {
return this.params
return this[params]
.map(
tuple =>
`${encodeURIComponent(tuple[0])}=${encodeURIComponent(tuple[1])}`
)
.join("&");
}
}

// @internal
// tslint:disable:max-line-length
// tslint:disable-next-line:variable-name
export const URLSearchParams = DomIterableMixin<string, string, typeof URLSearchParamsBase>(
URLSearchParamsBase,
params
);

0 comments on commit 3805944

Please sign in to comment.