flatten-ihe
Flatten javascript objects to a single level, with the ability to choose delimiter/separator. Ihe is an Igbo word for Object.
Installation
pnpm install flatten-ihe
yarn add flatten-ihe
npm install flatten-ihe
Usage
import flattenIhe from 'flatten-ihe'
const obj = {
status: 'success',
user: {
email: 'nwaughac@gmail.com',
name: {
first: 'Chukwuma',
last: 'Nwaugha'
}
}
}
const result = flattenIhe(obj)
{
'status': 'success',
'user.email': 'nwaughac@gmail.com',
'user.name.first': 'Chukwuma',
'user.name.last': 'Nwaugha'
}
const sep = '_'
const result = flattenIhe(obj, sep)
{
'status': 'success',
'user_email': 'nwaughac@gmail.com',
'user_name_first': 'Chukwuma',
'user_name_last': 'Nwaugha'
}
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)
{
'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
flattenIhe(obj: {[key: string]: any}, sep: string)