Skip to content

Commit 07fb919

Browse files
authored
Merge pull request #332 from gvergnaud/gvergnaud/feat-p-record-implementation
feat(P.record): implementation
2 parents 29fbd3a + 2b18391 commit 07fb919

File tree

12 files changed

+3904
-1880
lines changed

12 files changed

+3904
-1880
lines changed

README.md

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,10 @@ Check out 👉 [Type-Level TypeScript](https://type-level-typescript.com/), an o
106106
- [Wildcards](#wildcards)
107107
- [Objects](#objects)
108108
- [Tuples (arrays)](#tuples-arrays)
109-
- [Sets](#pset-patterns)
110-
- [Maps](#pmap-patterns)
111109
- [`P.array` patterns](#parray-patterns)
110+
- [`P.record` patterns](#precord-patterns)
111+
- [`P.set`](#pset-patterns)
112+
- [`P.map`](#pmap-patterns)
112113
- [`P.when` patterns](#pwhen-patterns)
113114
- [`P.not` patterns](#pnot-patterns)
114115
- [`P.select` patterns](#pselect-patterns)
@@ -1047,6 +1048,68 @@ const output = match(input)
10471048
.otherwise((input) => input);
10481049
```
10491050

1051+
### `P.record` patterns
1052+
1053+
To match a `Record<Key, Value>` (an object with consistent key and value types), you can use `P.record(keyPattern, valuePattern)`.
1054+
It takes a sub-pattern to match against the key, a sub-pattern to match against the value, and will match if **all entries** in the object
1055+
match these two sub-patterns.
1056+
1057+
```ts
1058+
import { match, P } from 'ts-pattern';
1059+
1060+
type Input = Record<string, number>;
1061+
1062+
const input: Input = {
1063+
alice: 100,
1064+
bob: 85,
1065+
charlie: 92,
1066+
};
1067+
1068+
const output = match(input)
1069+
.with(P.record(P.string, P.number), (scores) => `All user scores`)
1070+
.with(P.record(P.string, P.string), (names) => `All user names`)
1071+
.otherwise(() => '');
1072+
1073+
console.log(output);
1074+
// => "All user scores"
1075+
```
1076+
1077+
You can also use `P.record` with a single argument `P.record(valuePattern)`, which assumes string keys:
1078+
1079+
```ts
1080+
const userProfiles = {
1081+
alice: { name: 'Alice', age: 25 },
1082+
bob: { name: 'Bob', age: 30 },
1083+
};
1084+
1085+
const output = match(userProfiles)
1086+
.with(
1087+
P.record({ name: P.string, age: P.number }),
1088+
(profiles) => `User profiles with name and age`
1089+
)
1090+
.otherwise(() => 'Different format');
1091+
1092+
console.log(output);
1093+
// => "User profiles with name and age"
1094+
```
1095+
1096+
When using `P.select` in record patterns, you can extract all keys or all values as arrays:
1097+
1098+
```ts
1099+
const data = { a: 1, b: 2, c: 3 };
1100+
1101+
const keys = match(data)
1102+
.with(P.record(P.string.select(), P.number), (keys) => keys)
1103+
.otherwise(() => []);
1104+
1105+
const values = match(data)
1106+
.with(P.record(P.string, P.number.select()), (values) => values)
1107+
.otherwise(() => []);
1108+
1109+
console.log(keys); // => ['a', 'b', 'c']
1110+
console.log(values); // => [1, 2, 3]
1111+
```
1112+
10501113
### `P.set` patterns
10511114

10521115
To match a Set, you can use `P.set(subpattern)`.

jsr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@gabriel/ts-pattern",
3-
"version": "5.8.0",
3+
"version": "5.9.0",
44
"exports": {
55
".": "./src/index.ts",
66
"./types": "./src/index.ts",

0 commit comments

Comments
 (0)