From 69b95ba330d9def49d3f1df4f2f1d2d481f35610 Mon Sep 17 00:00:00 2001 From: airtoxin Date: Sun, 7 Mar 2021 06:29:53 +0900 Subject: [PATCH] Add documentation --- README.md | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4bd22e3..fd3e30c 100644 --- a/README.md +++ b/README.md @@ -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 @@ -32,6 +32,8 @@ ObjCodec.decode({ str: "foo" }) // Right({ str: "foo" }); const obj: GetInterface = { str: "foo" }; ``` +❤️ Support `schema`. + ### String Codec Module #### NonEmptyString @@ -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. @@ -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. @@ -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 @@ -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). @@ -96,6 +113,8 @@ NumberFromString.decode("Infinity"); // Left("[error message]") NumberFromString.decode(1234); // Left("[error message]") ``` +❤️ Support `schema`. + #### Integer Ensure input number is integer. @@ -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). @@ -116,6 +137,8 @@ IntegerFromString.decode("12.34"); // Left("[error message]") IntegerFromString.decode(1234); // Left("[error message]") ``` +❤️ Support `schema`. + ### Date Codec Module #### DateFromAny @@ -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. @@ -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 @@ -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.