Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
airtoxin committed Mar 6, 2021
1 parent 25fd211 commit 69b95ba
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# purify-ts-extra-codec

Extra utility codecs for [purify-ts](https://gigobyte.github.io/purify/).
🛠 Extra utility codecs for [purify-ts](https://gigobyte.github.io/purify/).

## Install

Expand Down Expand Up @@ -32,6 +32,8 @@ ObjCodec.decode({ str: "foo" }) // Right({ str: "foo" });
const obj: GetInterface<typeof ObjCodec> = { str: "foo" };
```

❤️ Support `schema`.

### String Codec Module

#### NonEmptyString
Expand All @@ -44,6 +46,8 @@ NonEmptyString.decode(""); // Left("[error message]")
NonEmptyString.decode(1234); // Left("[error message]")
```

❤️ Support `schema`.

#### StringLengthRangedIn

Ensure length of input string is in range.
Expand All @@ -54,16 +58,25 @@ StringLengthRangedIn({ gte: 5, lte: 5 }).decode("asdf"); // Left("[error message
StringLengthRangedIn({ gte: 5, lte: 5 }).decode(1234); // Left("[error message]")
```

❤️ Support `schema`.

#### RegExpMatchedString

Ensure input string matched to RegExp.

```typescript
RegExpMatchedString("\\w+").decode("asdf"); // Right("asdf")
RegExpMatchedString("\\w+").decode("1234"); // Left("[error message]")
RegExpMatchedString("\\w+").decode(1234); // Left("[error message]")

// those are deprecated interface (Non schema support)
RegExpMatchedString(/\w+/).decode("asdf"); // Right("asdf")
RegExpMatchedString(/\w+/).decode("1234"); // Left("[error message]")
RegExpMatchedString(/\w+/).decode(1234); // Left("[error message]")
```

❤️ Support `schema`.

#### FormattedStringFromDate

Convert Date instance into formatted string.
Expand All @@ -74,6 +87,8 @@ FormattedStringFromDate("yyyy_MM_dd").decode(new Date("InvalidDate")); // Left("
FormattedStringFromDate("yyyy_MM_dd").decode("asdf"); // Left("[error message]")
```

⚠️ No `schema` support.

### Number Codec Module

#### NumberRangedIn
Expand All @@ -86,6 +101,8 @@ NumberRangedIn({ gte: 2, lt: 5 }).decode(0); // Left("[error message]")
NumberRangedIn({ gt: 2, lte: 5 }).decode("a"); // Left("[error message]")
```

❤️ Support `schema`.

#### NumberFromString

Convert string into number (if parsable).
Expand All @@ -96,6 +113,8 @@ NumberFromString.decode("Infinity"); // Left("[error message]")
NumberFromString.decode(1234); // Left("[error message]")
```

❤️ Support `schema`.

#### Integer

Ensure input number is integer.
Expand All @@ -106,6 +125,8 @@ Integer.decode(12.34); // Left("[error message]")
Integer.decode("1234"); // Left("[error message]")
```

❤️ Support `schema`.

#### IntegerFromString

Convert string into integer number (if possible).
Expand All @@ -116,6 +137,8 @@ IntegerFromString.decode("12.34"); // Left("[error message]")
IntegerFromString.decode(1234); // Left("[error message]")
```

❤️ Support `schema`.

### Date Codec Module

#### DateFromAny
Expand All @@ -128,6 +151,8 @@ DateFromAny.decode(1577804400000); // Right(Wed Jan 01 2020 00:00:00)
DateFromAny.decode("today"); // Left("[error message]")
```

⚠️ No `schema` support.

#### DateFromStringFormatOf

Convert formatted date string into Date.
Expand All @@ -138,6 +163,8 @@ DateFromStringFormatOf("yyyy_MM_dd").decode("2020"); // Left("[error message]")
DateFromStringFormatOf("yyyy_MM_dd").decode(new Date()); // Left("[error message]")
```

⚠️ No `schema` support.

### Json Codec Module

#### JsonFromString
Expand All @@ -150,8 +177,24 @@ JsonFromString(Codec.interface({ type: string })).decode(`{}`); // Left("[error
JsonFromString(Codec.interface({ type: string })).decode(1234); // Left("[error message]")
```

⚠️ No `schema` support.

### Codec Utility Module

#### withSchema

Utility for adding a schema after defined.

```typescript
withSchema(
MyCodec,
(myCodecSchema) => ({
...myCodecSchema,
pattern: "^[a-fA-F\\d]{8}$"
})
);
```

#### extendCodec

Utility for type narrowing.
Expand Down

0 comments on commit 69b95ba

Please sign in to comment.