Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: type check examples in README files #1121

Merged
merged 6 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
deno-version: ${{ matrix.deno }}

- name: Run tests
run: deno test --doc --unstable --allow-all --coverage=./cov
run: deno test --doc --unstable --allow-all --coverage=./cov --import-map=test_import_map.json

- name: Type check browser compatible modules
shell: bash
Expand Down
59 changes: 53 additions & 6 deletions collections/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ using the given selector. If the selector produces the same key for multiple
elements, the latest one will be used (overriding the ones before it).

```ts
import { associateBy } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";
import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";

const users = [
{ id: "a2e", userName: "Anna" },
{ id: "5f8", userName: "Arnold" },
Expand All @@ -40,6 +43,8 @@ assertEquals(usersById, {
Splits the given array into chunks of the given size and returns them.

```ts
import { chunked } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";

const words = [
"lorem",
"ipsum",
Expand Down Expand Up @@ -71,7 +76,7 @@ include non enumerable properties too.

```ts
import { deepMerge } from "./deep_merge.ts";
import { assertEquals } from "../testing/asserts.ts";
import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";

const a = { foo: true };
const b = { foo: { bar: true } };
Expand All @@ -85,6 +90,8 @@ Returns all elements in the given array that produce a distinct value using the
given selector, preserving order by first occurence.

```ts
import { distinctBy } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";

const names = ["Anna", "Kim", "Arnold", "Kate"];
const exampleNamesByFirstLetter = distinctBy(names, (it) => it.charAt(0));

Expand All @@ -97,6 +104,8 @@ Returns all distinct elements in the given array, preserving order by first
occurence.

```ts
import { distinct } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";

const numbers = [3, 2, 5, 2, 5];
const distinctNumbers = distinct(numbers);

Expand All @@ -109,6 +118,8 @@ Returns a new record with all entries of the given record except the ones that
do not match the given predicate.

```ts
import { filterEntries } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";

const menu = {
"Salad": 11,
"Soup": 8,
Expand All @@ -131,6 +142,8 @@ Returns a new record with all entries of the given record except the ones that
have a key that does not match the given predicate.

```ts
import { filterKeys } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";

const menu = {
"Salad": 11,
"Soup": 8,
Expand All @@ -152,12 +165,14 @@ Returns a new record with all entries of the given record except the ones that
have a value that does not match the given predicate.

```ts
import { filterValues } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";

const people = {
"Arnold": 37,
"Sarah": 7,
"Kim": 23,
};
const adults = filterValues(people, (it) => it.age >= 18);
const adults = filterValues(people, (it) => it >= 18);

console.assert(
adults === {
Expand All @@ -172,6 +187,8 @@ console.assert(
Returns the last element in the given array matching the given predicate.

```ts
import { findLast } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";

const numbers = [4, 2, 7];
const lastEvenNumber = findLast(numbers, (it) => it % 2 === 0);

Expand All @@ -184,6 +201,8 @@ Returns the index of the last element in the given array matching the given
predicate.

```ts
import { findLastIndex } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";

const numbers = [0, 1, 2, 3, 4, 5, 6];
const lastIndexEvenNumber = findLastIndex(numbers, (it) => it % 2 === 0);

Expand All @@ -197,6 +216,8 @@ Record containing the results as keys and all values that produced that key as
values.

```ts
import { groupBy } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";

const people = [
{ name: "Anna" },
{ name: "Arnold" },
Expand All @@ -218,9 +239,11 @@ Returns all distinct elements that appear at least once in each of the given
arrays.

```ts
import { intersect } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";

const lisaInterests = ["Cooking", "Music", "Hiking"];
const kimInterests = ["Music", "Tennis", "Cooking"];
const commonInterests = intersectTest(lisaInterests, kimInterests);
const commonInterests = intersect(lisaInterests, kimInterests);

console.assert(commonInterests === ["Cooking", "Music"]);
```
Expand All @@ -230,7 +253,9 @@ console.assert(commonInterests === ["Cooking", "Music"]);
Applies the given transformer to all entries in the given record and returns a
new record containing the results.

```typescript
```ts
import { mapEntries } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";

const usersById = {
"a2e": { name: "Kim", age: 22 },
"dfe": { name: "Anna", age: 31 },
Expand All @@ -256,10 +281,12 @@ If the transformed entries contain the same key multiple times, only the last
one will appear in the returned record.

```ts
import { mapKeys } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";

const counts = { a: 5, b: 3, c: 8 };

console.assert(
mapKeys(counts, (it) => it.toUppercase()) === {
mapKeys(counts, (it) => it.toUpperCase()) === {
A: 5,
B: 3,
C: 8,
Expand All @@ -273,7 +300,10 @@ Returns a new array, containing all elements in the given array transformed
using the given transformer, except the ones that were transformed to `null` or
`undefined`.

```typescript
```ts
import { mapNotNullish } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";
import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";

const people = [
{ middleName: null },
{ middleName: "William" },
Expand All @@ -292,6 +322,8 @@ record containing the resulting keys associated to the last value that produced
them.

```ts
import { mapValues } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";

const usersById = {
"a5ec": { name: "Mischa" },
"de4f": { name: "Kim" },
Expand All @@ -313,6 +345,8 @@ given array that match the given predicate and the second one containing all
that do not.

```ts
import { partition } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";

const numbers = [5, 6, 7, 8, 9];
const [even, odd] = partition(numbers, (it) => it % 2 == 0);

Expand All @@ -327,6 +361,8 @@ of elements, meaning this will always reutrn the same number of permutations for
a given length of input.

```ts
import { permutations } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";

const numbers = [1, 2];
const windows = permutations(numbers);

Expand All @@ -344,6 +380,9 @@ Returns all elements in the given collection, sorted by their result using the
given selector

```ts
import { sortBy } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";
import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";

const people = [
{ name: "Anna", age: 34 },
{ name: "Kim", age: 42 },
Expand All @@ -363,6 +402,9 @@ assertEquals(sortedByAge, [
Returns all distinct elements that appear in any of the given arrays

```ts
import { union } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";
import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";

const soupIngredients = ["Pepper", "Carrots", "Leek"];
const saladIngredients = ["Carrots", "Radicchio", "Pepper"];
const shoppingList = union(soupIngredients, saladIngredients);
Expand All @@ -377,6 +419,9 @@ returned array holding all first tuple elements and the second one holding all
the second elements

```ts
import { unzip } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";
import { assertEquals } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts";

const parents = [
["Maria", "Jeff"],
["Anna", "Kim"],
Expand All @@ -395,6 +440,8 @@ Builds 2-tuples of elements from the given array with matching indices, stopping
when the smaller array's end is reached

```ts
import { zip } from "https://deno.land/std@$STD_VERSION/collections/mod.ts";

const numbers = [1, 2, 3, 4];
const letters = ["a", "b", "c", "d"];
const pairs = zip(numbers, letters);
Expand Down
23 changes: 6 additions & 17 deletions encoding/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,11 @@ writeVarbig(w: Deno.Writer, x: bigint, o: VarbigOptions = {}): Promise<number>

#### `readMatrix`

```ts
(reader: BufReader, opt: ReadOptions = {
comma: ",",
trimLeadingSpace: false,
lazyQuotes: false,
}): Promise<string[][]>
```

Parse the CSV from the `reader` with the options provided and return
`string[][]`.

#### `parse`

```ts
(input: string | BufReader, opt: ParseOptions = { skipFirstRow: false }): Promise<unknown[]>
```

Parse the CSV string/buffer with the options provided. The result of this
function is as follows:

Expand Down Expand Up @@ -95,10 +83,6 @@ function is as follows:

#### `stringify`

```ts
(data: DataItem[], columns: Column[], options?: StringifyOptions): Promise<string>
```

- **`data`** is the source data to stringify. It's an array of items which are
plain objects or arrays.

Expand Down Expand Up @@ -493,11 +477,16 @@ You can also use custom types by extending schemas.

```ts
import {
DEFAULT_SCHEMA,
parse,
Type,
} from "https://deno.land/std@$STD_VERSION/encoding/yaml.ts";

const MyYamlType = new Type("!myYamlType", {/* your type definition here*/});
const yaml = "...";
const MyYamlType = new Type("!myYamlType", {
kind: "sequence",
/* other type options here*/
});
const MY_SCHEMA = DEFAULT_SCHEMA.extend({ explicit: [MyYamlType] });

parse(yaml, { schema: MY_SCHEMA });
Expand Down
3 changes: 2 additions & 1 deletion encoding/yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export { parse, parseAll } from "./_yaml/parse.ts";
export type { DumpOptions as StringifyOptions } from "./_yaml/stringify.ts";
export { stringify } from "./_yaml/stringify.ts";
export type { SchemaDefinition } from "./_yaml/schema.ts";
export type { StyleVariant, Type } from "./_yaml/type.ts";
export { Type } from "./_yaml/type.ts";
export type { KindType, RepresentFn, StyleVariant } from "./_yaml/type.ts";
export {
CORE_SCHEMA,
DEFAULT_SCHEMA,
Expand Down
7 changes: 5 additions & 2 deletions fs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ ensureSymlinkSync("./folder/targetFile.dat", "./folder/targetFile.link.dat"); //
Detects and format the passed string for the targeted End Of Line character.

```ts
import { format, detect, EOL } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
import {
detect,
EOL,
format,
} from "https://deno.land/std@$STD_VERSION/fs/mod.ts";

const CRLFinput = "deno\r\nis not\r\nnode";
const Mixedinput = "deno\nis not\r\nnode";
Expand All @@ -87,7 +91,6 @@ detect(Mixedinput); // output EOL.CRLF
detect(NoNLinput); // output null

format(CRLFinput, EOL.LF); // output "deno\nis not\nnode"
...
```

### exists
Expand Down
8 changes: 6 additions & 2 deletions http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ let response: Response = {};
const cookie: Cookie = { name: "Space", value: "Cat" };
setCookie(response, cookie);

const cookieHeader = response.headers.get("set-cookie");
const cookieHeader = response.headers!.get("set-cookie");
console.log("Set-Cookie:", cookieHeader);
// Set-Cookie: Space=Cat
```
Expand All @@ -65,7 +65,7 @@ import { deleteCookie } from "https://deno.land/std@$STD_VERSION/http/cookie.ts"
let response: Response = {};
deleteCookie(response, "deno");

const cookieHeader = response.headers.get("set-cookie");
const cookieHeader = response.headers!.get("set-cookie");
console.log("Set-Cookie:", cookieHeader);
// Set-Cookie: deno=; Expires=Thus, 01 Jan 1970 00:00:00 GMT
```
Expand All @@ -74,6 +74,10 @@ console.log("Set-Cookie:", cookieHeader);
> were used to set the cookie.

```ts
import { Response } from "https://deno.land/std@$STD_VERSION/http/server.ts";
import { deleteCookie } from "https://deno.land/std@$STD_VERSION/http/cookie.ts";

let response: Response = {};
deleteCookie(response, "deno", { path: "/", domain: "deno.land" });
```

Expand Down
26 changes: 0 additions & 26 deletions io/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,6 @@ for await (let line of readLines(fileReader)) {
}
```

**Output:**

````text
# std/io

## readLines

```ts
import * as path from "https://deno.land/std@$STD_VERSION/path/mod.ts";

## Rest of the file
````

### readStringDelim

Read reader`[like file]` chunk by chunk, splitting based on delimiter.
Expand All @@ -49,19 +36,6 @@ for await (let line of readStringDelim(fileReader, "\n")) {
}
```

**Output:**

````text
# std/io

## readLines

```ts
import * as path from "https://deno.land/std@$STD_VERSION/path/mod.ts";

## Rest of the file
````

## Reader

### StringReader
Expand Down
Loading