Giter VIP home page Giter VIP logo

get-value-safely's Introduction

get-value-safely JS npm

JavaScript mini-library to get nested values safely.

Assures that a value of a child chain property is returned without runtime errors.

If the checked property does not exist in the provided object or one of the children in the chain is undefined, then it returns the provided fallback value.

The fallback can be also returned if the checked property is falsy (i.e. undefined, null, false, 0, '', etc.) and the fallbackOnFalsy flag is set to true.

Arrays could be checked as well by passing the index in the path; ex: getValue(arr, '0.obj.a')

Install

$ npm i get-value-safely

Import

const get = require('get-value-safely');

Use

// Test object.
const obj = {
  c: {
    a: 2,
    b: {
      c: [1, 2, 3, {a: 15, b: 10}, 15]
    },
    c: undefined,
    d: null
  },
  d: ''
};

get(null, 'c.a', 'fallback');

get(obj, 'c.d', 'fallback');

get(obj, 'c.c', 'fallback', true);

get(obj, 'c.b.c.2');

get(obj, 'c.b.c.3.a');

Tests Results

Check test.js to see the full source.

// Same test object.

assert('Test that null object returns fallback', 'fallback' === get(null, 'c.d.d', 'fallback'));

assert('Test that null path returns fallback', 'fallback' === get(obj, null, 'fallback'));

assert('Test that null object and path returns fallback', 'fallback' === get(null, null, 'fallback'));

assert('Test that non existing property returns fallback', 'fallback' === get(obj, 'c.d.d', 'fallback'));

assert('Test that a null falsy property value is well returned', null === get(obj, 'c.d', 'fallback'));

assert('Test that a undefined falsy property value is well returned', undefined === get(obj, 'c.c', 'fallback'));

assert('Test that fallback is returned when falsy flag is activated', 'fallback' === get(obj, 'c.c', 'fallback', true));

assert('Test that an array property returns the well the array', obj.c.b.c === get(obj, 'c.b.c', 'fallback'));

assert('Test that array index value is well returned', 3 === get(obj, 'c.b.c.2', 'fallback'));

assert('Test that fallback is returns when index does not exist', 'fallback' === get(obj, 'c.b.c.10', 'fallback'));

assert('Test that undefined is returned when index and fallback does not exist', undefined === get(obj, 'c.b.c.10'));

assert('Test that nested object inside array can be accesed by array index', 15 === get(obj, 'c.b.c.3.a'));

assert('Test that array length property can be accessed', 5 === get(obj, 'c.b.c.length', 'fallback'));

assert('Test that fallback is not split in reduce, by index', 'fallback' === get(obj, 'c.z.1', 'fallback'));

assert('Test that fallback is not split in reduce, also for falsy', 'fallback' === get(obj, 'c.z.1', 'fallback', true));

The results

screen shot 2018-08-15 at 12 20 22 pm

get-value-safely's People

Contributors

vsambor avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.