From 002034d38a01eb0c90d05a6c4dd6879caa3b2ff1 Mon Sep 17 00:00:00 2001 From: JUSTIVE Date: Wed, 5 Jun 2024 13:29:54 +0900 Subject: [PATCH 1/5] feat: Added P.string.length --- README.md | 13 +++++++++++++ src/patterns.ts | 14 ++++++++++++++ src/types/Pattern.ts | 15 +++++++++++++++ tests/strings.test.ts | 17 +++++++++++++++++ 4 files changed, 59 insertions(+) diff --git a/README.md b/README.md index b199bcbc..3b38bd31 100644 --- a/README.md +++ b/README.md @@ -1339,6 +1339,19 @@ const fn = (input: string) => console.log(fn('two')); // logs '🎉' ``` +### `P.string.length` + +`P.string.length(min)` matches strings with exact `len` characters. + +```ts +const fn = (input: string) => + match(input) + .with(P.string.length(2), () => '🎉') + .otherwise(() => '❌'); + +console.log(fn('ok')); // logs '🎉' +``` + ### `P.string.maxLength` `P.string.maxLength(max)` matches strings with at most `max` characters. diff --git a/src/patterns.ts b/src/patterns.ts index 4deeb1ac..adbc2495 100644 --- a/src/patterns.ts +++ b/src/patterns.ts @@ -712,6 +712,18 @@ const endsWith = ( const minLength = (min: min) => when((value) => isString(value) && value.length >= min); +/** + * `P.string.length(len)` is a pattern, matching **strings** with at exact `len` characters. + * + * [Read the documentation for `P.string.length` on GitHub](https://github.com/gvergnaud/ts-pattern#pstringlength) + * + * @example + * match(value) + * .with(P.string.minLength(10), () => 'string with more length >= 10') + */ +const length = (len: len) => + when((value) => isString(value) && value.length === len); + /** * `P.string.maxLength(max)` is a pattern, matching **strings** with at most `max` characters. * @@ -762,6 +774,8 @@ const stringChainable = >( stringChainable(intersection(pattern, endsWith(str))), minLength: (min: number) => stringChainable(intersection(pattern, minLength(min))), + length: (len: number) => + stringChainable(intersection(pattern, length(len))), maxLength: (max: number) => stringChainable(intersection(pattern, maxLength(max))), includes: (str: string) => diff --git a/src/types/Pattern.ts b/src/types/Pattern.ts index 2d2e610e..b6aefa58 100644 --- a/src/types/Pattern.ts +++ b/src/types/Pattern.ts @@ -313,6 +313,21 @@ export type StringChainable< MergeGuards>, omitted | 'minLength' >; + /** + * `P.string.length(len)` is a pattern, matching **strings** with at exact `len` characters. + * + * [Read the documentation for `P.string.length` on GitHub](https://github.com/gvergnaud/ts-pattern#pstringlength) + * + * @example + * match(value) + * .with(P.string.length(10), () => 'string with exact length === 10') + */ + length( + len: len + ): StringChainable< + MergeGuards>, + omitted | 'length' + >; /** * `P.string.maxLength(max)` is a pattern, matching **strings** with at most `max` characters. * diff --git a/tests/strings.test.ts b/tests/strings.test.ts index c59cc677..7a9d2160 100644 --- a/tests/strings.test.ts +++ b/tests/strings.test.ts @@ -108,6 +108,23 @@ describe('Strings', () => { expect(f('a')).toBe('no'); }); + it(`P.string.length(..)`, () => { + const f = (input: string | number) => + match(input) + .with(P.string.length(2), (value) => { + type t = Expect>; + return 'yes'; + }) + .otherwise((value) => { + type t = Expect>; + return 'no'; + }); + + expect(f('aa')).toBe('yes'); + expect(f('aaa')).toBe('no'); + expect(f('a')).toBe('no'); + }); + it(`P.string.maxLength(..)`, () => { const f = (input: string | number) => match(input) From 85277274178d35ee3991510eceb137076bbef848 Mon Sep 17 00:00:00 2001 From: JUSTIVE Date: Wed, 5 Jun 2024 15:16:28 +0900 Subject: [PATCH 2/5] chore: consistent typo --- src/patterns.ts | 4 ++-- src/types/Pattern.ts | 4 ++-- tests/strings.test.ts | 1 + 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/patterns.ts b/src/patterns.ts index adbc2495..dd821f87 100644 --- a/src/patterns.ts +++ b/src/patterns.ts @@ -713,13 +713,13 @@ const minLength = (min: min) => when((value) => isString(value) && value.length >= min); /** - * `P.string.length(len)` is a pattern, matching **strings** with at exact `len` characters. + * `P.string.length(len)` is a pattern, matching **strings** with exact `len` characters. * * [Read the documentation for `P.string.length` on GitHub](https://github.com/gvergnaud/ts-pattern#pstringlength) * * @example * match(value) - * .with(P.string.minLength(10), () => 'string with more length >= 10') + * .with(P.string.length(10), () => 'strings with length === 10') */ const length = (len: len) => when((value) => isString(value) && value.length === len); diff --git a/src/types/Pattern.ts b/src/types/Pattern.ts index b6aefa58..86e2a776 100644 --- a/src/types/Pattern.ts +++ b/src/types/Pattern.ts @@ -314,13 +314,13 @@ export type StringChainable< omitted | 'minLength' >; /** - * `P.string.length(len)` is a pattern, matching **strings** with at exact `len` characters. + * `P.string.length(len)` is a pattern, matching **strings** with exact `len` characters. * * [Read the documentation for `P.string.length` on GitHub](https://github.com/gvergnaud/ts-pattern#pstringlength) * * @example * match(value) - * .with(P.string.length(10), () => 'string with exact length === 10') + * .with(P.string.length(10), () => 'strings with length === 10') */ length( len: len diff --git a/tests/strings.test.ts b/tests/strings.test.ts index 7a9d2160..56b0672e 100644 --- a/tests/strings.test.ts +++ b/tests/strings.test.ts @@ -121,6 +121,7 @@ describe('Strings', () => { }); expect(f('aa')).toBe('yes'); + expect(f('bb')).toBe('yes'); expect(f('aaa')).toBe('no'); expect(f('a')).toBe('no'); }); From d3284224b47a4ac6f8ccdc244d01190312c7ab27 Mon Sep 17 00:00:00 2001 From: JUSTIVE Date: Sat, 8 Jun 2024 08:39:23 +0900 Subject: [PATCH 3/5] fix: typos in doc in P.string.length --- src/patterns.ts | 2 +- src/types/Pattern.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/patterns.ts b/src/patterns.ts index dd821f87..b08d8dfd 100644 --- a/src/patterns.ts +++ b/src/patterns.ts @@ -713,7 +713,7 @@ const minLength = (min: min) => when((value) => isString(value) && value.length >= min); /** - * `P.string.length(len)` is a pattern, matching **strings** with exact `len` characters. + * `P.string.length(len)` is a pattern, matching **strings** with exactly `len` characters. * * [Read the documentation for `P.string.length` on GitHub](https://github.com/gvergnaud/ts-pattern#pstringlength) * diff --git a/src/types/Pattern.ts b/src/types/Pattern.ts index 86e2a776..b4d4184e 100644 --- a/src/types/Pattern.ts +++ b/src/types/Pattern.ts @@ -314,7 +314,7 @@ export type StringChainable< omitted | 'minLength' >; /** - * `P.string.length(len)` is a pattern, matching **strings** with exact `len` characters. + * `P.string.length(len)` is a pattern, matching **strings** with exactly `len` characters. * * [Read the documentation for `P.string.length` on GitHub](https://github.com/gvergnaud/ts-pattern#pstringlength) * From 5c6a62c01cd9d91d3e9067022c772e64570dd1da Mon Sep 17 00:00:00 2001 From: JUSTIVE Date: Sat, 8 Jun 2024 08:41:18 +0900 Subject: [PATCH 4/5] fix: omit minLength, and maxLength after length --- src/types/Pattern.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/Pattern.ts b/src/types/Pattern.ts index b4d4184e..47e124d3 100644 --- a/src/types/Pattern.ts +++ b/src/types/Pattern.ts @@ -326,7 +326,7 @@ export type StringChainable< len: len ): StringChainable< MergeGuards>, - omitted | 'length' + omitted | 'length' | 'minLength' | 'maxLength' >; /** * `P.string.maxLength(max)` is a pattern, matching **strings** with at most `max` characters. From 473041e5caa74f219765904bd0327f5e446c02d5 Mon Sep 17 00:00:00 2001 From: gvergnaud Date: Wed, 12 Jun 2024 08:27:06 -0400 Subject: [PATCH 5/5] feat(str.length): fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3b38bd31..27f8c689 100644 --- a/README.md +++ b/README.md @@ -1341,7 +1341,7 @@ console.log(fn('two')); // logs '🎉' ### `P.string.length` -`P.string.length(min)` matches strings with exact `len` characters. +`P.string.length(min)` matches strings with exactly `len` characters. ```ts const fn = (input: string) =>