Skip to content

Commit

Permalink
fix(website): fix typos and grammar, elaborate documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
inhibitor1217 committed Apr 2, 2022
1 parent 07b4fb4 commit 70a7a39
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 25 deletions.
18 changes: 15 additions & 3 deletions website/docs/advanced-usages/extend-base.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ sidebar_position: 1

# Defining additional interfaces

Further extending the `extend`ed class is also possible.
Further extending the class of an `extend`ed enum is also possible.

- Define an interface for additional methods or getters.

Expand All @@ -16,7 +16,7 @@ interface IAnimal {
}
```

- Provide the interface as **third type paramter** when `extend`ing the enum.
- Provide the interface as **third type paramter** of the `extend` utility.

```ts
enum _Animal { Cat, Dog, Eagle }
Expand All @@ -26,7 +26,7 @@ class Animal extends extend<typeof _Animal, _Animal, IAnimal>(_Animal) {
}
```

- Implement the attached interface.
- Implement the attached interface at the `extend`ed class.

```ts
class Animal extends extend<typeof _Animal, _Animal, IAnimal>(_Animal) {
Expand All @@ -45,3 +45,15 @@ class Animal extends extend<typeof _Animal, _Animal, IAnimal>(_Animal) {

}
```

Now, all the enum values will implement the attached interface.

```ts
Animal.Dog.favorite; // true
Aniaml.Cat.favorite; // false

Animal.Dog.walks(); // true
Animal.Eagle.walks(); // false

Animal.Cat.code('animal'); // "animal:0"
```
16 changes: 8 additions & 8 deletions website/docs/advanced-usages/override.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sidebar_position: 2

## Motivation

Extended features - `is`, `from`, etc - determines the equality of the primitive and the enum by its original definition.
Extended features - `is`, `from`, etc - determine the equality of the primitive and the enum by its native definition. In the following example, values in enum `_Priority` are defined by the corresponding number primitives, `300`, `200`, and `100`.

```ts
enum _Priority { High = 300, Medium = 200, Low = 100 }
Expand All @@ -16,10 +16,10 @@ Priority.High.is(300); // true
Priority.Medium.is(200); // true
```

However, in some cases, you would want other values to be regarded as a valid primitive for this enum. In the above case, `Priority` enum is defined using number primitives, `100`, `200`, `300`.
However, in some cases, you would want other values to be regarded as a valid primitive for this enum.

- What if the priority should be defined using number ranges, `[0, 100], [101, 200], [201, 300]`?
- What if string values `"low", "medium", "high"` should be parsed into `Priority` enum?
- What if the priority should be defined by ranges, `[0, 100], [101, 200], [201, 300]`?
- What if string values `"low", "medium", "high"` should be also parsed to `Priority` enum?

```ts
Priority.High.is(250); // expected: true
Expand All @@ -34,17 +34,17 @@ Priority.High
}); // expected: false
```

To meet the additional needs for overriding the default behavior, `extend`ed enum exposes an interface `eq`.
To meet the additional needs for overriding the default behavior, `extend`ed enums expose an interface `eq`.

## `eq`

`eq` determines the equality of the given primitive value and the defined value. It governs the comparison performed in `extend`ed enums, such as in `from`, `is`, or `match`.

**Using this method directly in useases is not recommended.** `is` or `isNot` are perhaps the methods you are looking for.
**Using this method directly in usecases is not recommended.** `is` or `isNot` are perhaps the methods you are looking for.

In default behavior, `eq` does the reference equality comparison (`===`) Overriding this method will alter the core behavior, granting new possibilities.(See the example.)
In default behavior, `eq` does the reference equality comparison (`===`). Overriding this method will alter the core behavior, granting new possibilities. (See the example.)

In the following example, the case-insenstivie comparison overrides the default comparison. Observe how the behavior of `from`, `is`, or `match` differs from the original behavior.
In the following example, the case-insensitive comparison overrides the default comparison, as specified at overrided method `eq`. Observe how the behaviors of `from`, `is`, or `match` differ from the original behaviors.

```ts
enum _Level { Low = 'LOW', High = 'HIGH' }
Expand Down
6 changes: 3 additions & 3 deletions website/docs/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type T = typeof Fruit.Apple & typeof Fruit.Orange; // never

## Reverse mapping

Native TypeScript enums supports **reverse mapping**, which allows to retrieve keys back from the enum values.
Native TypeScript enums support **reverse mapping**, which allows us to retrieve keys back from the values.

```ts
enum _Fruit { Apple, Orange, Pear, Strawberry }
Expand All @@ -39,7 +39,7 @@ console.log(_Fruit[_Fruit.Apple]); // "Apple"
console.log(_Fruit[_Fruit.Orange]); // "Orange"
```

