Skip to content

Commit

Permalink
Release 0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
danilofuchs committed Nov 7, 2023
1 parent efbb316 commit 21cd5cf
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.5.0] - 2023-11-07

- **feat:** `unflatten` function

## [0.4.1] - 2021-11-18

- **fix:** `type 'List<dynamic>' is not a subtype of type 'List<Object>' in type cast` when flattening a JSON decoded object
Expand Down
77 changes: 44 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

Take a nested Map and flatten it with delimited keys. Entirely based on node.js [flat](https://www.npmjs.com/package/flat).

> It does not achieve feature parity yet, as some options are missing from `flatten` (maxDepth, transformKey).
> It does not achieve feature parity yet, as some options are missing from `flatten` (transformKey) and `unflatten` (`object`, `overwrite`, `transformKey`).
> Currently, it bails out of a tree if it finds something different than a `Map` or `List`.
> Currently, it bails out of a tree if it finds something different than a `Map` or `List` when flatening, or a nested `Map` or `List` when unflattening.
## Usage
## Methods

### `flatten`

```dart
import 'package:flat/flat.dart';
Expand All @@ -23,7 +25,7 @@ flatten(
},
},
);
// {
// "a": 1,
// "list1.0": "item1",
Expand All @@ -35,39 +37,13 @@ flatten(
// "f.h": true,
// "f.j": "text"
// }
unflatten({
"a": 1,
"list1.0": "item1",
"list1.1": "item2",
"f.list2.0": "item3",
"f.list2.1": "item4",
"f.list2.2": "item5",
"f.g": 2,
"f.h": true,
"f.j": "text"
});
// {
// "a": 1,
// "list1": ["item1", "item2"],
// "f": {
// "list2": ["item3", "item4", "item5"],
// "g": 2,
// "h": true,
// "j": "text",
// },
// }
```

## Options

### delimiter
#### `delimiter`

Use a custom delimiter for flattening your objects, instead of `.`

### safe
#### `safe`

When enabled, flat will preserve arrays and their contents. This is disabled by default.

Expand All @@ -93,7 +69,7 @@ flatten({
// };
```

### maxDepth
#### `maxDepth`

Maximum number of nested objects to flatten.

Expand All @@ -118,3 +94,38 @@ flatten({
// }
// };
```

### `unflatten`

Reverse process from `flatten`.

> Only supports completely flat maps as input.
```dart
unflatten({
"a": 1,
"list1.0": "item1",
"list1.1": "item2",
"f.list2.0": "item3",
"f.list2.1": "item4",
"f.list2.2": "item5",
"f.g": 2,
"f.h": true,
"f.j": "text"
});
// {
// "a": 1,
// "list1": ["item1", "item2"],
// "f": {
// "list2": ["item3", "item4", "item5"],
// "g": 2,
// "h": true,
// "j": "text",
// },
// }
```

#### `delimiter`

Use a custom delimiter for unflattening your objects, instead of `.`
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: flat
description: >-
A simple, no dependency 🚀 way to take a nested Map and flatten it into a single depth Map.
Based on Node.js `flat` package.
A simple, no dependency 🚀 package to convert a nested Map and flatten it into a single depth Map,
and recover it later. Based on Node.js `flat` package.
homepage: https://github.com/danilofuchs/flat
repository: https://github.com/danilofuchs/flat

version: 0.4.1
version: 0.5.0

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down

0 comments on commit 21cd5cf

Please sign in to comment.