sleeve

failsafe object property checker

Usage no npm install needed!

<script type="module">
  import sleeve from 'https://cdn.skypack.dev/sleeve';
</script>

README

sleeve

always have an ace up your sleeve


explore, search for and read Javascript object nested properties without many a nasty error
npm version

Installation:

npm install --save sleeve

Usage:

const s = require('sleeve');

Syntax:

s(object, "chain.of.properties.that.can.be.rather.long", fallbackValue)

Examples:
const user = {
    name: "John",
    login: "jjohanson",
    contacts: {
        phoneNumber: "(555)834-1337"
    },
    location:{},
    data: {
        a:{
            b: ""
        }
    }
};
not okay:
const email = user && user.contacts && user.contacts.email && user.contacts.email.primaryEmail ? user.contacts.email.primaryEmail : "no email provided";
okay:

fallbacks:

const email = s(user, "contacts.email.primaryEmail", "no email provided");
console.log(email);  // "no email provided"

property checking:

let countryCode;
if (!s(user, "location.country.code")) {
    countryCode = "US";
}

reading value:

console.log(s(user, "data.a.b.c.going.too.deep")); // null

callback function:

s(user, "i.do.not.know.where.to.go", () => console.log("this is a fail"));