From 20bf04dc7e046e8191443e75be451ec81523a86a Mon Sep 17 00:00:00 2001 From: Marcos Casagrande Date: Mon, 25 May 2020 15:12:09 +0200 Subject: [PATCH] Move getHeaderValueParams & hasHeaderValueOf to util.ts (#5824) --- cli/js/web/body.ts | 18 +----------------- cli/js/web/fetch.ts | 18 +----------------- cli/js/web/util.ts | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 34 deletions(-) diff --git a/cli/js/web/body.ts b/cli/js/web/body.ts index a1cf2038ae..f65ed9cd0e 100644 --- a/cli/js/web/body.ts +++ b/cli/js/web/body.ts @@ -2,6 +2,7 @@ import * as blob from "./blob.ts"; import * as encoding from "./text_encoding.ts"; import * as domTypes from "./dom_types.d.ts"; import { ReadableStreamImpl } from "./streams/readable_stream.ts"; +import { getHeaderValueParams, hasHeaderValueOf } from "./util.ts"; // only namespace imports work for now, plucking out what we need const { TextEncoder, TextDecoder } = encoding; @@ -90,23 +91,6 @@ function bufferFromStream(stream: ReadableStreamReader): Promise { }); } -function getHeaderValueParams(value: string): Map { - const params = new Map(); - // Forced to do so for some Map constructor param mismatch - value - .split(";") - .slice(1) - .map((s): string[] => s.trim().split("=")) - .filter((arr): boolean => arr.length > 1) - .map(([k, v]): [string, string] => [k, v.replace(/^"([^"]*)"$/, "$1")]) - .forEach(([k, v]): Map => params.set(k, v)); - return params; -} - -function hasHeaderValueOf(s: string, value: string): boolean { - return new RegExp(`^${value}[\t\s]*;?`).test(s); -} - export const BodyUsedError = "Failed to execute 'clone' on 'Body': body is already used"; diff --git a/cli/js/web/fetch.ts b/cli/js/web/fetch.ts index 38ca03aca7..3485a770ac 100644 --- a/cli/js/web/fetch.ts +++ b/cli/js/web/fetch.ts @@ -10,23 +10,7 @@ import { close } from "../ops/resources.ts"; import { Buffer } from "../buffer.ts"; import { fetch as opFetch, FetchResponse } from "../ops/fetch.ts"; import { DomFileImpl } from "./dom_file.ts"; - -function getHeaderValueParams(value: string): Map { - const params = new Map(); - // Forced to do so for some Map constructor param mismatch - value - .split(";") - .slice(1) - .map((s): string[] => s.trim().split("=")) - .filter((arr): boolean => arr.length > 1) - .map(([k, v]): [string, string] => [k, v.replace(/^"([^"]*)"$/, "$1")]) - .forEach(([k, v]): Map => params.set(k, v)); - return params; -} - -function hasHeaderValueOf(s: string, value: string): boolean { - return new RegExp(`^${value}[\t\s]*;?`).test(s); -} +import { getHeaderValueParams, hasHeaderValueOf } from "./util.ts"; class Body implements domTypes.Body, ReadableStream, io.Reader, io.Closer { diff --git a/cli/js/web/util.ts b/cli/js/web/util.ts index 0ab367554a..53ff8ef227 100644 --- a/cli/js/web/util.ts +++ b/cli/js/web/util.ts @@ -189,3 +189,22 @@ export function defineEnumerableProps( Reflect.defineProperty(Ctor.prototype, prop, { enumerable: true }); } } + +// @internal +export function getHeaderValueParams(value: string): Map { + const params = new Map(); + // Forced to do so for some Map constructor param mismatch + value + .split(";") + .slice(1) + .map((s): string[] => s.trim().split("=")) + .filter((arr): boolean => arr.length > 1) + .map(([k, v]): [string, string] => [k, v.replace(/^"([^"]*)"$/, "$1")]) + .forEach(([k, v]): Map => params.set(k, v)); + return params; +} + +// @internal +export function hasHeaderValueOf(s: string, value: string): boolean { + return new RegExp(`^${value}[\t\s]*;?`).test(s); +}