aichholzer / salvus

Safely set, get and delete any property in any object.
MIT License
3 stars 2 forks source link

salvus

Greenkeeper badge Build Status Dependency status Downloads Always useful

Tu ne cede malis sed contra audentior ito.

Safely set, get and remove any property, in any object.

Say goodbye to those (ugly and unfriendly) undefined exceptions and having to probe multiple properties to get what you actually want. Let salvus handle it for you.

The default usage, require('salvus'), will bind salvus to your application's objects as a prototype, making it available everywhere.

Install

npm install --save salvus

Default use

require('salvus');

const soldier = {
  name: 'Persephone',
  age: 23
};

soldier.lego('name');
// Persephone

soldier.lego('contact.address.city');
// undefined

soldier.noto('contact.address.city:Rome');
// soldier is now
// {
     name: 'Persephone',
     age: 23,
     contact: {
       address: {
         city: 'Rome'
       }
     }
   }

soldier.noto(['wars.fought:100', 'wars.won:97']);
// soldier is now
// {
     name: 'Persephone',
     age: 23,
     contact: {
       address: {
         city: 'Rome'
       }
     },
     wars: {
       fought: 100,
       won: 97
     }
   }

Function use

cons salvus = require('salvus/lib/io');
const soldier = {
  name: 'Persephone',
  age: 23
};

salvus.lego(soldier, 'name');
// Persephone

salvus.lego(soldier, 'contact.address.city');
// undefined

salvus.noto(soldier, 'contact.address.city:Rome');
// soldier is now
// {
     name: 'Persephone',
     age: 23,
     contact: {
       address: {
         city: 'Rome'
       }
     }
   }

salvus.noto(soldier, ['wars.fought:100', 'wars.won:97']);
// soldier is now
// {
     name: 'Persephone',
     age: 23,
     contact: {
       address: {
         city: 'Rome'
       }
     },
     wars: {
       fought: 100,
       won: 97
     }
   }

The object parameter always represents the object being operated on.

More examples

Take a look at the tests; tests/index.js and tests/io.js.

Contribute

fork https://github.com/aichholzer/salvus/

License

MIT

Silentium est aureum.