Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

flatten-ihe

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

flatten-ihe

Flatten javascript objects to a single level

  • 0.0.7
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
177
decreased by-54.26%
Maintainers
1
Weekly downloads
 
Created
Source

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'
// 1. as a simple object flattener

const obj = {
    status: 'success',
    user: {
        email: 'nwaughac@gmail.com',
        name: {
            first: 'Chukwuma',
            last: 'Nwaugha'
        }
    }
}

const result = flattenIhe(obj)

// result : =>
{
    'status': 'success',
    'user.email': 'nwaughac@gmail.com',
    'user.name.first': 'Chukwuma',
    'user.name.last': 'Nwaugha'
}

// 2. accepts a separator

  const sep = '_'
  const result = flattenIhe(obj, sep)

// result :=>
{
    'status': 'success',
    'user_email': 'nwaughac@gmail.com',
    '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

flattenIhe(obj: {[key: string]: any}, sep: string)
  • obj: A javascript object of any nesting level.

  • sep: Separator string for flattening, defaults to '.'

Keywords

FAQs

Package last updated on 21 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc