Skip to content

Commit 0d8bf60

Browse files
committed
README
1 parent a372b93 commit 0d8bf60

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

packages/utils/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ The package provides a comprehensive set of utilities for traversing, transformi
176176

177177
### Visiting Nodes
178178

179+
| Function | Description |
180+
| ------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------- |
181+
| [`forEachNode`](https://github.com/datocms/structured-text/blob/main/packages/utils/src/manipulation.ts#L109) | Visit every node in the tree synchronously using pre-order traversal |
182+
| [`forEachNodeAsync`](https://github.com/datocms/structured-text/blob/main/packages/utils/src/manipulation.ts#L142) | Visit every node in the tree asynchronously using pre-order traversal |
183+
179184
Visit all nodes in the tree using pre-order traversal:
180185

181186
```javascript
@@ -194,6 +199,11 @@ await forEachNodeAsync(structuredText, async (node, parent, path) => {
194199

195200
### Transforming Trees
196201

202+
| Function | Description |
203+
| --------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------- |
204+
| [`mapNodes`](https://github.com/datocms/structured-text/blob/main/packages/utils/src/manipulation.ts#L176) | Transform nodes in the tree synchronously while preserving structure |
205+
| [`mapNodesAsync`](https://github.com/datocms/structured-text/blob/main/packages/utils/src/manipulation.ts#L257) | Transform nodes in the tree asynchronously while preserving structure |
206+
197207
Transform nodes while preserving the tree structure:
198208

199209
```javascript
@@ -217,6 +227,13 @@ const processed = await mapNodesAsync(structuredText, async (node) => {
217227

218228
### Finding Nodes
219229

230+
| Function | Description |
231+
| -------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------ |
232+
| [`collectNodes`](https://github.com/datocms/structured-text/blob/main/packages/utils/src/manipulation.ts#L340) | Collect all nodes that match a predicate function |
233+
| [`collectNodesAsync`](https://github.com/datocms/structured-text/blob/main/packages/utils/src/manipulation.ts#L398) | Collect all nodes that match an async predicate function |
234+
| [`findFirstNode`](https://github.com/datocms/structured-text/blob/main/packages/utils/src/manipulation.ts#L437) | Find the first node that matches a predicate function |
235+
| [`findFirstNodeAsync`](https://github.com/datocms/structured-text/blob/main/packages/utils/src/manipulation.ts#L515) | Find the first node that matches an async predicate function |
236+
220237
Find specific nodes using predicates or type guards:
221238

222239
```javascript
@@ -248,6 +265,11 @@ const strongText = collectNodes(
248265

249266
### Filtering Trees
250267

268+
| Function | Description |
269+
| ------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------- |
270+
| [`filterNodes`](https://github.com/datocms/structured-text/blob/main/packages/utils/src/manipulation.ts#L564) | Remove nodes that don't match a predicate synchronously |
271+
| [`filterNodesAsync`](https://github.com/datocms/structured-text/blob/main/packages/utils/src/manipulation.ts#L647) | Remove nodes that don't match an async predicate |
272+
251273
Remove nodes that don't match a predicate:
252274

253275
```javascript
@@ -267,6 +289,11 @@ const validated = await filterNodesAsync(structuredText, async (node) => {
267289

268290
### Reducing Trees
269291

292+
| Function | Description |
293+
| ------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------- |
294+
| [`reduceNodes`](https://github.com/datocms/structured-text/blob/main/packages/utils/src/manipulation.ts#L734) | Reduce the tree to a single value using a synchronous reducer function |
295+
| [`reduceNodesAsync`](https://github.com/datocms/structured-text/blob/main/packages/utils/src/manipulation.ts#L779) | Reduce the tree to a single value using an async reducer function |
296+
270297
Reduce the entire tree to a single value:
271298

272299
```javascript
@@ -297,6 +324,13 @@ const nodeCounts = reduceNodes(
297324

298325
### Checking Conditions
299326

327+
| Function | Description |
328+
| ---------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- |
329+
| [`someNode`](https://github.com/datocms/structured-text/blob/main/packages/utils/src/manipulation.ts#L821) | Check if any node in the tree matches a predicate (short-circuit evaluation) |
330+
| [`someNodeAsync`](https://github.com/datocms/structured-text/blob/main/packages/utils/src/manipulation.ts#L862) | Check if any node in the tree matches an async predicate (short-circuit evaluation) |
331+
| [`everyNode`](https://github.com/datocms/structured-text/blob/main/packages/utils/src/manipulation.ts#L903) | Check if every node in the tree matches a predicate (short-circuit evaluation) |
332+
| [`everyNodeAsync`](https://github.com/datocms/structured-text/blob/main/packages/utils/src/manipulation.ts#L934) | Check if every node in the tree matches an async predicate (short-circuit evaluation) |
333+
300334
Test if any or all nodes match a condition:
301335

302336
```javascript

0 commit comments

Comments
 (0)