mzvonar / getIn

Get value from object by path. Undefined-safe and accepts default value

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

getIn Build Status Coverage Status npm version

Get value from object by path. Path can be string or array (e.g. ['user', 'profile', 'gender']).
If any value in path is undefined then undefined is returned or defaultValue if provided.

Installation

npm install @mzvonar/getin

Usage

const getIn = require('@mzvonar/getin');
  
const context = {
    user: {
        profile: {
            gender: 'female'
        }
    }
};
  
const gender = getIn(context, ['user', 'profile', 'gender']);

gender is female

const country = getIn(context, ['user', 'address', 'country']);

country is undefined

const verified = getIn(context, ['user', 'profile', 'verified'], false);

verified is false

Tests

npm test

About

Get value from object by path. Undefined-safe and accepts default value

License:MIT License


Languages

Language:JavaScript 100.0%