JSPM

bonnefooi

1.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q5648F
  • License GPL-3.0

Get and set nested nonexistent object properties without errors.

Package Exports

  • bonnefooi

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (bonnefooi) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Bonnefooi

This npm package lets you access nonexistent object properties in javascript.

Bonnefooi is Dutch for going on a journey without planning ahead.

Usage

npm install bonnefooi

When you try to access a nonexistent member, it will automatically be initialized as an empty object. This means you can safely do this:

const bonnefooi = require('bonnefooi');

let myobj = {};
let myobjSafe = new bonnefooi(myobj);

myobjSafe.something.somethingelse.importantnumber = 123;
console.log(myobj); // Output: { something: { somethingelse: { importantnumber: 123 } } }

And you can even do this:

const bonnefooi = require('bonnefooi');

let myobjSafe = new bonnefooi({});

myobjSafe.something.somethingelse.importantnumber = 123;
console.log(myobjSafe); // Output: { something: { somethingelse: { importantnumber: 123 } } }

What it can't do

Beware that some code requires nonexistent properties to return undefined. For example, this will result in a stack overflow:

const bonnefooi = require('bonnefooi');

let myobjSafe = new bonnefooi({});

myobjSafe.something.somethingelse.importantnumber = 123;
JSON.stringify(myobjSafe); // infinite recursion

But this works fine:

const bonnefooi = require('bonnefooi');

let myobj = {};
let myobjSafe = new bonnefooi(myobj);

myobjSafe.something.somethingelse.importantnumber = 123;
JSON.stringify(myobj); // String: { something: { somethingelse: { importantnumber: 123 } } }

myobjSafe.hasOwnProperty() also does not work, because it's a proxy object which doesn't have an object prototype. If you want to check whether a property exists or not, refer to the original object, or use in:

const bonnefooi = require('bonnefooi');

let myobjSafe = new bonnefooi({});
console.log('nonexistentproperty' in myobjSafe); // Output: false

License

GNU GPLv3