🚨 Active Supply Chain Attack:node-ipc Package Compromised.Learn More
Socket
Book a DemoSign in
Socket

keypaths

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

keypaths

A tools for javascript Object key paths.

latest
npmnpm
Version
0.1.5
Version published
Maintainers
1
Created
Source

Keypaths

A tools for javascript Object key paths.

Install

npm install keypaths --save

Test

git clone https://github.com/hiowenluke/keypaths
cd keypaths
npm install
npm test

Usage

Convert the key paths of an object to an array

const kp = require('keypaths');

const obj = {
    a: {},
    b: {
        b1: {
            b11: {}
        },
        b2: {},
    },
    c: {},
};

const paths = kp.toPaths(obj);
console.log(paths);

Result

[
    'a',
    'b.b1.b11',
    'b.b2',
    'c',
]

Convert a key paths string array to an object

const kp = require('keypaths');

const paths = [
    'a',
    'b.b1.b11',
    'b.b2',
    'c',
];

const obj = kp.toObject(paths);
console.log(obj);

Result

{
    a: {},
    b: {
        b1: {
            b11: {}
        },
        b2: {},
    },
    c: {},
}

Get by key path

const kp = require('keypaths');

const obj = {
    a: {
        b: {
            c: 123
        }
    }
};

console.log(kp.get(obj, 'a.b.c')); // 123

Set by key path

const kp = require('keypaths');

const obj = {
    a: {
        b: {
            c: 123
        }
    }
};

kp.set(obj, 'a.b.c', [1, 2, 3]);
console.log(kp.get(obj, 'a.b.c')); // [1, 2, 3]

License

MIT

Copyright (c) 2019, Owen Luke

FAQs

Package last updated on 06 Jan 2020

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