Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ddeltree authored Jun 26, 2024
1 parent edf06d0 commit 7287ce0
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ A package that provides some helper functions for **implementing leaf value chan

## Examples

Suppose we have the following interfaces:
### propose and save a list of entries

```ts
import changes from 'leavify/changes';

interface Book {
title: string;
author: string;
Expand All @@ -29,19 +31,12 @@ interface Chapter {
title: string;
number: number;
}

const book: Book = {
title: 'The Golden Compass',
author: 'Philip Pullman',
year: 1995,
chapters: [{ title: 'The Decanter of Tokay', number: 1 }],
};
```

### propose and save a list of entries

```ts
import changes from 'leavify/changes';

changes.propose(book, [
['author', 'Pullman'],
Expand All @@ -52,19 +47,6 @@ changes.save(book);
changes.getOriginal(book).chapters?.[0].number; // 1
```

### iterate the leaf entries of an object

```ts
import { walkLeaves, toTree } from 'leavify';

for (const [path, value] of walkLeaves(book)) {
console.log(path, value); // ['title', 'new title']
}

// Leaves to/from tree
const tree = toTree([...walkLeaves(book)]);
```

### set up getter methods for a class

```ts
Expand All @@ -88,6 +70,19 @@ book.title; // 'new title'
book.original.title; // 'default title'
```

### iterate the leaf entries of an object

```ts
import { walkLeaves, toTree } from 'leavify';

for (const [path, value] of walkLeaves(book)) {
console.log(path, value); // ['title', 'new title']
}

// Leaves to/from tree
const tree = toTree([...walkLeaves(book)]);
```

## API

Here's a brief description of each function defined:
Expand Down

0 comments on commit 7287ce0

Please sign in to comment.