Skip to content

Commit

Permalink
Merge pull request #257 from JUSTIVE/feature-string-length
Browse files Browse the repository at this point in the history
feat: Added P.string.length
  • Loading branch information
gvergnaud authored Jun 12, 2024
2 parents 7d08131 + 473041e commit 1a568a5
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1339,6 +1339,19 @@ const fn = (input: string) =>
console.log(fn('two')); // logs '🎉'
```

### `P.string.length`

`P.string.length(min)` matches strings with exactly `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.
Expand Down
14 changes: 14 additions & 0 deletions src/patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,18 @@ const endsWith = <input, const end extends string>(
const minLength = <const min extends number>(min: min) =>
when((value) => isString(value) && value.length >= min);

/**
* `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)
*
* @example
* match(value)
* .with(P.string.length(10), () => 'strings with length === 10')
*/
const length = <const len extends number>(len: len) =>
when((value) => isString(value) && value.length === len);

/**
* `P.string.maxLength(max)` is a pattern, matching **strings** with at most `max` characters.
*
Expand Down Expand Up @@ -762,6 +774,8 @@ const stringChainable = <pattern extends Matcher<any, any, any, any, any>>(
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) =>
Expand Down
15 changes: 15 additions & 0 deletions src/types/Pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,21 @@ export type StringChainable<
MergeGuards<input, p, GuardExcludeP<unknown, string, never>>,
omitted | 'minLength'
>;
/**
* `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)
*
* @example
* match(value)
* .with(P.string.length(10), () => 'strings with length === 10')
*/
length<input, const len extends number>(
len: len
): StringChainable<
MergeGuards<input, p, GuardExcludeP<unknown, string, never>>,
omitted | 'length' | 'minLength' | 'maxLength'
>;
/**
* `P.string.maxLength(max)` is a pattern, matching **strings** with at most `max` characters.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/strings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,24 @@ 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<Equal<typeof value, string>>;
return 'yes';
})
.otherwise((value) => {
type t = Expect<Equal<typeof value, string | number>>;
return 'no';
});

expect(f('aa')).toBe('yes');
expect(f('bb')).toBe('yes');
expect(f('aaa')).toBe('no');
expect(f('a')).toBe('no');
});

it(`P.string.maxLength(..)`, () => {
const f = (input: string | number) =>
match(input)
Expand Down

0 comments on commit 1a568a5

Please sign in to comment.