Skip to content

Commit

Permalink
Merge pull request #2247 from tetsuharuohzeki/improve-doc
Browse files Browse the repository at this point in the history
Update examples section for "how to import"
  • Loading branch information
tetsuharuohzeki committed May 13, 2024
2 parents 21b620f + 4511fbf commit 2ba6522
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
32 changes: 22 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ We target to run in following environments.
- ES Module ([ES2020](https://262.ecma-international.org/11.0/) level).
- CommonJS
- A runtime environment or module bundler must support Node.js' [package.json's `exports` field](https://nodejs.org/api/packages.html#package-entry-points) (Newer is better).
- We require TypeScript's [`--moduleResolution`](https://www.typescriptlang.org/tsconfig/#moduleResolution)
is set as `node16`, `bundler`, or others that supports `exports` field if your project use TypeScript.


### Caution
Expand Down Expand Up @@ -171,28 +173,38 @@ Additional documents are in [`docs/`](./docs/).

### How to import

**You can use [these paths](./docs/public_api_list.md) in both of CommonJS style and ES Module style.**
This package provides some sub directories to import various functions (e.g. `option-t/PlainResult`).

If you're project cannot import by their path, please read [this guide](./docs/how_to_import.md) to know more details.

#### Examples

```ts
// Import only functions or types which you would like to use.
import { isNotNull, type Nullable } from 'option-t/Nullable';
import { unwrapNullable } from 'option-t/Nullable/Nullable';
import { createOk, isOk } from 'option-t/PlainResult';
// Import functions and types which you would like to use:
import { type Maybe } from 'option-t/Maybe';
import { type Nullable, isNotNull } from 'option-t/Nullable';
import { type Undefinable } from 'option-t/Undefinable';
import { type Result, createOk, isOk } from 'option-t/PlainResult';
```

```ts
// You can use `<TypeName>.<operatorName>` style.
// You can also use `<TypeName>.<operatorName>`
import { Nullable } from 'option-t/Nullable/namespace';

declare let numberOrNull: Nullable.Nullable<number>;
// IntelliSense can suggest and narrow down by the order of Type -> related operations.
const some = Nullable.unwrapOr(numberOrNull, -1);
```

```ts
// You can import only a specific function by the more detailed path.
import { type Nullable, unwrapNullable } from 'option-t/Nullable/Nullable';
import { unwrapOrForNullable } from 'option-t/Nullable/unwrapOr';
```

#### See also

**You can use [these paths](./docs/public_api_list.md) in both of CommonJS style and ES Module style.**
This package provides some sub directories to import various functions (e.g. `option-t/PlainResult`).

If you're project cannot import by their path, please read [this guide](./docs/how_to_import.md) to know more details.


### Deprecation

Expand Down
25 changes: 13 additions & 12 deletions docs/how_to_import.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,31 @@
### Examples

```ts
// Import only functions or types which you would like to use.
import { isNotNull, type Nullable } from 'option-t/Nullable';
import { unwrapNullable } from 'option-t/Nullable/Nullable';
import { createOk, isOk } from 'option-t/PlainResult';
// Import functions and types which you would like to use:
import { type Maybe } from 'option-t/Maybe';
import { type Nullable, isNotNull } from 'option-t/Nullable';
import { type Undefinable } from 'option-t/Undefinable';
import { type Result, createOk, isOk } from 'option-t/PlainResult';
```

```ts
// You can use `<TypeName>.<operatorName>` style.
// You can also use `<TypeName>.<operatorName>`
import { Nullable } from 'option-t/Nullable/namespace';

declare let numberOrNull: Nullable.Nullable<number>;
// IntelliSense can suggest and narrow down by the order of Type -> related operations.
const some = Nullable.unwrapOr(numberOrNull, -1);
```

```js
// for commonjs
const { isNotNull } = require('option-t/Nullable');
const { mapForNullable } = require('option-t/Nullable/map');
const { createOk, isOk } = require('option-t/PlainResult');
```ts
// You can import only a specific function by the more detailed path.
import { type Nullable, unwrapNullable } from 'option-t/Nullable/Nullable';
import { unwrapOrForNullable } from 'option-t/Nullable/unwrapOr';
```

### Cautions

1. If your project use TypeScript, you need to set TypeScript's `--moduleResolution` with `'bundler'` or `node16`
or other values that enables to support `exports` field in package.json.
1. If your project use TypeScript, you need to set TypeScript's [`--moduleResolution`](https://www.typescriptlang.org/tsconfig/#moduleResolution)
with `bundler`, `node16` or other values that enables to support `exports` field in package.json.
- If your project still use TypeScript's `--moduleResolution` with `node/node10` setting,
please use [**`v35`**](https://github.com/option-t/option-t/tree/v35.0.0).

0 comments on commit 2ba6522

Please sign in to comment.