Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nwaughachukwuma/flatten-ihe
Flatten javascript objects to a single level
https://github.com/nwaughachukwuma/flatten-ihe
Last synced: about 1 month ago
JSON representation
Flatten javascript objects to a single level
- Host: GitHub
- URL: https://github.com/nwaughachukwuma/flatten-ihe
- Owner: nwaughachukwuma
- Created: 2021-11-17T02:32:14.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-12-22T03:48:05.000Z (about 3 years ago)
- Last Synced: 2024-11-08T18:50:47.625Z (2 months ago)
- Language: JavaScript
- Size: 72.3 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# flatten-ihe
Flatten javascript objects to a single level, with the ability to choose delimiter/separator. Ihe is an [Igbo word](https://en.wikipedia.org/wiki/Igbo_language) for Object.
## Installation
```bash
pnpm install flatten-iheyarn add flatten-ihe
npm install flatten-ihe
```## Usage
```js
import flattenIhe from 'flatten-ihe'
// 1. as a simple object flattenerconst obj = {
status: 'success',
user: {
email: '[email protected]',
name: {
first: 'Chukwuma',
last: 'Nwaugha'
}
}
}const result = flattenIhe(obj)
// result : =>
{
'status': 'success',
'user.email': '[email protected]',
'user.name.first': 'Chukwuma',
'user.name.last': 'Nwaugha'
}// 2. accepts a separator
const sep = '_'
const result = flattenIhe(obj, sep)// result :=>
{
'status': 'success',
'user_email': '[email protected]',
'user_name_first': 'Chukwuma',
'user_name_last': 'Nwaugha'
}// 3. can be used in creating route paths in web frameworks
const api = {
users: {
getUser(id) {},
getUsers() {}
},
video: {
getVideo(id) {},
getVideos() {}
},
channel: {
getChannel(id) {},
getChannels() {},
videos: {
getVideo(id, cid) {},
getVideos(cid) {}
}
}
}const sep = '/'
const result = flattenIhe(obj, sep)// result :=>
{
'users/getUser': api.users.getUser,
'users/getUsers': api.users.getUsers,
'video/getVideo': api.video.getVideo,
'video/getVideos': api.video.getVideos,
'channel/getChannel': api.channel.getChannel,
'channel/getChannels': api.channel.getChannels,
'channel/videos/getVideo': api.channel.videos.getVideo,
'channel/videos/getVideos': api.channel.videos.getVideos
}```
## API
```ts
flattenIhe(obj: {[key: string]: any}, sep: string)
```- **obj**: A javascript object of any nesting level.
- **sep**: Separator string for flattening, defaults to '.'