Skip to content

Commit

Permalink
Merge branch 'main' into fix/expect-custom-equality-case
Browse files Browse the repository at this point in the history
  • Loading branch information
eryue0220 authored Feb 27, 2024
2 parents 6bccad1 + 27517a8 commit 22c4561
Show file tree
Hide file tree
Showing 17 changed files with 185 additions and 22 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,25 @@ Check out the documentation [here](https://deno.land/std?doc).
import * as fs from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
```

1. Do not import symbols with an underscore in the name.
1. Do not import symbols with a name _prefixed_ by an underscore (they're not
intended for public use).

Bad:
```ts
```ts, ignore
import { _format } from "https://deno.land/std@$STD_VERSION/path/_common/format.ts";
```

1. Do not import modules with an underscore in the path.
1. Do not import modules with a directory or filename _prefixed_ by an
underscore (they're not intended for public use).

Bad:
```ts, ignore
import { createLPS } from "https://deno.land/std@$STD_VERSION/streams/_common.ts";
```

Good:
```ts
import { filterInPlace } from "https://deno.land/std@$STD_VERSION/collections/_utils.ts";
import { TextLineStream } from "https://deno.land/std@$STD_VERSION/streams/text_line_stream.ts";
```

1. Do not import test modules or test data.
Expand Down
4 changes: 4 additions & 0 deletions assert/assert_rejects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { assertIsError } from "./assert_is_error.ts";
/**
* Executes a function which returns a promise, expecting it to reject.
*
* To assert that a synchronous function throws, use {@linkcode assertThrows}.
*
* @example
* ```ts
* import { assertRejects } from "https://deno.land/std@$STD_VERSION/assert/assert_rejects.ts";
Expand All @@ -22,6 +24,8 @@ export function assertRejects(
* If it does not, then it throws. An error class and a string that should be
* included in the error message can also be asserted.
*
* To assert that a synchronous function throws, use {@linkcode assertThrows}.
*
* @example
* ```ts
* import { assertRejects } from "https://deno.land/std@$STD_VERSION/assert/assert_rejects.ts";
Expand Down
6 changes: 6 additions & 0 deletions assert/assert_throws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { AssertionError } from "./assertion_error.ts";
* Executes a function, expecting it to throw. If it does not, then it
* throws.
*
* To assert that an asynchronous function rejects, use
* {@linkcode assertRejects}.
*
* @example
* ```ts
* import { assertThrows } from "https://deno.land/std@$STD_VERSION/assert/assert_throws.ts";
Expand All @@ -23,6 +26,9 @@ export function assertThrows(
* throws. An error class and a string that should be included in the
* error message can also be asserted.
*
* To assert that an asynchronous function rejects, use
* {@linkcode assertRejects}.
*
* @example
* ```ts
* import { assertThrows } from "https://deno.land/std@$STD_VERSION/assert/assert_throws.ts";
Expand Down
2 changes: 2 additions & 0 deletions collections/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ export * from "./drop_last_while.ts";
export * from "./reduce_groups.ts";
export * from "./sample.ts";
export * from "./running_reduce.ts";
export * from "./pick.ts";
export * from "./omit.ts";
26 changes: 26 additions & 0 deletions collections/omit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

/**
* Creates a new object by excluding the specified keys from the provided object.
*
* @example
* ```ts
* import { omit } from "https://deno.land/std@$STD_VERSION/collections/omit.ts";
* import { assertEquals } from "https://deno.land/std@$STD_VERSION/assert/assert_equals.ts";
*
* const obj = { a: 5, b: 6, c: 7, d: 8 };
* const omitted = omit(obj, ["a", "c"]);
*
* assertEquals(omitted, { b: 6, d: 8 });
* ```
*/
export function omit<T extends object, K extends keyof T>(
obj: Readonly<T>,
keys: readonly K[],
): Omit<T, K> {
const excludes = new Set(keys);
const has = excludes.has.bind(excludes);
return Object.fromEntries(
Object.entries(obj).filter(([k, _]) => !has(k as K)),
) as Omit<T, K>;
}
38 changes: 38 additions & 0 deletions collections/omit_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { assertEquals, assertNotStrictEquals } from "../assert/mod.ts";
import { omit } from "./omit.ts";

Deno.test({
name: "omit() returns a new object from the provided object",
fn() {
const obj = { a: 5, b: 6, c: 7, d: 8 };
const omitted = omit(obj, []);

assertEquals(omitted, { a: 5, b: 6, c: 7, d: 8 });
assertNotStrictEquals(omitted, obj);
},
});

Deno.test({
name:
"omit() returns a new object from the provided object without the provided keys",
fn() {
const obj = { a: 5, b: 6, c: 7, d: 8 };
const omitted = omit(obj, ["a", "c"]);

assertEquals(omitted, { b: 6, d: 8 });
assertNotStrictEquals(omitted, obj);
},
});

Deno.test({
name: "omit() returns an empty object when the provided keys is empty",
fn() {
const obj = { a: 5, b: 6, c: 7, d: 8 };
const omitted = omit(obj, ["a", "b", "c", "d"]);

assertEquals(omitted, {});
assertNotStrictEquals(omitted, obj);
},
});
22 changes: 22 additions & 0 deletions collections/pick.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

/**
* Creates a new object by including the specified keys from the provided object.
*
* @example
* ```ts
* import { pick } from "https://deno.land/std@$STD_VERSION/collections/pick.ts";
* import { assertEquals } from "https://deno.land/std@$STD_VERSION/assert/assert_equals.ts";
*
* const obj = { a: 5, b: 6, c: 7, d: 8 };
* const picked = pick(obj, ["a", "c"]);
*
* assertEquals(picked, { a: 5, c: 7 });
* ```
*/
export function pick<T extends object, K extends keyof T>(
obj: Readonly<T>,
keys: readonly K[],
): Pick<T, K> {
return Object.fromEntries(keys.map((k) => [k, obj[k]])) as Pick<T, K>;
}
39 changes: 39 additions & 0 deletions collections/pick_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { assertEquals, assertNotStrictEquals } from "../assert/mod.ts";
import { pick } from "./pick.ts";

Deno.test({
name: "pick() returns a new empty object when no keys are provided",
fn() {
const obj = { a: 5, b: 6, c: 7, d: 8 };
const picked = pick(obj, []);

assertEquals(picked, {});
assertNotStrictEquals(picked, obj);
},
});

Deno.test({
name:
"pick() returns a new object from the provided object with the provided keys",
fn() {
const obj = { a: 5, b: 6, c: 7, d: 8 };
const picked = pick(obj, ["a", "c"]);

assertEquals(picked, { a: 5, c: 7 });
assertNotStrictEquals(picked, obj);
},
});

Deno.test({
name:
"pick() returns a new object from the provided object with the provided keys (all keys are provided)",
fn() {
const obj = { a: 5, b: 6, c: 7, d: 8 };
const picked = pick(obj, ["a", "b", "c", "d"]);

assertEquals(picked, { a: 5, b: 6, c: 7, d: 8 });
assertNotStrictEquals(picked, obj);
},
});
33 changes: 26 additions & 7 deletions log/base_handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { getLevelByName, LevelName } from "./levels.ts";
import { getLevelByName, getLevelName, LevelName, LogLevel } from "./levels.ts";
import type { LogRecord } from "./logger.ts";

export type FormatterFunction = (logRecord: LogRecord) => string;
Expand All @@ -11,14 +11,33 @@ export interface BaseHandlerOptions {
}

export class BaseHandler {
level: number;
levelName: LevelName;
#levelName: LevelName;
#level: LogLevel;
formatter: FormatterFunction;

constructor(levelName: LevelName, options: BaseHandlerOptions = {}) {
this.level = getLevelByName(levelName);
this.levelName = levelName;
this.formatter = options.formatter || DEFAULT_FORMATTER;
constructor(
levelName: LevelName,
{ formatter = DEFAULT_FORMATTER }: BaseHandlerOptions = {},
) {
this.#levelName = levelName;
this.#level = getLevelByName(levelName);
this.formatter = formatter;
}

get level() {
return this.#level;
}
set level(level: LogLevel) {
this.#level = level;
this.#levelName = getLevelName(level);
}

get levelName() {
return this.#levelName;
}
set levelName(levelName: LevelName) {
this.#levelName = levelName;
this.#level = getLevelByName(levelName);
}

handle(logRecord: LogRecord) {
Expand Down
6 changes: 3 additions & 3 deletions media_types/_util_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { assertEquals } from "../assert/mod.ts";
import { consumeMediaParam, consumeToken, consumeValue } from "./_util.ts";

Deno.test({
name: "media_types::util - consumeToken()",
name: "consumeToken()",
fn() {
const fixtures = [
["foo bar", "foo", " bar"],
Expand All @@ -19,7 +19,7 @@ Deno.test({
});

Deno.test({
name: "media_types::util - consumeValue()",
name: "consumeValue()",
fn() {
const fixtures = [
["foo bar", "foo", " bar"],
Expand All @@ -44,7 +44,7 @@ Deno.test({
});

Deno.test({
name: "media_types::util - consumeMediaParam()",
name: "consumeMediaParam()",
fn() {
const fixtures = [
[" ; foo=bar", "foo", "bar", ""],
Expand Down
4 changes: 2 additions & 2 deletions media_types/content_type_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { contentType } from "./content_type.ts";
import { assertEquals } from "../assert/mod.ts";

Deno.test({
name: "media_types - contentType()",
name: "contentType()",
fn() {
const fixtures = [
[".json", "application/json; charset=UTF-8"],
Expand All @@ -22,7 +22,7 @@ Deno.test({
});

Deno.test({
name: "media_types - contentType()",
name: "contentType() implies types",
fn() {
let _str: string;
// For well-known content types, the return type is a string.
Expand Down
2 changes: 1 addition & 1 deletion media_types/extension_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { assertEquals } from "../assert/mod.ts";
import { extension } from "./mod.ts";

Deno.test({
name: "media_types - extension()",
name: "extension()",
fn() {
const fixtures: [string, string | undefined][] = [
["image/gif", "gif"],
Expand Down
2 changes: 1 addition & 1 deletion media_types/extensions_by_type_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { assertEquals } from "../assert/mod.ts";
import { extensionsByType } from "./extensions_by_type.ts";

Deno.test({
name: "media_types - extensionsByType()",
name: "extensionsByType()",
fn() {
const fixtures: [string, string[] | undefined][] = [
["image/gif", ["gif"]],
Expand Down
2 changes: 1 addition & 1 deletion media_types/format_media_type_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { assertEquals } from "../assert/mod.ts";
import { formatMediaType } from "./mod.ts";

Deno.test({
name: "media_types - formatMediaType",
name: "formatMediaType()",
fn() {
const fixtures = [
["noslash", { X: "Y" }, "noslash; x=Y"],
Expand Down
2 changes: 1 addition & 1 deletion media_types/get_charset_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { assertEquals } from "../assert/mod.ts";
import { getCharset } from "./mod.ts";

Deno.test({
name: "media-types - getCharset()",
name: "getCharset()",
fn() {
const fixtures = [
["text/plain", "UTF-8"],
Expand Down
2 changes: 1 addition & 1 deletion media_types/parse_media_type_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { assertEquals } from "../assert/mod.ts";
import { parseMediaType } from "./mod.ts";

Deno.test({
name: "media_types - parseMediaType()",
name: "parseMediaType()",
fn() {
const nameFoo = { "name": "foo" };
const fixtures: [string, string, Record<string, string> | undefined][] = [
Expand Down
2 changes: 1 addition & 1 deletion media_types/type_by_extension_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { assertEquals } from "../assert/mod.ts";
import { typeByExtension } from "./mod.ts";

Deno.test({
name: "media_types - typeByExtension",
name: "typeByExtension()",
fn() {
const fixtures = [
["js", "application/javascript"],
Expand Down

0 comments on commit 22c4561

Please sign in to comment.