2.0.5 • Published 3 years ago

undefsafe v2.0.5

Weekly downloads
3,338,300
License
MIT
Repository
github
Last release
3 years ago

undefsafe

Simple function for retrieving deep object properties without getting "Cannot read property 'X' of undefined"

Can also be used to safely set deep values.

Usage

var object = {
  a: {
    b: {
      c: 1,
      d: [1,2,3],
      e: 'remy'
    }
  }
};

console.log(undefsafe(object, 'a.b.e')); // "remy"
console.log(undefsafe(object, 'a.b.not.found')); // undefined

Demo: https://jsbin.com/eroqame/3/edit?js,console

Setting

var object = {
  a: {
    b: [1,2,3]
  }
};

// modified object
var res = undefsafe(object, 'a.b.0', 10);

console.log(object); // { a: { b: [10, 2, 3] } }
console.log(res); // 1 - previous value

Star rules in paths

As of 1.2.0, undefsafe supports a * in the path if you want to search all of the properties (or array elements) for a particular element.

The function will only return a single result, either the 3rd argument validation value, or the first positive match. For example, the following github data:

const githubData = {
        commits: [{
          modified: [
            "one",
            "two"
          ]
        }, /* ... */ ]
      };

// first modified file found in the first commit
console.log(undefsafe(githubData, 'commits.*.modified.0'));

// returns `two` or undefined if not found
console.log(undefsafe(githubData, 'commits.*.modified.*', 'two'));
nodemoner-notifications-api@everreal/er-common-react-components@everreal/react-building-stacking-planer-prism-web@everreal/er-common-helpersdiscord-music-bots@everreal/react-mui-downshifthexa-node-commonexpress-api-pack-tropea-bootstraap@everything-registry/sub-chunk-3010taxtoolstallytellstallytellsappwhatsconcsaturninosemplice-watchrfp-librolandetechbase-x-betateapackage-tatespoorman297seaotterteste_cegidvalidarminajscreatoremina-salib-mobtimer-api2mjl001mergearrayofobjectmergearrayofobjectbyidlizikang01hellohubjsbinjmempesa-cookiempesa-cookie-jarhw9hello-heratgmojjsiotgjfogjmynodeapp2245lx_mod-mainmatrix_implementationnodejsmasternodejs_batch1_22maynodemonplusnodemon-msikmanodemon-tea-carenodus-service-manageropenai-node-expressopea-bootstrappropertymanagentclass9pin-apinpmtest-05041royman-experiment-npm-publishsnyk-add-to-packagesnyk-brokersigyets-joi-objectidundefsafe-typedvue-picocss@ajayos/nodemon@b-gran/nodemon@bkrmadtya/sorting-steps9-12-1@a1motion/nodemonyanbaobaoyanbaobao1wangyaling-1avis-cal@leafphp/watcher@nikandlv/fox@nachttheferret/utils@mlamota2020/gc-cli@kadabra/nodemon@kandelvijaya/homebridge-philipsair-platform@jacobbubu/nodemon@imrtoy/homebridge-night-light@kacao/chops@samwinslow/edgedb@orschweitzer/git-clone-package@sudophunk/v4-client@remy/snyk-shrink-test@stackbuild/nodemoncauseway-concrete-stylesajayos-nodemon@zambezi/jsbinappxgenclassgroliaccsvalapp@vandai-nguyen/module_test@valuis0429/nodemon@xiwen5566/aqara-automation-switchdk_2018_1_1docker-nodemoner-common-componentsewm-dico-enginegdphpttgenerate-password-by-osb
2.0.5

3 years ago

2.0.3

5 years ago

2.0.2

7 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.3.1

8 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.1.0

9 years ago

1.0.0

9 years ago

0.0.3

10 years ago

0.0.2

11 years ago

0.0.1

11 years ago