`extend`ed enum values are **objects**, so it is not an indexable type in JavaScript. Instead, it provides `keyOf` API to support reverse mapping.
`extend`ed enum value is an **objects**, so it is not an indexable type in JavaScript. Instead, it provides `keyOf` API to support reverse mapping.

```ts
console.log(Fruit.keyOf(Fruit.Apple)); // "Apple"
Expand All @@ -62,7 +62,7 @@ const user = {
favoriteFruits: [Fruit.Apple, Fruit.Strawberry],
}

console.log(JSON.stringify(user)); // {"id":"42","favoriteFruits":[0,3]}
console.log(JSON.stringify(user)); // { "id": "42", "favoriteFruits": [0, 3] }
```

### `keyOf`
Expand Down
2 changes: 0 additions & 2 deletions website/docs/extended-features/iteration.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ enum _Fruit {
}
class Fruit extends extend<typeof _Fruit, _Fruit>(_Fruit) {}

// USAGE

/**
* Prints:
* "apple"
Expand Down
2 changes: 1 addition & 1 deletion website/docs/extended-features/matches.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ sidebar_position: 3

## Equality - `is`

To determine equality between enum and primitives, keys, or other enums, use **`is`**,
To determine equality between an enum and a primitive, a key, or another enum, use **`is`**,

```ts
enum _Size { small = 'S', medium = 'M', large = 'L' }
Expand Down
2 changes: 1 addition & 1 deletion website/docs/extended-features/parsing.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ enum _LogLevel {
Warn = 'WARN',
Error = 'ERROR',
}
class LogLevel extends extend<typeof _LogLevel, LogLevel>(LogLevel) {}
class LogLevel extends extend<typeof _LogLevel, _LogLevel>(_LogLevel) {}

// USAGE
const level0 = LogLevel.from('INFO'); // LogLevel.Info
Expand Down
4 changes: 2 additions & 2 deletions website/docs/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ sidebar_position: 1

# Get Started

## Installtion
## Installation

Install the package via `npm` or `yarn`.

Expand Down Expand Up @@ -48,7 +48,7 @@ Now, you are good to go!
## What should I do next?

If your existing code was using native TypeScript enums, simply `extend`ing them should require no or very little migration. You can use native TypeScript enum features as it is.
If your existing code was using native TypeScript enums, simply `extend`ing them should require no or very little migration. You may use native features from TypeScript enums as they are.

`extend`ed enums do not break the original enum, so your project could choose to migrate into `extend`ed enums gradually.

Expand Down
3 changes: 2 additions & 1 deletion website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ const darkCodeTheme = require('prism-react-renderer/themes/dracula');
/** @type {import('@docusaurus/types').Config} */
const config = {
title: 'extended-enum',
tagline: 'Grant object-oriented powers to TypeScript enums',
tagline: 'Grant handy utilities and object-oriented powers to TypeScript enums',
url: 'https://inhibitor1217.github.io',
baseUrl: '/extended-enum/',
onBrokenLinks: 'throw',
onBrokenMarkdownLinks: 'warn',
trailingSlash: true,
favicon: 'img/favicon.ico',
organizationName: 'inhibitor1217',
projectName: 'extended-enum',
Expand Down
8 changes: 4 additions & 4 deletions website/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function HomepageHeader() {
export default function Home(): JSX.Element {
return (
<Layout
description="Grant object-oriented powers to TypeScript enums">
description="Grant handy utilities and object-oriented powers to TypeScript enums">
<HomepageHeader />
<main className="container margin-vert--xl">
<section className="row">
Expand All @@ -43,7 +43,7 @@ export default function Home(): JSX.Element {
enum _Priority { Critical = 100, High = 50, Medium = 30, Low = 10 }
// now, your native enum is extended!
class Priority extends extend<typeof _Priority, Priority>(Priority) {}`}
class Priority extends extend<typeof _Priority, _Priority>(_Priority) {}`}
</CodeBlock>
</div>
<div className="col col--2" />
Expand All @@ -55,7 +55,7 @@ class Priority extends extend<typeof _Priority, Priority>(Priority) {}`}
Out-of-the-box utilities
</h2>
<p>
<b>Parsing, iterating and pattern matching</b> utilties are available from the extended enum class.<br/>
<b>Parsing, iterating and pattern matching</b> utilties are available from the extended enum instance.<br/>
<br/>
All of the utilities work seamlessly with TypeScript, so don't worry!
</p>
Expand Down Expand Up @@ -84,7 +84,7 @@ const isHigh = priority.is(Priority.High);
<CodeBlock language="ts">
{`interface IPriority { shouldIgnore(): boolean }
class Priority extends <typeof _Priority, Priority, IPriority>(Priority) {
class Priority extends <typeof _Priority, _Priority, IPriority>(_Priority) {
// define methods in addition to extended enum class
shouldIgnore() {
Expand Down

0 comments on commit 70a7a39

Please sign in to comment.